Skip to main content

EspKeyValueStorage

Struct EspKeyValueStorage 

Source
pub struct EspKeyValueStorage<T: NvsPartitionId>(/* private fields */);
Expand description

A specialized key-value storage wrapper around EspNvs that provides a simplified interface for storing and retrieving arbitrary data as byte (u8) slices.

EspKeyValueStorage provides an interface for:

  • Storing any data that can be represented as &[u8]
  • Automatic optimization: values ≤7 bytes stored as ESP-NVS u64 values, larger values as ESP-NVS blobs
  • Consistent contains() method that works correctly with this storage strategy
  • Full compatibility with Rust serde implementations (postcard, json, etc.) in that these can naturally do serde over byte slices

§Usage

use esp_idf_svc::nvs::{EspDefaultNvsPartition, EspKeyValueStorage};

let partition = EspDefaultNvsPartition::take()?;
let storage = EspKeyValueStorage::new(partition, "my_namespace", true)?;

// Store data as bytes
let data = b"hello world";
storage.set_raw("my_key", data)?;

// Check if key exists (this works correctly, unlike the original EspNvs bug)
assert!(storage.contains("my_key")?);

// Retrieve data
let mut buffer = [0u8; 64];
if let Some(retrieved) = storage.get_raw("my_key", &mut buffer)? {
    assert_eq!(retrieved, data);
}

Implementations§

Source§

impl<T: NvsPartitionId> EspKeyValueStorage<T>

Source

pub const fn new(nvs: EspNvs<T>) -> Self

Source

pub fn contains(&self, name: &str) -> Result<bool, EspError>

Source

pub fn remove(&self, name: &str) -> Result<bool, EspError>

Source

pub fn get_raw<'a>( &self, name: &str, buf: &'a mut [u8], ) -> Result<Option<&'a [u8]>, EspError>

Source

pub fn set_raw(&self, name: &str, buf: &[u8]) -> Result<bool, EspError>

Trait Implementations§

Source§

impl<T: NvsPartitionId> RawStorage for EspKeyValueStorage<T>

Source§

fn len(&self, name: &str) -> Result<Option<usize>, Self::Error>

Source§

fn get_raw<'a>( &self, name: &str, buf: &'a mut [u8], ) -> Result<Option<&'a [u8]>, Self::Error>

Source§

fn set_raw(&mut self, name: &str, buf: &[u8]) -> Result<bool, Self::Error>

Source§

impl<T: NvsPartitionId> StorageBase for EspKeyValueStorage<T>

Source§

type Error = EspError

Source§

fn contains(&self, name: &str) -> Result<bool, Self::Error>

Source§

fn remove(&mut self, name: &str) -> Result<bool, Self::Error>

Auto Trait Implementations§

§

impl<T> Freeze for EspKeyValueStorage<T>
where EspNvs<T>: Freeze,

§

impl<T> RefUnwindSafe for EspKeyValueStorage<T>
where EspNvs<T>: RefUnwindSafe,

§

impl<T> Send for EspKeyValueStorage<T>
where EspNvs<T>: Send,

§

impl<T> Sync for EspKeyValueStorage<T>
where EspNvs<T>: Sync,

§

impl<T> Unpin for EspKeyValueStorage<T>
where EspNvs<T>: Unpin,

§

impl<T> UnsafeUnpin for EspKeyValueStorage<T>
where EspNvs<T>: UnsafeUnpin,

§

impl<T> UnwindSafe for EspKeyValueStorage<T>
where EspNvs<T>: UnwindSafe,

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.