Lifetime when using chunks

I have a vector of lots of Point structures that I wish to iterate constantly in batches.

For example

loop {
    let mut all_points : Vec<Point> = points;

    for chunk in all_points.chunks_mut(100) {
        for point in chunk.to_owned() {
        }
    }
}

This works ok but I would like to able to access the point as a mutable

if I change the above to

loop {
    let mut all_points : Vec<Point> = points;

    for chunk in all_points.chunks_mut(100) {
        for point in chunk.iter_mut() {
        }
    }
}

I get the error

for chunk in all_points.chunks_mut(100) {
    |                      ^^^^^^^^^^------------------------------
    |                      |
    |                      borrowed value does not live long enough
    |                      argument requires that `all_points` is borrowed for `'static`

How can I work around this ?

Thanks

Can you give more context? I don't get this error on the playground.

It often helps to provide the full unmodified error message. In this case, maybe the full error explains why rust wants to borrow something for 'static.

Sorry it involves threads as well. I didn't realise.

Ah, so the problem is that threads can outlive the scope that creates them, so they can't safely take non-'static references. crossbeam::scope allows you to work around this by guaranteeing that all threads will have terminated by the time the values are destroyed.

If you want to avoid libraries and Point implements Default (or has a cheap throwaway value), you can std::mem::take it (or std::mem::replace it) and move each point into its thread. Each thread should then return the updated Point, and when you join them you can put them back into all_points.

If Point doesn't implement Default or have a cheap throwaway value and you don't mind unsafe , you can just transmute the lifetime of the point mutable references to 'static (as crossbeam::scope does internally) - but this requires a lot of care.

3 Likes

Thanks for that. I remember now about using crossbeam for this type of thing. I always wondered how crossbeam was able to do what it did internally.