Having trouble with a lifetime for a reference passed with the value itself
I solved my previous problems with implementing an enum that supports polling different types of async results, but now I'm running into a lifetime problem.
My thinking is as follows -
(L78) I hold on to the enum values in the remaining futures vec.
(L84-85) We get the values back from select_all in .2 minus the one that completed, and move that back into our remaining_futures variable. next is the value of the future that completed.
(L90) we match and move the value of a ReadDir to rd.
Calling next_entry will be a reference to rd, so we need to ensure that the ReadDir value is kept alive alongside the future, which we do in another field of the tuple.
In my head at least, I'm being careful to ensure that the value is kept alive to be referenced, but I'm really struggling to express that to the compiler (or I'm not understanding something).
Any help very welcome to help me understand what I'm missing here.
Thanks!
Posted Jun 5, 2021, 7:01 PM UTC
Your problem is that you are attempting to construct a self referential struct which you can't safely do in Rust. You're creating a reference to
rdby callingrd.next_entry()and are then attempting to moverd. If the compiler let you do this, the reference you created would become invalid. Also if you were able to construct that struct, moving it would invalidate the reference to rd again.basically:
This doesn't work that easily. You could pin rd to the heap (
Box::pin(rd)) and then use raw pointers to it in your future. This would requireunsafeand thus quite some safety reasoning of course.This design seems somewhat overcomplicated. Is there a reason you can't just have a tokio task per directory which iterates over it's children and in turn spawns a new task for iterating over each child folders children?
Thanks, that's useful to think about.
I've done a few toy projects in Rust, but I've often avoided some areas intentionally (particularly the borrows and lifetimes). I'm trying to put myself in a position where I've got some non-trivial problem to solve that forces me to work through and understand some things that you just don't hit in rustlings exercises. If I understand the problem that makes the direction of the solution untenable, I've learnt something and can rewrite it in another way until I've gotten through it all, but I'm not just immediately picking the easier paths because I don't have a full toolbox of approaches to solving problems yet.
More out of curiosity than anything, this is possible with metadata, but it seems to come down to the issue that
metadataandread_diruseto_owned()to grab an owned copy of the paths. (some code modifications left out, but the main gist...)