Explore interior mutability
jonathanpallant opened this issue · 2 comments
Now that critical-section
is a thing, maybe we can consider interior mutability:
Volume
can hold an&VolumeManager
Directory
andFile
can hold an&Volume
let volume = volume_mgr.get_volume(VolumeIdx(0))?;
let root_dir = volume.open_root_dir()?;
let file = root_dir.open("HELLO.TXT")?;
let mut buffer = [0u8; 64];
let contents = file.read_to_buffer(&mut buffer)?;
So one issue here is stopping people opening the same volume twice, but allowing all the objects to be stashed in a struct for later use (so they can't refer to each other).
Perhaps the VolumeManager can hold all the volumes internally and lend out &mut Volume references cheaply, but only for one Volume at a time. You could even const generic the number of Volumes a VolumeManager can hold (one is usually fine, but sometimes there's an EFI partition first or something). Then I think for opening files you can either get a File (which holds a &Volume) or a RawFile (which does not, but does hold some unique ID for the Volume so it'll panic if you use it with the wrong Volume in future).
Looked at this in #103 is a start on this, but using &mut
to start.
Also there's an old branch: https://github.com/rust-embedded-community/embedded-sdmmc-rs/tree/make_api_immutable