Skip to content

Commit d33233d

Browse files
authored
Merge pull request #143 from rust-embedded-community/interior-mutability
Use a RefCell to provide interior mutability for VolumeManager.
2 parents aec477c + 413d814 commit d33233d

21 files changed

+592
-491
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ let sdcard = embedded_sdmmc::SdCard::new(sdmmc_spi, delay);
1717
println!("Card size is {} bytes", sdcard.num_bytes()?);
1818
// Now let's look for volumes (also known as partitions) on our block device.
1919
// To do this we need a Volume Manager. It will take ownership of the block device.
20-
let mut volume_mgr = embedded_sdmmc::VolumeManager::new(sdcard, time_source);
20+
let volume_mgr = embedded_sdmmc::VolumeManager::new(sdcard, time_source);
2121
// Try and access Volume 0 (i.e. the first partition).
2222
// The volume object holds information about the filesystem on that volume.
23-
let mut volume0 = volume_mgr.open_volume(embedded_sdmmc::VolumeIdx(0))?;
23+
let volume0 = volume_mgr.open_volume(embedded_sdmmc::VolumeIdx(0))?;
2424
println!("Volume 0: {:?}", volume0);
2525
// Open the root directory (mutably borrows from the volume).
26-
let mut root_dir = volume0.open_root_dir()?;
26+
let root_dir = volume0.open_root_dir()?;
2727
// Open a file called "MY_FILE.TXT" in the root directory
2828
// This mutably borrows the directory.
29-
let mut my_file = root_dir.open_file_in_dir("MY_FILE.TXT", embedded_sdmmc::Mode::ReadOnly)?;
29+
let my_file = root_dir.open_file_in_dir("MY_FILE.TXT", embedded_sdmmc::Mode::ReadOnly)?;
3030
// Print the contents of the file, assuming it's in ISO-8859-1 encoding
3131
while !my_file.is_eof() {
3232
let mut buffer = [0u8; 32];
@@ -43,7 +43,7 @@ By default the `VolumeManager` will initialize with a maximum number of `4` open
4343

4444
```rust
4545
// Create a volume manager with a maximum of 6 open directories, 12 open files, and 4 volumes (or partitions)
46-
let mut cont: VolumeManager<_, _, 6, 12, 4> = VolumeManager::new_with_limits(block, time_source);
46+
let cont: VolumeManager<_, _, 6, 12, 4> = VolumeManager::new_with_limits(block, time_source);
4747
```
4848

4949
## Supported features

examples/append_file.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
3030
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
3131
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
3232
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
33-
let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
33+
let volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
3434
VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
35-
let mut volume = volume_mgr.open_volume(VolumeIdx(0))?;
36-
let mut root_dir = volume.open_root_dir()?;
35+
let volume = volume_mgr.open_volume(VolumeIdx(0))?;
36+
let root_dir = volume.open_root_dir()?;
3737
println!("\nCreating file {}...", FILE_TO_APPEND);
38-
let mut f = root_dir.open_file_in_dir(FILE_TO_APPEND, Mode::ReadWriteAppend)?;
38+
let f = root_dir.open_file_in_dir(FILE_TO_APPEND, Mode::ReadWriteAppend)?;
3939
f.write(b"\r\n\r\nThis has been added to your file.\r\n")?;
4040
Ok(())
4141
}

examples/big_dir.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
1111
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
1212
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
1313
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
14-
let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
14+
let volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
1515
VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
16-
let mut volume = volume_mgr
16+
let volume = volume_mgr
1717
.open_volume(embedded_sdmmc::VolumeIdx(1))
1818
.unwrap();
1919
println!("Volume: {:?}", volume);
20-
let mut root_dir = volume.open_root_dir().unwrap();
20+
let root_dir = volume.open_root_dir().unwrap();
2121

