Skip to main content

FlashBlock

Struct FlashBlock 

Source
pub struct FlashBlock { /* private fields */ }
Available on non-crate feature host only.
Expand description

Type of a FlashArray block, with methods such as load, save, and clear.

See FlashArray for usage examples.

Implementations§

Source§

impl FlashBlock

Source

pub fn load<T>(&mut self) -> Result<Option<T>>
where T: Serialize + for<'de> Deserialize<'de>,

Load data stored in this block.

See FlashArray for usage examples.

Examples found in repository?
examples/flash.rs (line 58)
40async fn inner_main(_spawner: Spawner) -> Result<()> {
41    info!("Flash Storage Example");
42
43    // Initialize hardware
44    let p = embassy_rp::init(Default::default());
45
46    // Initialize Flash device
47    let [_, _, _, mut string_block, mut config_block] = FlashArray::<5>::new(p.FLASH)?;
48
49    info!("Part 1: Storing data to flash");
50    string_block.save(&String::<64>::try_from("Hello, Flash Storage!")?)?;
51    config_block.save(&SensorConfig {
52        name: String::<32>::try_from("Temperature")?,
53        sample_rate_hz: 1000,
54        enabled: true,
55    })?;
56
57    info!("Part 2: Reading data from flash");
58    let string: Option<String<64>> = string_block.load()?;
59    assert!(string.as_deref() == Some("Hello, Flash Storage!"));
60    let config: Option<SensorConfig> = config_block.load()?;
61    assert!(
62        config
63            == Some(SensorConfig {
64                name: String::<32>::try_from("Temperature")?,
65                sample_rate_hz: 1000,
66                enabled: true,
67            })
68    );
69
70    info!("Part 3: Reading a different type counts as empty");
71    // Try to read the string block as a SensorConfig
72    let wrong_type_result: Option<SensorConfig> = string_block.load()?;
73    assert!(wrong_type_result.is_none());
74
75    info!("Part 4: Clearing flash blocks");
76    string_block.clear()?;
77    config_block.clear()?;
78
79    info!("Part 5: Verifying cleared blocks");
80    let string: Option<String<64>> = string_block.load()?;
81    assert!(string.is_none());
82    let config: Option<SensorConfig> = config_block.load()?;
83    assert!(config.is_none());
84
85    info!("Flash Storage Example Complete!");
86    loop {
87        embassy_time::Timer::after_secs(1).await;
88    }
89}
Source

pub fn save<T>(&mut self, value: &T) -> Result<()>
where T: Serialize + for<'de> Deserialize<'de>,

Save data to this block.

See FlashArray for usage examples.

Examples found in repository?
examples/flash.rs (line 50)
40async fn inner_main(_spawner: Spawner) -> Result<()> {
41    info!("Flash Storage Example");
42
43    // Initialize hardware
44    let p = embassy_rp::init(Default::default());
45
46    // Initialize Flash device
47    let [_, _, _, mut string_block, mut config_block] = FlashArray::<5>::new(p.FLASH)?;
48
49    info!("Part 1: Storing data to flash");
50    string_block.save(&String::<64>::try_from("Hello, Flash Storage!")?)?;
51    config_block.save(&SensorConfig {
52        name: String::<32>::try_from("Temperature")?,
53        sample_rate_hz: 1000,
54        enabled: true,
55    })?;
56
57    info!("Part 2: Reading data from flash");
58    let string: Option<String<64>> = string_block.load()?;
59    assert!(string.as_deref() == Some("Hello, Flash Storage!"));
60    let config: Option<SensorConfig> = config_block.load()?;
61    assert!(
62        config
63            == Some(SensorConfig {
64                name: String::<32>::try_from("Temperature")?,
65                sample_rate_hz: 1000,
66                enabled: true,
67            })
68    );
69
70    info!("Part 3: Reading a different type counts as empty");
71    // Try to read the string block as a SensorConfig
72    let wrong_type_result: Option<SensorConfig> = string_block.load()?;
73    assert!(wrong_type_result.is_none());
74
75    info!("Part 4: Clearing flash blocks");
76    string_block.clear()?;
77    config_block.clear()?;
78
79    info!("Part 5: Verifying cleared blocks");
80    let string: Option<String<64>> = string_block.load()?;
81    assert!(string.is_none());
82    let config: Option<SensorConfig> = config_block.load()?;
83    assert!(config.is_none());
84
85    info!("Flash Storage Example Complete!");
86    loop {
87        embassy_time::Timer::after_secs(1).await;
88    }
89}
Source

pub fn clear(&mut self) -> Result<()>

Clear this block.

Examples found in repository?
examples/flash.rs (line 76)
40async fn inner_main(_spawner: Spawner) -> Result<()> {
41    info!("Flash Storage Example");
42
43    // Initialize hardware
44    let p = embassy_rp::init(Default::default());
45
46    // Initialize Flash device
47    let [_, _, _, mut string_block, mut config_block] = FlashArray::<5>::new(p.FLASH)?;
48
49    info!("Part 1: Storing data to flash");
50    string_block.save(&String::<64>::try_from("Hello, Flash Storage!")?)?;
51    config_block.save(&SensorConfig {
52        name: String::<32>::try_from("Temperature")?,
53        sample_rate_hz: 1000,
54        enabled: true,
55    })?;
56
57    info!("Part 2: Reading data from flash");
58    let string: Option<String<64>> = string_block.load()?;
59    assert!(string.as_deref() == Some("Hello, Flash Storage!"));
60    let config: Option<SensorConfig> = config_block.load()?;
61    assert!(
62        config
63            == Some(SensorConfig {
64                name: String::<32>::try_from("Temperature")?,
65                sample_rate_hz: 1000,
66                enabled: true,
67            })
68    );
69
70    info!("Part 3: Reading a different type counts as empty");
71    // Try to read the string block as a SensorConfig
72    let wrong_type_result: Option<SensorConfig> = string_block.load()?;
73    assert!(wrong_type_result.is_none());
74
75    info!("Part 4: Clearing flash blocks");
76    string_block.clear()?;
77    config_block.clear()?;
78
79    info!("Part 5: Verifying cleared blocks");
80    let string: Option<String<64>> = string_block.load()?;
81    assert!(string.is_none());
82    let config: Option<SensorConfig> = config_block.load()?;
83    assert!(config.is_none());
84
85    info!("Flash Storage Example Complete!");
86    loop {
87        embassy_time::Timer::after_secs(1).await;
88    }
89}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

Source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
Source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

Source§

fn lossy_into(self) -> Dst

Performs the conversion.
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> StrictAs for T

Source§

fn strict_as<Dst>(self) -> Dst
where T: StrictCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> StrictCastFrom<Src> for Dst
where Src: StrictCast<Dst>,

Source§

fn strict_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.