Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl Borrow and BorrowMut for String and Vec. #499

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
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
Loading