diff --git a/examples/storage/src/main.rs b/examples/storage/src/main.rs index ae39ab6d4..4194b5ccb 100644 --- a/examples/storage/src/main.rs +++ b/examples/storage/src/main.rs @@ -131,6 +131,15 @@ async fn main() { } info!(""); + info!("Accessing an object by composite key"); + let key1: usize = storage::get(("key", 1)).await.unwrap(); + info!("Original element with key (\"key\", 1) was: {:?}", key1); + if key1 != 42 { + info!("Storing 42 there."); + storage::insert(("key", 1), 42usize).await.unwrap(); + } + info!(""); + info!("Exit storage example"); exit(ExitCode::SUCCESS); diff --git a/src/ariel-os-storage/src/storage.rs b/src/ariel-os-storage/src/storage.rs index 93200df16..986c84e35 100644 --- a/src/ariel-os-storage/src/storage.rs +++ b/src/ariel-os-storage/src/storage.rs @@ -186,6 +186,20 @@ impl SealedKey for &str { } } +impl super::Key for (&str, usize) {} +impl SealedKey for (&str, usize) { + fn key(&self) -> CborKey { + // At some point we could start impl'ing it for anything that is minicbor encodable, or + // stay selective (and find a good way to express that they all go through minicbor + // anyway without duplicating code verbatim). + let mut vec = heapless::Vec::new(); + let mut encoder = + minicbor::encode::Encoder::new(minicbor_adapters::WriteToHeapless(&mut vec)); + encoder.encode(self).unwrap(); + CborKey(vec) + } +} + /// An owned buffer for key storage. /// /// It is a panic-worthy invariant of this type that the data in the inner vector are CBOR encoded