Skip to content
This repository was archived by the owner on Nov 27, 2020. It is now read-only.

Commit 465a2cf

Browse files
committed
Implement Clone for String
1 parent a7a17d0 commit 465a2cf

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

src/string.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ use core::{
8080
#[cfg(feature = "std")]
8181
use std::borrow::Cow;
8282

83-
use crate::alloc::handle_alloc_error;
83+
use crate::{alloc::handle_alloc_error, clone::CloneIn};
8484
pub use liballoc::string::{ParseError, ToString};
8585

8686
/// A UTF-8 encoded, growable string.
@@ -1991,8 +1991,13 @@ impl fmt::Display for FromUtf16Error {
19911991
}
19921992
}
19931993

1994-
impl Clone for String {
1995-
#[must_use]
1994+
impl<A> Clone for String<A>
1995+
where
1996+
A: AllocRef,
1997+
A::BuildAlloc: Clone,
1998+
{
1999+
#[inline]
2000+
#[must_use = "Cloning is expected to be expensive"]
19962001
fn clone(&self) -> Self {
19972002
Self {
19982003
vec: self.vec.clone(),
@@ -2004,6 +2009,26 @@ impl Clone for String {
20042009
}
20052010
}
20062011

2012+
#[allow(clippy::use_self)]
2013+
impl<A: AllocRef, B: AllocRef> CloneIn<B> for String<A> {
2014+
type Cloned = String<B>;
2015+
2016+
#[inline]
2017+
#[must_use = "Cloning is expected to be expensive"]
2018+
fn clone_in(&self, a: B) -> Self::Cloned {
2019+
String {
2020+
vec: self.vec.clone_in(a),
2021+
}
2022+
}
2023+
2024+
#[inline]
2025+
fn try_clone_in(&self, a: B) -> Result<Self::Cloned, B::Error> {
2026+
Ok(String {
2027+
vec: self.vec.try_clone_in(a)?,
2028+
})
2029+
}
2030+
}
2031+
20072032
impl FromIterator<char> for String {
20082033
fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> Self {
20092034
let mut buf = Self::new();

src/vec.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,7 @@
6969
//! [`vec!`]: ../macro.vec.html
7070
7171
use crate::{
72-
alloc::{
73-
handle_alloc_error,
74-
AllocRef,
75-
BuildAllocRef,
76-
DeallocRef,
77-
Global,
78-
NonZeroLayout,
79-
ReallocRef,
80-
},
72+
alloc::{handle_alloc_error, AllocRef, BuildAllocRef, DeallocRef, Global, ReallocRef},
8173
boxed::Box,
8274
clone::CloneIn,
8375
collections::CollectionAllocErr,

0 commit comments

Comments
 (0)