Skip to content

Commit

Permalink
Impl Borrow and BorrowMut for String and Vec.
Browse files Browse the repository at this point in the history
This enables them to be used in maps, especially with &str get values in a Map<String, T>.
  • Loading branch information
Icelk committed Jul 2, 2024
1 parent a9ed238 commit 1e57078
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/linear_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ where
/// assert_eq!(map.capacity(), 8);
/// ```
pub fn capacity(&self) -> usize {
self.buffer.borrow().capacity()
self.buffer.capacity()
}

/// Clears the map, removing all key-value pairs.
Expand Down
12 changes: 12 additions & 0 deletions src/string/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! A fixed capacity [`String`](https://doc.rust-lang.org/std/string/struct.String.html).

use core::{
borrow,
char::DecodeUtf16Error,
cmp::Ordering,
fmt,
Expand Down Expand Up @@ -732,6 +733,17 @@ impl<S: Storage> ops::DerefMut for StringInner<S> {
}
}

impl<S: Storage> borrow::Borrow<str> for StringInner<S> {
fn borrow(&self) -> &str {
self.as_str()
}
}
impl<S: Storage> borrow::BorrowMut<str> for StringInner<S> {
fn borrow_mut(&mut self) -> &mut str {
self.as_mut_str()
}
}

impl<S: Storage> AsRef<str> for StringInner<S> {
#[inline]
fn as_ref(&self) -> &str {
Expand Down
12 changes: 12 additions & 0 deletions src/vec/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! A fixed capacity [`Vec`](https://doc.rust-lang.org/std/vec/struct.Vec.html).

use core::borrow;
use core::{
borrow::{Borrow, BorrowMut},
cmp::Ordering,
Expand Down Expand Up @@ -1410,6 +1411,17 @@ impl<T, S: Storage> ops::DerefMut for VecInner<T, S> {
}
}

impl<T, S: Storage> borrow::Borrow<[T]> for VecInner<T, S> {
fn borrow(&self) -> &[T] {
self.as_slice()
}
}
impl<T, S: Storage> borrow::BorrowMut<[T]> for VecInner<T, S> {
fn borrow_mut(&mut self) -> &mut [T] {
self.as_mut_slice()
}
}

impl<T, S: Storage> AsRef<VecInner<T, S>> for VecInner<T, S> {
#[inline]
fn as_ref(&self) -> &Self {
Expand Down

0 comments on commit 1e57078

Please sign in to comment.