2222
let mut file_num = 0;
2323
loop {
2424
file_num += 1;
2525
let file_name = format!("{}.da", file_num);
2626
println!("opening file {file_name} for writing");
27-
let mut file = root_dir
27+
let file = root_dir
2828
.open_file_in_dir(
2929
file_name.as_str(),
3030
embedded_sdmmc::Mode::ReadWriteCreateOrTruncate,

examples/create_file.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
3030
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
3131
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
3232
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
33-
let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
33+
let volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
3434
VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
35-
let mut volume = volume_mgr.open_volume(VolumeIdx(0))?;
36-
let mut root_dir = volume.open_root_dir()?;
35+
let volume = volume_mgr.open_volume(VolumeIdx(0))?;
36+
let root_dir = volume.open_root_dir()?;
3737
println!("\nCreating file {}...", FILE_TO_CREATE);
3838
// This will panic if the file already exists: use ReadWriteCreateOrAppend
3939
// or ReadWriteCreateOrTruncate instead if you want to modify an existing
4040
// file.
41-
let mut f = root_dir.open_file_in_dir(FILE_TO_CREATE, Mode::ReadWriteCreate)?;
41+
let f = root_dir.open_file_in_dir(FILE_TO_CREATE, Mode::ReadWriteCreate)?;
4242
f.write(b"Hello, this is a new file on disk\r\n")?;
4343
Ok(())
4444
}

examples/delete_file.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
3333
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
3434
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
3535
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
36-
let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
36+
let volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
3737
VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
38-
let mut volume = volume_mgr.open_volume(VolumeIdx(0))?;
39-
let mut root_dir = volume.open_root_dir()?;
38+
let volume = volume_mgr.open_volume(VolumeIdx(0))?;
39+
let root_dir = volume.open_root_dir()?;
4040
println!("Deleting file {}...", FILE_TO_DELETE);
4141
root_dir.delete_file_in_dir(FILE_TO_DELETE)?;
4242
println!("Deleted!");

examples/list_dir.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ fn main() -> Result<(), Error> {
4747
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
4848
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
4949
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
50-
let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
50+
let volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
5151
VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
52-
let mut volume = volume_mgr.open_volume(VolumeIdx(0))?;
52+
let volume = volume_mgr.open_volume(VolumeIdx(0))?;
5353
let root_dir = volume.open_root_dir()?;
5454
list_dir(root_dir, "/")?;
5555
Ok(())
@@ -59,7 +59,7 @@ fn main() -> Result<(), Error> {
5959
///
6060
/// The path is for display purposes only.
6161
fn list_dir(
62-
mut directory: Directory<LinuxBlockDevice, Clock, 8, 8, 4>,
62+
directory: Directory<LinuxBlockDevice, Clock, 8, 8, 4>,
6363
path: &str,
6464
) -> Result<(), Error> {
6565
println!("Listing {}", path);

examples/read_file.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,15 @@ fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
4747
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
4848
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
4949
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
50-
let mut volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
50+
let volume_mgr: VolumeManager<LinuxBlockDevice, Clock, 8, 8, 4> =
5151
VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
52-
let mut volume = volume_mgr.open_volume(VolumeIdx(0))?;
53-
let mut root_dir = volume.open_root_dir()?;
52+
let volume = volume_mgr.open_volume(VolumeIdx(0))?;
53+
let root_dir = volume.open_root_dir()?;
5454
println!("\nReading file {}...", FILE_TO_READ);
55-
let mut f = root_dir.open_file_in_dir(FILE_TO_READ, Mode::ReadOnly)?;
55+
let f = root_dir.open_file_in_dir(FILE_TO_READ, Mode::ReadOnly)?;
56+
// Proves we can open two files at once now (or try to - this file doesn't exist)
57+
let f2 = root_dir.open_file_in_dir("MISSING.DAT", Mode::ReadOnly);
58+
assert!(f2.is_err());
5659
while !f.is_eof() {
5760
let mut buffer = [0u8; 16];
5861
let offset = f.offset();

examples/readme_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,16 @@ fn main() -> Result<(), Error> {
125125
println!("Card size is {} bytes", sdcard.num_bytes()?);
126126
// Now let's look for volumes (also known as partitions) on our block device.
127127
// To do this we need a Volume Manager. It will take ownership of the block device.
128-
let mut volume_mgr = embedded_sdmmc::VolumeManager::new(sdcard, time_source);
128+
let volume_mgr = embedded_sdmmc::VolumeManager::new(sdcard, time_source);
129129
// Try and access Volume 0 (i.e. the first partition).
130130
// The volume object holds information about the filesystem on that volume.
131-
let mut volume0 = volume_mgr.open_volume(embedded_sdmmc::VolumeIdx(0))?;
131+
let volume0 = volume_mgr.open_volume(embedded_sdmmc::VolumeIdx(0))?;
132132
println!("Volume 0: {:?}", volume0);
133133
// Open the root directory (mutably borrows from the volume).
134-
let mut root_dir = volume0.open_root_dir()?;
134+
let root_dir = volume0.open_root_dir()?;
135135
// Open a file called "MY_FILE.TXT" in the root directory
136136
// This mutably borrows the directory.
137-
let mut my_file = root_dir.open_file_in_dir("MY_FILE.TXT", embedded_sdmmc::Mode::ReadOnly)?;
137+
let my_file = root_dir.open_file_in_dir("MY_FILE.TXT", embedded_sdmmc::Mode::ReadOnly)?;
138138
// Print the contents of the file, assuming it's in ISO-8859-1 encoding
139139
while !my_file.is_eof() {
140140
let mut buffer = [0u8; 32];

0 commit comments

Comments
 (0)