These are chat archives for rust-lang/rust

29th
Nov 2017
Fra ns
@snarf95_twitter
Nov 29 2017 09:16
let mut chunk_render_list = 
    chunk_indices.into_iter()
        .filter_map(|i| chunks.get_mut(&i))
        .filter_map(|chnk| chnk.render_snapshot.as_mut());
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
What to do
Jonas Platte
@jplatte
Nov 29 2017 09:48
post the full error message
or a playground link
Fra ns
@snarf95_twitter
Nov 29 2017 09:55
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
   --> src\main.rs:281:36
    |
281 |             .filter_map(|i| chunks.get_mut(&i))
    |                                    ^^^^^^^
    |
note: first, the lifetime cannot outlive the lifetime  as defined on the body at 281:25...
   --> src\main.rs:281:25
    |
281 |             .filter_map(|i| chunks.get_mut(&i))
    |                         ^^^^^^^^^^^^^^^^^^^^^^
note: ...so that closure can access `chunks`
   --> src\main.rs:281:29
    |
281 |             .filter_map(|i| chunks.get_mut(&i))
    |                             ^^^^^^
note: but, the lifetime must be valid for the expression at 282:32...
   --> src\main.rs:282:32
    |
282 |             .filter_map(|chnk| chnk.render_snapshot.as_mut());
    |                                ^^^^
note: ...so type `&mut ChunkWorldInfo<gfx_device_gl::Resources>` of expression is valid
during the expression
   --> src\main.rs:282:32
    |
282 |             .filter_map(|chnk| chnk.render_snapshot.as_mut());
    |                                ^^^^
Jonas Platte
@jplatte
Nov 29 2017 14:47
@snarf95_twitter I think there's a bit of context missing...
Is the full code available somewhere?
Zakarum
@omni-viral
Nov 29 2017 14:49
What the type of expression chunks.get_mut(&i)?
This message was deleted
Option<?>
This is the version where it works but I need to change a value of render_snapshot inside the following loop
Zakarum
@omni-viral
Nov 29 2017 14:52
So it breaks when you change get to get_mut and as_ref to as_mut?
Fra ns
@snarf95_twitter
Nov 29 2017 14:54
Correct
Zakarum
@omni-viral
Nov 29 2017 14:54
Try change to this .filter_map(|i| chunks.get_mut(&i).and_then(|chnk| chnk.render_snapshot.as_mut()))
Fra ns
@snarf95_twitter
Nov 29 2017 14:54
I think I already have tried that with the same result :/
Denis Lisov
@tanriol
Nov 29 2017 14:55
And it is completely correct to do so. Note that nothing in this code guarantees that chunk_indices contains only unique indices. However, if they were not unique, you'd have two mutable references into the same snapshot.
Fra ns
@snarf95_twitter
Nov 29 2017 14:57
I’m not quite following you @tanriol
Zakarum
@omni-viral
Nov 29 2017 14:57
@snarf95_twitter If you add collect to the end you'll end up with collection with mutable references.
But there is no guarantie that those references will be different
That's why borrow checker complains that it cannot infer an appropriate lifetime
Fra ns
@snarf95_twitter
Nov 29 2017 14:59
I see
Zakarum
@omni-viral
Nov 29 2017 14:59
You'll have to do filtering in for loop
@tanriol So closure that returns mutable reference to the captured value can only be FnOnce?
There's no such thing as FnMutThatYouCanCallAgainOnlyAfterYouDropPreviouslyReturnedValue
Denis Lisov
@tanriol
Nov 29 2017 15:03
@omni-viral Not exactly sure, but makes sense to me.
Zakarum
@omni-viral
Nov 29 2017 15:04
In another words
trait FnMutBorrowed<A> {
    type Output<'a>;
    fn call(&'a mut self, args: A) -> Self::Output<'a>;
}
It doesn't work even with FnOnce
Fra ns
@snarf95_twitter
Nov 29 2017 15:11
Okay thanks guys for helping me understand this :+1: