Skip to content

Commit 9912929

Browse files
authored
Merge pull request #59 from tweedegolf/keys-by-ref
Take keys by reference
2 parents aea3565 + aefb7d3 commit 9912929

File tree

10 files changed

+110
-124
lines changed

10 files changed

+110
-124
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@ jobs:
3434
steps:
3535
- uses: actions/checkout@v3
3636
- uses: dtolnay/rust-toolchain@stable
37+
- uses: cargo-bins/cargo-binstall@main
3738

3839
- name: Install cargo fuzz
39-
uses: taiki-e/install-action@v2
40-
with:
41-
tool: cargo-fuzz
40+
run: cargo binstall cargo-fuzz --no-confirm --target x86_64-unknown-linux-gnu
4241

4342
- name: Smoke-test fuzz targets
4443
run: |

CHANGELOG.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@
44

55
## Unreleased
66

7+
## 3.0.0 17-07-24
8+
9+
- *Breaking:* Map keys are now always passed by reference. This avoids extra cloning and memory use for bigger keys.
710
- Added `space_left` function for queue
811
- Added a new `map::remove_all_items()` API to remove all stored items in flash.
912

10-
# 2.0.2 07-05-24
13+
## 2.0.2 07-05-24
1114

1215
- Added check for too big items that won't ever fit in flash so it returns a good clear error.
1316

14-
# 2.0.1 06-05-24
17+
## 2.0.1 06-05-24
1518

1619
- Implemented the `get_len` function for all built-in key types
1720

18-
# 2.0.0 06-05-24
21+
## 2.0.0 06-05-24
1922

2023
- *Breaking:* Made the cache API a bit more strict. Caches now always have to be passed as a mutable reference.
2124
The API before would lead to a lot of extra unncesessary binary size.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sequential-storage"
3-
version = "2.0.2"
3+
version = "3.0.0"
44
edition = "2021"
55
license = "MIT OR Apache-2.0"
66
description = "A crate for storing data in flash with minimal erase cycles."

example/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ async fn run_map<E: defmt::Format>(
9696
flash_range.clone(),
9797
&mut NoCache::new(),
9898
&mut data_buffer,
99-
0u8,
99+
&0u8,
100100
&0u8,
101101
)
102102
.await
@@ -108,7 +108,7 @@ async fn run_map<E: defmt::Format>(
108108
flash_range.clone(),
109109
&mut NoCache::new(),
110110
&mut data_buffer,
111-
1u8,
111+
&1u8,
112112
&123u32,
113113
)
114114
.await
@@ -120,7 +120,7 @@ async fn run_map<E: defmt::Format>(
120120
flash_range.clone(),
121121
&mut NoCache::new(),
122122
&mut data_buffer,
123-
2u8,
123+
&2u8,
124124
&0.123f32,
125125
)
126126
.await
@@ -132,7 +132,7 @@ async fn run_map<E: defmt::Format>(
132132
flash_range.clone(),
133133
&mut NoCache::new(),
134134
&mut data_buffer,
135-
3,
135+
&3,
136136
)
137137
.await
138138
);
@@ -145,7 +145,7 @@ async fn run_map<E: defmt::Format>(
145145
flash_range.clone(),
146146
&mut NoCache::new(),
147147
&mut data_buffer,
148-
1,
148+
&1,
149149
)
150150
.await
151151
);

