Skip to content

Commit

Permalink
fix: more rustic way of option chaining in constructor?
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenrahnemoon committed Mar 5, 2024
1 parent 9d46080 commit abcd4c0
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions libraries/common-arm/src/sd_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use embedded_sdmmc as sd;
use hal::spi::FullDuplex;

use messages::ErrorContext;
use sd::Directory;
use crate::herror;

/// Time source for `[SdInterface]`. It doesn't return any useful information for now, and will
Expand Down Expand Up @@ -75,33 +74,35 @@ where
}
};

let mut root_directory: Option<Directory> = None;
if volume.is_some() {
root_directory = match sd_controller.open_root_dir(volume.as_ref().unwrap()) {
let root_directory = match &volume {
Some(volume) => match sd_controller.open_root_dir(volume) {
Ok(root_directory) => Some(root_directory),
Err(_) => {
error!("Cannot get root");
None
}
};
}

let file: Result<sd::File, sd::Error<sd::SdMmcError>>;
if volume.is_some() && root_directory.is_some() {
file = sd_controller.open_file_in_dir(
volume.as_mut().unwrap(),
root_directory.as_ref().unwrap(),
"log.txt",
sd::Mode::ReadWriteCreateOrTruncate,
);
}

let file: Option<sd::File> = match file {
Ok(file) => Some(file),
Err(_) => {
error!("Cannot create file.");
None
}
_ => None,
};

let file = match (&mut volume, &root_directory) {
(Some(volume), Some(root_directory)) => {
let _file = sd_controller.open_file_in_dir(
volume,
&root_directory,
"log.txt",
sd::Mode::ReadWriteCreateOrTruncate,
);
let _file = match _file {
Ok(__file) => Some(__file),
Err(_) => {
error!("Cannot create file.");
None
}
};
_file
},
_ => None
};

SdManager {
Expand All @@ -111,6 +112,7 @@ where
file,
}
}

pub fn write(
&mut self,
file: &mut sd::File,
Expand All @@ -121,6 +123,7 @@ where
}
self.sd_controller.write(self.volume.as_mut().unwrap(), file, buffer)
}

pub fn write_str(
&mut self,
file: &mut sd::File,
Expand All @@ -132,6 +135,7 @@ where
let buffer: &[u8] = msg.as_bytes();
self.sd_controller.write(self.volume.as_mut().unwrap(), file, buffer)
}

pub fn open_file(&mut self, file_name: &str) -> Result<sd::File, sd::Error<sd::SdMmcError>> {
if !self.is_mounted() {
return Err(sd::Error::DeviceError(sd::SdMmcError::CardNotFound));
Expand All @@ -143,6 +147,7 @@ where
sd::Mode::ReadWriteCreateOrTruncate,
)
}

pub fn close_current_file(&mut self) -> Result<(), sd::Error<sd::SdMmcError>> {
if !self.is_mounted() {
return Err(sd::Error::DeviceError(sd::SdMmcError::CardNotFound));
Expand All @@ -152,19 +157,21 @@ where
}
Ok(())
}

pub fn close_file(&mut self, file: sd::File) -> Result<(), sd::Error<sd::SdMmcError>> {
if !self.is_mounted() {
return Err(sd::Error::DeviceError(sd::SdMmcError::CardNotFound));
}
self.sd_controller.close_file(self.volume.as_ref().unwrap(), file)
}

pub fn close(&mut self) -> Result<(), sd::Error<sd::SdMmcError>> {
if !self.is_mounted() {
return Err(sd::Error::DeviceError(sd::SdMmcError::CardNotFound));
}
self.sd_controller.close_dir(
self.volume.as_ref().unwrap(),
self.root_directory.as_ref().unwrap()
self.root_directory.unwrap()

Check failure on line 174 in libraries/common-arm/src/sd_manager.rs

View workflow job for this annotation

GitHub Actions / All

cannot move out of `self.root_directory` which is behind a mutable reference

Check failure on line 174 in libraries/common-arm/src/sd_manager.rs

View workflow job for this annotation

GitHub Actions / clippy

cannot move out of `self.root_directory` which is behind a mutable reference

error[E0507]: cannot move out of `self.root_directory` which is behind a mutable reference --> libraries/common-arm/src/sd_manager.rs:174:13 | 174 | self.root_directory.unwrap() | ^^^^^^^^^^^^^^^^^^^ -------- `self.root_directory` moved due to this method call | | | help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents | move occurs because `self.root_directory` has type `core::option::Option<embedded_sdmmc::Directory>`, which does not implement the `Copy` trait | note: `core::option::Option::<T>::unwrap` takes ownership of the receiver `self`, which moves `self.root_directory` --> /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/option.rs:928:25 help: you could `clone` the value and consume it, if the `embedded_sdmmc::Directory: core::clone::Clone` trait bound could be satisfied | 174 | <core::option::Option<embedded_sdmmc::Directory> as Clone>::clone(&self.root_directory).unwrap() | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +
);
Ok(())
}
Expand Down

0 comments on commit abcd4c0

Please sign in to comment.