32 ms·
This actually brings me to one of the few (current?) limitations of rust that drive me nuts! In all the other modern languages I work with, my big projects end
by ComputerGuru 5y ago
This actually brings me to one of the few (current?) limitations of rust that drive me nuts! In all the other modern languages I work with, my big projects end up with a utility RAII class that wraps a closure to execute when the scope expires. It’s great for cleaning up logical operations as opposed to what is traditionally done by RAII destructors in oop languages.
Eg if I’m creating a temporary file or directory in a function to handle some IO logic, I’ll create a RAII wrapper around a closure that deletes that folder when the scope expires - this might be a one-off thing so instead of creating an actual RAII “TemporaryFolder” object that wraps around a path, creates the folder in its constructor, and deletes the folder and its contents in the destructor (and have to reference the inner path instead of the object itself when needing it, etc), I can just stick a closure to delete the folder in my wrapper and call it a day.
You can’t really do that in rust because you’d have to give ownership of the file/folder/whatever to the closure - meaning you can’t use it yourself afterwards! Technically the compiler just has to guarantee that the object persists to the end of scope (isn’t destroyed or moved out earlier) and then there would be no borrow conflicts (since it’s only run after everything else has finished and can’t be used for anything else in the RAII wrapper) but it can’t really know that, so you’re disallowed from sharing it between your actual code and the cleanup closure.
- masklinn 5y ago> You can’t really do that in rust because you’d have to give ownership of the file/folder/whatever to the closure - meaning you can’t use it yourself afterwards! The way to do this is pretty much what C++ has in unique_ptr(p, deleter): have the utility RAII class own both the object and the closure, dereference to the wrapped object, and on drop pass the wrapped object to the closure. Altho > Eg if I’m creating a temporary file or directory in a function to handle some IO logic, I’ll create a RAII wrapper around a closure that deletes that folder when the scope expires There are crates for lots of those situations. https://crates.io/crates/tempfile https://crates.io/crates/tempfile for instance.
- ComputerGuru 5y ago> have the utility RAII class own both the object and the closure Yeah, but the ergonomics suck. Especially for random cleanup tasks that aren’t necessarily one-object, one-task. The temp file thing was just an example, actual use case was more domain-specific.
- masklinn 5y ago> Yeah, but the ergonomics suck. Especially for random cleanup tasks that aren’t necessarily one-object, one-task. Doesn’t seem much more of a problem? Put a tuple in, get references out, cleanup gets the entire tuple. If lifetimes get complicated, use refcells and / or Rcs.
- tomjakubowski 5y agoIf it's only a temp file (as opposed to a dir) and it's Linux, you may want to use O_TMPFILE (possibly O_EXCL too) when open()ing the file instead of the RAII wrapper. That ensures the OS will delete the file even if the program is killed.
- notriddle 5y agohttps://docs.rs/tempfile/3.3.0/src/tempfile/file/imp/unix.rs.html#71-86 https://docs.rs/tempfile/3.3.0/src/tempfile/file/imp/unix.rs...
- ComputerGuru 5y agoAn additional (and more important) constraint to being on Linux and creating a single file is that this assumes you are the one creating the file, which is oftentimes not the case. e.g. in my rust ffmpeg wrapper, I have pipelines that invoke ffmpeg and through it create multiple intermediate files that need to be cleaned up at the end of the run in all cases (success and failure).
- slaymaker1907 5y agoI'm not sure what your performance constraints are, but you could try using RefCell<T> for objects which need to be shared with the closure. It's a fairly low overhead abstraction. Also, you can try creating a reference explicitly that gets used in the closure for objects which don't need mut references. let helperX = &x; let ycell = RefCell::new(y); let yref = &ycell; fn helper() { helperX.whatever(); ycell.borrow_mut().ystuff(); } In this case, RefCell checks at runtime that you don't have an outstanding borrow in your regular code (which you can avoid via explicit drops or by carefully using blocks). It's essentially a very low overhead RW lock and unlike Rc, it doesn't even allocate on the heap.