Impl Borrow and BorrowMut for String and Vec.

This enables them to be used in maps, especially with &str get values in a Map<String, T>.
This commit is contained in:
Icelk 2024-07-02 16:26:28 +02:00
parent a9ed238672
commit 103ae85bb5
4 changed files with 26 additions and 1 deletions

View File

@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added `DequeView`, the `!Sized` version of `Deque`.
- Added `QueueView`, the `!Sized` version of `Queue`.
- Added `SortedLinkedListView`, the `!Sized` version of `SortedLinkedList`.
- Added implementation of `Borrow` and `BorrowMut` for `String` and `Vec`.
### Changed

View File

@ -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.

View File

@ -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,
@ -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 {

View File

@ -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,
@ -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 {