fuzz/fuzz_targets/map.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ fn fuzz(ops: Input, mut cache: impl KeyCacheImpl<u8> + Debug) {
105105
FLASH_RANGE,
106106
&mut cache,
107107
&mut buf.0,
108-
key,
108+
&key,
109109
&value.as_slice(),
110110
)) {
111111
Ok(_) => {
@@ -121,7 +121,7 @@ fn fuzz(ops: Input, mut cache: impl KeyCacheImpl<u8> + Debug) {
121121
FLASH_RANGE,
122122
&mut cache,
123123
&mut buf.0,
124-
key,
124+
&key,
125125
)) {
126126
Ok(Some(check_item)) if check_item == value => {
127127
#[cfg(fuzzing_repro)]
@@ -152,7 +152,7 @@ fn fuzz(ops: Input, mut cache: impl KeyCacheImpl<u8> + Debug) {
152152
FLASH_RANGE,
153153
&mut cache,
154154
&mut buf.0,
155-
key,
155+
&key,
156156
)) {
157157
Ok(Some(fetch_result)) => {
158158
let map_value = map
@@ -186,7 +186,7 @@ fn fuzz(ops: Input, mut cache: impl KeyCacheImpl<u8> + Debug) {
186186
FLASH_RANGE,
187187
&mut cache,
188188
&mut buf.0,
189-
key,
189+
&key,
190190
)) {
191191
Ok(()) => {
192192
map.remove(&key);
@@ -201,7 +201,7 @@ fn fuzz(ops: Input, mut cache: impl KeyCacheImpl<u8> + Debug) {
201201
FLASH_RANGE,
202202
&mut cache,
203203
&mut buf.0,
204-
key,
204+
&key,
205205
)) {
206206
Ok(Some(_)) => {
207207
#[cfg(fuzzing_repro)]
@@ -247,7 +247,7 @@ fn fuzz(ops: Input, mut cache: impl KeyCacheImpl<u8> + Debug) {
247247
FLASH_RANGE,
248248
&mut cache,
249249
&mut buf.0,
250-
key,
250+
&key,
251251
)) {
252252
Ok(Some(_)) => {
253253
#[cfg(fuzzing_repro)]

src/cache/key_pointers.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use core::{fmt::Debug, num::NonZeroU32};
22

3-
pub(crate) trait KeyPointersCache<KEY: Eq> {
3+
use crate::map::Key;
4+
5+
pub(crate) trait KeyPointersCache<KEY: Key> {
46
fn key_location(&self, key: &KEY) -> Option<u32>;
57

6-
fn notice_key_location(&mut self, key: KEY, item_address: u32);
8+
fn notice_key_location(&mut self, key: &KEY, item_address: u32);
79
fn notice_key_erased(&mut self, key: &KEY);
810

911
fn invalidate_cache_state(&mut self);
@@ -45,20 +47,20 @@ impl<KEY: Eq, const KEYS: usize> CachedKeyPointers<KEY, KEYS> {
4547
}
4648
}
4749

48-
impl<KEY: Eq, const KEYS: usize> KeyPointersCache<KEY> for CachedKeyPointers<KEY, KEYS> {
50+
impl<KEY: Key, const KEYS: usize> KeyPointersCache<KEY> for CachedKeyPointers<KEY, KEYS> {
4951
fn key_location(&self, key: &KEY) -> Option<u32> {
5052
self.key_index(key)
5153
.map(|index| self.key_pointers[index].as_ref().unwrap().1.get())
5254
}
5355

54-
fn notice_key_location(&mut self, key: KEY, item_address: u32) {
55-
match self.key_index(&key) {
56+
fn notice_key_location(&mut self, key: &KEY, item_address: u32) {
57+
match self.key_index(key) {
5658
Some(existing_index) => {
5759
self.key_pointers[existing_index] =
58-
Some((key, NonZeroU32::new(item_address).unwrap()));
60+
Some((key.clone(), NonZeroU32::new(item_address).unwrap()));
5961
move_to_front(&mut self.key_pointers, existing_index);
6062
}
61-
None => self.insert_front((key, NonZeroU32::new(item_address).unwrap())),
63+
None => self.insert_front((key.clone(), NonZeroU32::new(item_address).unwrap())),
6264
}
6365
}
6466

@@ -77,12 +79,12 @@ impl<KEY: Eq, const KEYS: usize> KeyPointersCache<KEY> for CachedKeyPointers<KEY
7779
#[derive(Debug)]
7880
pub(crate) struct UncachedKeyPointers;
7981

80-
impl<KEY: Eq> KeyPointersCache<KEY> for UncachedKeyPointers {
82+
impl<KEY: Key> KeyPointersCache<KEY> for UncachedKeyPointers {
8183
fn key_location(&self, _key: &KEY) -> Option<u32> {
8284
None
8385
}
8486

85-
fn notice_key_location(&mut self, _key: KEY, _item_address: u32) {}
87+
fn notice_key_location(&mut self, _key: &KEY, _item_address: u32) {}
8688

8789
fn notice_key_erased(&mut self, _key: &KEY) {}
8890

src/cache/mod.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::{fmt::Debug, ops::Range};
44

55
use embedded_storage_async::nor_flash::NorFlash;
66

7-
use crate::{item::ItemHeader, PageState};
7+
use crate::{item::ItemHeader, map::Key, PageState};
88

99
use self::{
1010
key_pointers::{CachedKeyPointers, KeyPointersCache, UncachedKeyPointers},
@@ -26,7 +26,7 @@ pub trait CacheImpl: PrivateCacheImpl {}
2626

2727
/// Trait implemented by all cache types that know about keys
2828
#[allow(private_bounds)]
29-
pub trait KeyCacheImpl<KEY: Eq>: CacheImpl + PrivateKeyCacheImpl<KEY> {}
29+
pub trait KeyCacheImpl<KEY: Key>: CacheImpl + PrivateKeyCacheImpl<KEY> {}
3030

3131
pub(crate) trait Invalidate {
3232
fn invalidate_cache_state(&mut self);
@@ -133,7 +133,7 @@ impl<T: PrivateCacheImpl> PrivateCacheImpl for &mut T {
133133
}
134134
}
135135

136-
pub(crate) trait PrivateKeyCacheImpl<KEY: Eq>: PrivateCacheImpl {
136+
pub(crate) trait PrivateKeyCacheImpl<KEY: Key>: PrivateCacheImpl {
137137
type KPC: KeyPointersCache<KEY>;
138138

139139
fn key_pointers(&mut self) -> &mut Self::KPC;
@@ -142,7 +142,7 @@ pub(crate) trait PrivateKeyCacheImpl<KEY: Eq>: PrivateCacheImpl {
142142
self.key_pointers().key_location(key)
143143
}
144144

145-
fn notice_key_location(&mut self, key: KEY, item_address: u32, dirty: bool) {
145+
fn notice_key_location(&mut self, key: &KEY, item_address: u32, dirty: bool) {
146146
if dirty {
147147
self.mark_dirty();
148148
}
@@ -155,7 +155,7 @@ pub(crate) trait PrivateKeyCacheImpl<KEY: Eq>: PrivateCacheImpl {
155155
}
156156
}
157157

158-
impl<KEY: Eq, T: PrivateKeyCacheImpl<KEY>> PrivateKeyCacheImpl<KEY> for &mut T {
158+
impl<KEY: Key, T: PrivateKeyCacheImpl<KEY>> PrivateKeyCacheImpl<KEY> for &mut T {
159159
type KPC = T::KPC;
160160

161161
fn key_pointers(&mut self) -> &mut Self::KPC {
@@ -240,13 +240,13 @@ impl PrivateCacheImpl for NoCache {
240240
}
241241

242242
impl CacheImpl for NoCache {}
243-
impl<KEY: Eq> KeyCacheImpl<KEY> for NoCache {}
243+
impl<KEY: Key> KeyCacheImpl<KEY> for NoCache {}
244244

245245
impl Invalidate for NoCache {
246246
fn invalidate_cache_state(&mut self) {}
247247
}
248248

249-
impl<KEY: Eq> PrivateKeyCacheImpl<KEY> for NoCache {
249+
impl<KEY: Key> PrivateKeyCacheImpl<KEY> for NoCache {
250250
type KPC = UncachedKeyPointers;
251251

252252
fn key_pointers(&mut self) -> &mut Self::KPC {
@@ -309,7 +309,7 @@ impl<const PAGE_COUNT: usize> PrivateCacheImpl for PageStateCache<PAGE_COUNT> {
309309
}
310310

311311
impl<const PAGE_COUNT: usize> CacheImpl for PageStateCache<PAGE_COUNT> {}
312-
impl<KEY: Eq, const PAGE_COUNT: usize> KeyCacheImpl<KEY> for PageStateCache<PAGE_COUNT> {}
312+
impl<KEY: Key, const PAGE_COUNT: usize> KeyCacheImpl<KEY> for PageStateCache<PAGE_COUNT> {}
313313

314314
impl<const PAGE_COUNT: usize> Invalidate for PageStateCache<PAGE_COUNT> {
315315
fn invalidate_cache_state(&mut self) {
@@ -319,7 +319,7 @@ impl<const PAGE_COUNT: usize> Invalidate for PageStateCache<PAGE_COUNT> {
319319
}
320320
}
321321

322-
impl<KEY: Eq, const PAGE_COUNT: usize> PrivateKeyCacheImpl<KEY> for PageStateCache<PAGE_COUNT> {
322+
impl<KEY: Key, const PAGE_COUNT: usize> PrivateKeyCacheImpl<KEY> for PageStateCache<PAGE_COUNT> {
323323
type KPC = UncachedKeyPointers;
324324

325325
fn key_pointers(&mut self) -> &mut Self::KPC {
@@ -382,7 +382,7 @@ impl<const PAGE_COUNT: usize> PrivateCacheImpl for PagePointerCache<PAGE_COUNT>
382382
}
383383

384384
impl<const PAGE_COUNT: usize> CacheImpl for PagePointerCache<PAGE_COUNT> {}
385-
impl<KEY: Eq, const PAGE_COUNT: usize> KeyCacheImpl<KEY> for PagePointerCache<PAGE_COUNT> {}
385+
impl<KEY: Key, const PAGE_COUNT: usize> KeyCacheImpl<KEY> for PagePointerCache<PAGE_COUNT> {}
386386

387387
impl<const PAGE_COUNT: usize> Invalidate for PagePointerCache<PAGE_COUNT> {
388388
fn invalidate_cache_state(&mut self) {
@@ -392,7 +392,7 @@ impl<const PAGE_COUNT: usize> Invalidate for PagePointerCache<PAGE_COUNT> {
392392
}
393393
}
394394

395-
impl<KEY: Eq, const PAGE_COUNT: usize> PrivateKeyCacheImpl<KEY> for PagePointerCache<PAGE_COUNT> {
395+
impl<KEY: Key, const PAGE_COUNT: usize> PrivateKeyCacheImpl<KEY> for PagePointerCache<PAGE_COUNT> {
396396
type KPC = UncachedKeyPointers;
397397

398398
fn key_pointers(&mut self) -> &mut Self::KPC {
@@ -417,14 +417,14 @@ impl<KEY: Eq, const PAGE_COUNT: usize> PrivateKeyCacheImpl<KEY> for PagePointerC
417417
/// the chance of a cache hit.
418418
/// The keys are cached in a fifo and any time its location is updated in cache it's added to the front.
419419
#[derive(Debug)]
420-
pub struct KeyPointerCache<const PAGE_COUNT: usize, KEY: Eq, const KEYS: usize> {
420+
pub struct KeyPointerCache<const PAGE_COUNT: usize, KEY: Key, const KEYS: usize> {
421421
dirt_tracker: DirtTracker,
422422
page_states: CachedPageStates<PAGE_COUNT>,
423423
page_pointers: CachedPagePointers<PAGE_COUNT>,
424424
key_pointers: CachedKeyPointers<KEY, KEYS>,
425425
}
426426

427-
impl<const PAGE_COUNT: usize, KEY: Eq, const KEYS: usize> KeyPointerCache<PAGE_COUNT, KEY, KEYS> {
427+
impl<const PAGE_COUNT: usize, KEY: Key, const KEYS: usize> KeyPointerCache<PAGE_COUNT, KEY, KEYS> {
428428
/// Construct a new instance
429429
pub const fn new() -> Self {
430430
Self {
@@ -436,15 +436,15 @@ impl<const PAGE_COUNT: usize, KEY: Eq, const KEYS: usize> KeyPointerCache<PAGE_C
436436
}
437437
}
438438

439-
impl<const PAGE_COUNT: usize, KEY: Eq, const KEYS: usize> Default
439+
impl<const PAGE_COUNT: usize, KEY: Key, const KEYS: usize> Default
440440
for KeyPointerCache<PAGE_COUNT, KEY, KEYS>
441441
{
442442
fn default() -> Self {
443443
Self::new()
444444
}
445445
}
446446

447-
impl<const PAGE_COUNT: usize, KEY: Eq, const KEYS: usize> PrivateCacheImpl
447+
impl<const PAGE_COUNT: usize, KEY: Key, const KEYS: usize> PrivateCacheImpl
448448
for KeyPointerCache<PAGE_COUNT, KEY, KEYS>
449449
{
450450
type PSC = CachedPageStates<PAGE_COUNT>;
@@ -463,16 +463,16 @@ impl<const PAGE_COUNT: usize, KEY: Eq, const KEYS: usize> PrivateCacheImpl
463463
}
464464
}
465465

466-
impl<const PAGE_COUNT: usize, KEY: Eq, const KEYS: usize> CacheImpl
466+
impl<const PAGE_COUNT: usize, KEY: Key, const KEYS: usize> CacheImpl
467467
for KeyPointerCache<PAGE_COUNT, KEY, KEYS>
468468
{
469469
}
470-
impl<const PAGE_COUNT: usize, KEY: Eq, const KEYS: usize> KeyCacheImpl<KEY>
470+
impl<const PAGE_COUNT: usize, KEY: Key, const KEYS: usize> KeyCacheImpl<KEY>
471471
for KeyPointerCache<PAGE_COUNT, KEY, KEYS>
472472
{
473473
}
474474

475-
impl<const PAGE_COUNT: usize, KEY: Eq, const KEYS: usize> Invalidate
475+
impl<const PAGE_COUNT: usize, KEY: Key, const KEYS: usize> Invalidate
476476
for KeyPointerCache<PAGE_COUNT, KEY, KEYS>
477477
{
478478
fn invalidate_cache_state(&mut self) {
@@ -483,7 +483,7 @@ impl<const PAGE_COUNT: usize, KEY: Eq, const KEYS: usize> Invalidate
483483
}
484484
}
485485

486-
impl<const PAGE_COUNT: usize, KEY: Eq, const KEYS: usize> PrivateKeyCacheImpl<KEY>
486+
impl<const PAGE_COUNT: usize, KEY: Key, const KEYS: usize> PrivateKeyCacheImpl<KEY>
487487
for KeyPointerCache<PAGE_COUNT, KEY, KEYS>
488488
{
489489
type KPC = CachedKeyPointers<KEY, KEYS>;

src/cache/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ mod map_tests {
213213
FLASH_RANGE,
214214
cache,
215215
&mut data_buffer,
216-
i as u16,
216+
&(i as u16),
217217
&vec![i as u8; LENGHT_PER_KEY[i]].as_slice(),
218218
)
219219
.await
@@ -231,7 +231,7 @@ mod map_tests {
231231
FLASH_RANGE,
232232
cache,
233233
&mut data_buffer,
234-
i as u16,
234+
&(i as u16),
235235
)
236236
.await
237237
.unwrap()

0 commit comments

Comments
 (0)