Skip to content

Commit

Permalink
deps: use heapless::Vec for fixed-capacity Vec
Browse files Browse the repository at this point in the history
Switches to the actively maintained `heapless` crate for a
`no_std`-compatible, fixed-capacity `Vec` implementation.
  • Loading branch information
rmsyn committed Nov 12, 2023
1 parent 5775bf1 commit 3f31a55
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ readme = "README.md"
include = ["src/**/*", "LICENSE", "README.md"]

[dependencies]
arrayvec = { version = "0.7", default-features = false }
heapless = "0.8"
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//!
//! See the [`LRUCache`](LRUCache) docs for details.
use arrayvec::ArrayVec;
use heapless::Vec;
use core::{fmt, mem::replace};

#[cfg(test)]
Expand Down Expand Up @@ -59,7 +59,7 @@ pub struct LRUCache<T, const N: usize> {
/// The most-recently-used entry is at index `head`. The entries form a linked list, linked to
/// each other by indices within the `entries` array. After an entry is added to the array,
/// its index never changes, so these links are never invalidated.
entries: ArrayVec<Entry<T>, N>,
entries: Vec<Entry<T>, N>,
/// Index of the first entry. If the cache is empty, ignore this field.
head: u16,
/// Index of the last entry. If the cache is empty, ignore this field.
Expand All @@ -79,7 +79,7 @@ struct Entry<T> {
impl<T, const N: usize> Default for LRUCache<T, N> {
fn default() -> Self {
let cache = LRUCache {
entries: ArrayVec::new(),
entries: Vec::new(),
head: 0,
tail: 0,
};
Expand Down Expand Up @@ -109,7 +109,7 @@ impl<T, const N: usize> LRUCache<T, N> {
let previous_entry = replace(&mut self.entries[i as usize], entry);
(i, Some(previous_entry.val))
} else {
self.entries.push(entry);
self.entries.push(entry).ok();
(self.entries.len() as u16 - 1, None)
};

Expand Down

0 comments on commit 3f31a55

Please sign in to comment.