This still isn't the map trait, but I think you could try passing a block
to the find function, like this (the syntax is likely a bit out of date...
):
fn find<'a>(&'a self, key: &K, block: &fn(Option<&'a V>) -> T) -> T;
Then, instead of doing something like,
let x = m.find(y);
... do stuff with x ...
you'd do something like this:
m.find(y, |x| {
... do stuff with x ...
})
On Mon, Sep 30, 2013 at 4:30 PM, David Brown <[email protected]> wrote:
> On Thu, Sep 26, 2013 at 12:58:48PM -0700, Brandon Sanderson wrote:
>
> If the values are cached, and the structure implementing the cache has
>> the same lifetime (or longer? Unsure on that...) as the map then I'd
>> expect it to work.� For generated/retrieved, non-cached values I don't
>> believe it would work.
>>
>
> I'm still not sure this would quite be right. How long would we keep
> things in the cache, since the compiler only knows the lifetime is the
> same as the structure holding it, that would suggest nothing could be
> removed from the cache.
>
> For now, I'm just going to return similar operations that return
> cloneable values rather than references. My use cases are small types
> (such as uint), so this doesn't really hurt anything.
>
> Thanks,
> David
>
> On 2013-09-25 7:54 PM, "Daniel Micay" <[1][email protected]>
>> wrote:
>>
>> On Wed, Sep 25, 2013 at 10:36 PM, David Brown <[2][email protected]>
>>
>> wrote:
>>
>> Let's say I'm implementing some type of map-like structure that
>> I'd
>> like to be able to implement the Map trait on.
>>
>> However, my map doesn't actually store the 'V' directly, but they
>> are
>> computed/retrieve/cached in some manner.
>>
>> Is this possible with the existing map trait? �Given that:
>>
>> �fn find<'a>(&'a self, key: &K) -> Option<&'a V>;
>>
>> doesn't the V need to be a pointer to something with the same
>> lifetime
>> as self?
>>
>> Is there a way of dealing with this, or should I just implement my
>> own
>> similar find function, not implementing Map, and return an
>> Option<V>
>> instead?
>>
>> The Map trait represents a map storing the values, so it can't be
>> used for that use case.
>> ______________________________**_________________
>> Rust-dev mailing list
>> [3][email protected]
>>
>> [4]https://mail.mozilla.org/**listinfo/rust-dev<https://mail.mozilla.org/listinfo/rust-dev>
>>
>> References
>>
>> Visible links
>> 1. mailto:[email protected]
>> 2. mailto:[email protected]
>> 3. mailto:[email protected]
>> 4.
>> https://mail.mozilla.org/**listinfo/rust-dev<https://mail.mozilla.org/listinfo/rust-dev>
>>
>
> ______________________________**_________________
>> Rust-dev mailing list
>> [email protected]
>> https://mail.mozilla.org/**listinfo/rust-dev<https://mail.mozilla.org/listinfo/rust-dev>
>>
>
> ______________________________**_________________
> Rust-dev mailing list
> [email protected]
> https://mail.mozilla.org/**listinfo/rust-dev<https://mail.mozilla.org/listinfo/rust-dev>
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev