Skip to main content

FlashBlockEsp

Struct FlashBlockEsp 

Source
pub struct FlashBlockEsp { /* private fields */ }
Expand description

A device abstraction for type-safe persistent storage in flash memory.

FlashBlockEsp provides a generic flash-block storage system for ESP, allowing you to store any serde-compatible type in the device’s internal flash.

Use FlashBlockEsp::new_array to allocate one or more blocks. Block operations like load, save, and clear are provided by FlashBlock, so bring the trait into scope:

use device_envoy_esp::flash_block::FlashBlock as _;

§Features

  • Type safety: Hash-based type checking prevents reading data written under a different Rust type name. The hash is derived from the full type path (for example, app1::BootCounter). Trying to read a different type returns Ok(None). Structural changes (adding or removing fields) do not change the hash, but may cause deserialization to fail and return an error.
  • Postcard serialization: A compact, no_std-friendly binary format.

§Block allocation

Conceptually, flash is treated as an array of fixed-size erase blocks counted from the end of the configured region backward. Your code can split that array using destructuring assignment and hand individual blocks to subsystems that need persistent storage.

⚠️ Warning: ESP firmware and user data share the same flash device. Allocating too many blocks can overwrite your firmware.

§Example

use device_envoy_esp::{Result, init_and_start, flash_block::{FlashBlockEsp, FlashBlock as _}};

#[derive(serde::Serialize, serde::Deserialize, Clone)]
struct WifiPersistedState {
    ssid: heapless::String<32>,
    password: heapless::String<64>,
    timezone_offset_minutes: i32,
}

init_and_start!(p);
let [mut wifi_persisted_state_flash_block, mut fields_flash_block] =
    FlashBlockEsp::new_array::<2>(p.FLASH)?;

let wifi_persisted_state = wifi_persisted_state_flash_block.load::<WifiPersistedState>()?;
if wifi_persisted_state.is_none() {
    let wifi_persisted_state = WifiPersistedState {
        ssid: heapless::String::new(),
        password: heapless::String::new(),
        timezone_offset_minutes: 0,
    };
    wifi_persisted_state_flash_block.save(&wifi_persisted_state)?;
}

fields_flash_block.clear()?;

Implementations§

Source§

impl FlashBlockEsp

Source

pub fn new_array<const N: usize>( flash: FLASH<'static>, ) -> Result<[FlashBlockEsp; N]>

Reserve N blocks in the default tail region.

Trait Implementations§

Source§

impl Clone for FlashBlockEsp

Source§

fn clone(&self) -> FlashBlockEsp

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl FlashBlock for FlashBlockEsp

Available on target_os=none only.
Source§

type Error = Error

Error returned by block operations.
Source§

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

Load a typed value from this block. Read more
Source§

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

Save a typed value to this block. Read more
Source§

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

Clear this block. Read more
Source§

impl Copy for FlashBlockEsp

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.