From 075a23de6d525373f39b8ef3c7907464620e27bf Mon Sep 17 00:00:00 2001 From: Zachary Dremann Date: Mon, 13 Sep 2021 17:24:52 -0400 Subject: [PATCH] Explicitly implement Borrow for String & Vec for non-nightly While it isn't as fully featured as the nightly version, this allows using borrowed keys for `get` for what I believe are the most common cases: `Borrow` and `Borrow<[T]>`. I believe this is also backward compatible with eventually enabling `Borrow where K: Borrow`. --- src/lib.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7ea9b85..987fb6d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,7 +26,7 @@ //! //! ## Example //! -//! ```rust,no_run +//! ```rust //! extern crate lru; //! //! use lru::LruCache; @@ -130,6 +130,20 @@ impl Borrow for KeyRef { } } +#[cfg(not(feature = "nightly"))] +impl Borrow for KeyRef { + fn borrow(&self) -> &str { + unsafe { &*self.k } + } +} + +#[cfg(not(feature = "nightly"))] +impl Borrow<[T]> for KeyRef> { + fn borrow(&self) -> &[T] { + unsafe { &*self.k } + } +} + // Struct used to hold a key value pair. Also contains references to previous and next entries // so we can maintain the entries in a linked list ordered by their use. struct LruEntry { @@ -1495,7 +1509,6 @@ mod tests { } #[test] - #[cfg(feature = "nightly")] fn test_get_with_borrow() { use alloc::string::String; @@ -1508,7 +1521,6 @@ mod tests { } #[test] - #[cfg(feature = "nightly")] fn test_get_mut_with_borrow() { use alloc::string::String;