Skip to content

deps: use heapless::Vec for fixed-capacity Vec #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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