Skip to content

Commit

Permalink
feat(storage): allow (str, number) keys
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Feb 17, 2025
1 parent 84674a0 commit 19fbd40
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/storage/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions src/ariel-os-storage/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 19fbd40

Please sign in to comment.