Skip to content

Commit

Permalink
Merge pull request #108 from wada314/master
Browse files Browse the repository at this point in the history
Add Borrow and BorrowMut impls for Vec and String
  • Loading branch information
fitzgen committed May 11, 2021
2 parents 9160907 + e9adce5 commit 674def4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/collections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
use crate::collections::str::lossy;
use crate::collections::vec::Vec;
use crate::Bump;
use core::borrow::{Borrow, BorrowMut};
use core::char::decode_utf16;
use core::fmt;
use core::hash;
Expand Down Expand Up @@ -2079,6 +2080,20 @@ impl<'bump> fmt::Write for String<'bump> {
}
}

impl<'bump> Borrow<str> for String<'bump> {
#[inline]
fn borrow(&self) -> &str {
&self[..]
}
}

impl<'bump> BorrowMut<str> for String<'bump> {
#[inline]
fn borrow_mut(&mut self) -> &mut str {
&mut self[..]
}
}

/// A draining iterator for `String`.
///
/// This struct is created by the [`drain`] method on [`String`]. See its
Expand Down
15 changes: 15 additions & 0 deletions src/collections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
use super::raw_vec::RawVec;
use crate::collections::CollectionAllocErr;
use crate::Bump;
use core::borrow::{Borrow, BorrowMut};
use core::cmp::Ordering;
use core::fmt;
use core::hash::{self, Hash};
Expand Down Expand Up @@ -2058,6 +2059,20 @@ impl<'bump, T: 'bump> From<Vec<'bump, T>> for crate::boxed::Box<'bump, [T]> {
}
}

impl<'bump, T: 'bump> Borrow<[T]> for Vec<'bump, T> {
#[inline]
fn borrow(&self) -> &[T] {
&self[..]
}
}

impl<'bump, T: 'bump> BorrowMut<[T]> for Vec<'bump, T> {
#[inline]
fn borrow_mut(&mut self) -> &mut [T] {
&mut self[..]
}
}

////////////////////////////////////////////////////////////////////////////////
// Clone-on-write
////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 674def4

Please sign in to comment.