From 8851a74dbb51ff75f2c30bb66b69cc77df11984c Mon Sep 17 00:00:00 2001 From: Dion Dokter Date: Tue, 26 Mar 2024 17:11:33 +0100 Subject: [PATCH] Prepare for doc fixing --- src/lib.rs | 2 +- src/map.rs | 58 ++++++++++++++++++++++++++++-------------------------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 86387dc..efa1ef1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ #![cfg_attr(not(any(test, doctest, feature = "std")), no_std)] -// #![deny(missing_docs)] +#![deny(missing_docs)] #![doc = include_str!("../README.md")] // Assumptions made in this crate: diff --git a/src/map.rs b/src/map.rs index b3c26fc..00506e1 100644 --- a/src/map.rs +++ b/src/map.rs @@ -80,23 +80,24 @@ use self::{ use super::*; -/// Get a storage item from the flash. -/// Only the last stored item of the given key is returned. -/// -/// If no value with the key is found, None is returned. -/// -/// The data buffer must be long enough to hold the longest serialized data of your [StorageItem] type, -/// rounded up to flash word alignment. -/// -/// *Note: On a given flash range, make sure to use only the same type as [StorageItem] every time -/// or types that serialize and deserialize the key in the same way.* -pub async fn fetch_item<'d, K: Key, I: MapItem<'d>, S: NorFlash>( +// TODO revise +// /// Get a storage item from the flash. +// /// Only the last stored item of the given key is returned. +// /// +// /// If no value with the key is found, None is returned. +// /// +// /// The data buffer must be long enough to hold the longest serialized data of your [StorageItem] type, +// /// rounded up to flash word alignment. +// /// +// /// *Note: On a given flash range, make sure to use only the same type as [StorageItem] every time +// /// or types that serialize and deserialize the key in the same way.* +pub async fn fetch_item<'d, K: Key, V: Value<'d>, S: NorFlash>( flash: &mut S, flash_range: Range, cache: &mut impl KeyCacheImpl, data_buffer: &'d mut [u8], search_key: K, -) -> Result, Error> { +) -> Result, Error> { let result = run_with_auto_repair!( function = { fetch_item_with_location( @@ -120,7 +121,7 @@ pub async fn fetch_item<'d, K: Key, I: MapItem<'d>, S: NorFlash>( let data_len = item.data().len(); Ok(Some( - I::deserialize_from(&item.destruct().1[K::LEN..][..data_len - K::LEN]) + V::deserialize_from(&item.destruct().1[K::LEN..][..data_len - K::LEN]) .map_err(Error::Item)?, )) } @@ -282,21 +283,22 @@ async fn fetch_item_with_location<'d, K: Key, S: NorFlash>( } } -/// Store an item into flash memory. -/// It will overwrite the last value that has the same key. -/// The flash needs to be at least 2 pages long. -/// -/// The data buffer must be long enough to hold the longest serialized data of your [StorageItem] type. -/// -/// *Note: On a given flash range, make sure to use only the same type as [StorageItem] every time -/// or types that serialize and deserialize the key in the same way.* -pub async fn store_item<'d, K: Key, I: MapItem<'d>, S: NorFlash>( +// TODO revise +// /// Store an item into flash memory. +// /// It will overwrite the last value that has the same key. +// /// The flash needs to be at least 2 pages long. +// /// +// /// The data buffer must be long enough to hold the longest serialized data of your [StorageItem] type. +// /// +// /// *Note: On a given flash range, make sure to use only the same type as [StorageItem] every time +// /// or types that serialize and deserialize the key in the same way.* +pub async fn store_item<'d, K: Key, V: Value<'d>, S: NorFlash>( flash: &mut S, flash_range: Range, cache: &mut impl KeyCacheImpl, data_buffer: &mut [u8], key: K, - item: &I, + item: &V, ) -> Result<(), Error> { run_with_auto_repair!( function = store_item_inner( @@ -318,7 +320,7 @@ async fn store_item_inner<'d, K: Key, S: NorFlash>( cache: &mut impl KeyCacheImpl, data_buffer: &mut [u8], key: K, - item: &dyn MapItem<'d>, + item: &dyn Value<'d>, ) -> Result<(), Error> { assert_eq!(flash_range.start % S::ERASE_SIZE as u32, 0); assert_eq!(flash_range.end % S::ERASE_SIZE as u32, 0); @@ -622,14 +624,14 @@ impl Key for [u8; N] { } } -pub trait MapItem<'a> { +pub trait Value<'a> { fn serialize_into(&self, buffer: &mut [u8]) -> Result; fn deserialize_from(buffer: &'a [u8]) -> Result where Self: Sized; } -impl<'a> MapItem<'a> for &'a [u8] { +impl<'a> Value<'a> for &'a [u8] { fn serialize_into(&self, buffer: &mut [u8]) -> Result { if buffer.len() < self.len() { return Err(MapItemError::BufferTooSmall); @@ -647,7 +649,7 @@ impl<'a> MapItem<'a> for &'a [u8] { } } -impl<'a, const N: usize> MapItem<'a> for [u8; N] { +impl<'a, const N: usize> Value<'a> for [u8; N] { fn serialize_into(&self, buffer: &mut [u8]) -> Result { if buffer.len() < self.len() { return Err(MapItemError::BufferTooSmall); @@ -667,7 +669,7 @@ impl<'a, const N: usize> MapItem<'a> for [u8; N] { macro_rules! impl_map_item_num { ($int:ty) => { - impl<'a> MapItem<'a> for $int { + impl<'a> Value<'a> for $int { fn serialize_into(&self, buffer: &mut [u8]) -> Result { buffer[..core::mem::size_of::()].copy_from_slice(&self.to_le_bytes()); Ok(core::mem::size_of::())