Skip to content

Commit

Permalink
Merge pull request #115 from Dr-Emann/explicit_borrow_str_slice
Browse files Browse the repository at this point in the history
Explicitly implement Borrow for String & Vec for non-nightly
  • Loading branch information
jeromefroe committed Sep 15, 2021
2 parents ffba115 + 075a23d commit 0bf27e9
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/lib.rs
Expand Up @@ -26,7 +26,7 @@
//!
//! ## Example
//!
//! ```rust,no_run
//! ```rust
//! extern crate lru;
//!
//! use lru::LruCache;
Expand Down Expand Up @@ -130,6 +130,20 @@ impl<K> Borrow<K> for KeyRef<K> {
}
}

#[cfg(not(feature = "nightly"))]
impl Borrow<str> for KeyRef<alloc::string::String> {
fn borrow(&self) -> &str {
unsafe { &*self.k }
}
}

#[cfg(not(feature = "nightly"))]
impl<T> Borrow<[T]> for KeyRef<alloc::vec::Vec<T>> {
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<K, V> {
Expand Down Expand Up @@ -1495,7 +1509,6 @@ mod tests {
}

#[test]
#[cfg(feature = "nightly")]
fn test_get_with_borrow() {
use alloc::string::String;

Expand All @@ -1508,7 +1521,6 @@ mod tests {
}

#[test]
#[cfg(feature = "nightly")]
fn test_get_mut_with_borrow() {
use alloc::string::String;

Expand Down

0 comments on commit 0bf27e9

Please sign in to comment.