From 03d7eee1288ce88dfdcd38b73888d048d063699c Mon Sep 17 00:00:00 2001 From: Matt Brubeck Date: Sun, 7 Apr 2024 18:57:18 -0700 Subject: [PATCH] Add a const constructor --- src/lib.rs | 13 +++++++++---- src/tests.rs | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c457e62..5d5ad8c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,20 +78,25 @@ struct Entry { impl Default for LRUCache { fn default() -> Self { + Self::new() + } +} + +impl LRUCache { + /// Create an empty cache. + pub const fn new() -> Self { let cache = LRUCache { - entries: ArrayVec::new(), + entries: ArrayVec::new_const(), head: 0, tail: 0, }; assert!( - cache.entries.capacity() < u16::max_value() as usize, + N < u16::max_value() as usize, "Capacity overflow" ); cache } -} -impl LRUCache { /// Insert a given key in the cache. /// /// This item becomes the front (most-recently-used) item in the cache. If the cache is full, diff --git a/src/tests.rs b/src/tests.rs index 978e129..2db361d 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -20,7 +20,7 @@ where #[test] fn empty() { - let mut cache = TestCache::default(); + let mut cache = TestCache::new(); assert_eq!(cache.is_empty(), true); assert_eq!(items(&mut cache), []); cache.insert(1);