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
u64values, 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);
}