Skip to content

Commit 68cc4d1

Browse files
authored
Merge pull request #499 from Icelk/borrow
Impl Borrow and BorrowMut for String and Vec.
2 parents a9ed238 + 103ae85 commit 68cc4d1

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3333
- Added `DequeView`, the `!Sized` version of `Deque`.
3434
- Added `QueueView`, the `!Sized` version of `Queue`.
3535
- Added `SortedLinkedListView`, the `!Sized` version of `SortedLinkedList`.
36+
- Added implementation of `Borrow` and `BorrowMut` for `String` and `Vec`.
3637

3738
### Changed
3839

src/linear_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ where
7171
/// assert_eq!(map.capacity(), 8);
7272
/// ```
7373
pub fn capacity(&self) -> usize {
74-
self.buffer.borrow().capacity()
74+
self.buffer.capacity()
7575
}
7676

7777
/// Clears the map, removing all key-value pairs.

src/string/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! A fixed capacity [`String`](https://doc.rust-lang.org/std/string/struct.String.html).
22
33
use core::{
4+
borrow,
45
char::DecodeUtf16Error,
56
cmp::Ordering,
67
fmt,
@@ -732,6 +733,17 @@ impl<S: Storage> ops::DerefMut for StringInner<S> {
732733
}
733734
}
734735

736+
impl<S: Storage> borrow::Borrow<str> for StringInner<S> {
737+
fn borrow(&self) -> &str {
738+
self.as_str()
739+
}
740+
}
741+
impl<S: Storage> borrow::BorrowMut<str> for StringInner<S> {
742+
fn borrow_mut(&mut self) -> &mut str {
743+
self.as_mut_str()
744+
}
745+
}
746+
735747
impl<S: Storage> AsRef<str> for StringInner<S> {
736748
#[inline]
737749
fn as_ref(&self) -> &str {

src/vec/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! A fixed capacity [`Vec`](https://doc.rust-lang.org/std/vec/struct.Vec.html).
22
3+
use core::borrow;
34
use core::{
45
borrow::{Borrow, BorrowMut},
56
cmp::Ordering,
@@ -1410,6 +1411,17 @@ impl<T, S: Storage> ops::DerefMut for VecInner<T, S> {
14101411
}
14111412
}
14121413

1414+
impl<T, S: Storage> borrow::Borrow<[T]> for VecInner<T, S> {
1415+
fn borrow(&self) -> &[T] {
1416+
self.as_slice()
1417+
}
1418+
}
1419+
impl<T, S: Storage> borrow::BorrowMut<[T]> for VecInner<T, S> {
1420+
fn borrow_mut(&mut self) -> &mut [T] {
1421+
self.as_mut_slice()
1422+
}
1423+
}
1424+
14131425
impl<T, S: Storage> AsRef<VecInner<T, S>> for VecInner<T, S> {
14141426
#[inline]
14151427
fn as_ref(&self) -> &Self {

0 commit comments

Comments
 (0)