Skip to content

Commit 3d31363

Browse files
committed
Auto merge of #85176 - a1phyr:impl_clone_from, r=yaahc
Override `clone_from` for some types Override `clone_from` method of the `Clone` trait for: - `cell::RefCell` - `cmp::Reverse` - `io::Cursor` - `mem::ManuallyDrop` This can bring performance improvements.
2 parents 4e3e6db + 9332ac3 commit 3d31363

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

library/core/src/cell.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,15 @@ impl<T: Clone> Clone for RefCell<T> {
11231123
fn clone(&self) -> RefCell<T> {
11241124
RefCell::new(self.borrow().clone())
11251125
}
1126+
1127+
/// # Panics
1128+
///
1129+
/// Panics if `other` is currently mutably borrowed.
1130+
#[inline]
1131+
#[track_caller]
1132+
fn clone_from(&mut self, other: &Self) {
1133+
self.get_mut().clone_from(&other.borrow())
1134+
}
11261135
}
11271136

11281137
#[stable(feature = "rust1", since = "1.0.0")]

library/core/src/cmp.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ impl Ordering {
578578
/// v.sort_by_key(|&num| (num > 3, Reverse(num)));
579579
/// assert_eq!(v, vec![3, 2, 1, 6, 5, 4]);
580580
/// ```
581-
#[derive(PartialEq, Eq, Debug, Copy, Clone, Default, Hash)]
581+
#[derive(PartialEq, Eq, Debug, Copy, Default, Hash)]
582582
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
583583
#[repr(transparent)]
584584
pub struct Reverse<T>(#[stable(feature = "reverse_cmp_key", since = "1.19.0")] pub T);
@@ -616,6 +616,19 @@ impl<T: Ord> Ord for Reverse<T> {
616616
}
617617
}
618618

619+
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
620+
impl<T: Clone> Clone for Reverse<T> {
621+
#[inline]
622+
fn clone(&self) -> Reverse<T> {
623+
Reverse(self.0.clone())
624+
}
625+
626+
#[inline]
627+
fn clone_from(&mut self, other: &Self) {
628+
self.0.clone_from(&other.0)
629+
}
630+
}
631+
619632
/// Trait for types that form a [total order](https://en.wikipedia.org/wiki/Total_order).
620633
///
621634
/// An order is a total order if it is (for all `a`, `b` and `c`):

library/core/src/mem/manually_drop.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use crate::ptr;
4444
/// [`MaybeUninit<T>`]: crate::mem::MaybeUninit
4545
#[stable(feature = "manually_drop", since = "1.20.0")]
4646
#[lang = "manually_drop"]
47-
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
47+
#[derive(Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
4848
#[repr(transparent)]
4949
pub struct ManuallyDrop<T: ?Sized> {
5050
value: T,
@@ -160,3 +160,16 @@ impl<T: ?Sized> DerefMut for ManuallyDrop<T> {
160160
&mut self.value
161161
}
162162
}
163+
164+
#[stable(feature = "manually_drop", since = "1.20.0")]
165+
impl<T: Clone> Clone for ManuallyDrop<T> {
166+
#[inline]
167+
fn clone(&self) -> ManuallyDrop<T> {
168+
ManuallyDrop { value: self.value.clone() }
169+
}
170+
171+
#[inline]
172+
fn clone_from(&mut self, other: &Self) {
173+
self.value.clone_from(&other.value)
174+
}
175+
}

library/std/src/io/cursor.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ use core::convert::TryInto;
7171
/// }
7272
/// ```
7373
#[stable(feature = "rust1", since = "1.0.0")]
74-
#[derive(Clone, Debug, Default, Eq, PartialEq)]
74+
#[derive(Debug, Default, Eq, PartialEq)]
7575
pub struct Cursor<T> {
7676
inner: T,
7777
pos: u64,
@@ -205,6 +205,23 @@ impl<T> Cursor<T> {
205205
}
206206
}
207207

208+
#[stable(feature = "rust1", since = "1.0.0")]
209+
impl<T> Clone for Cursor<T>
210+
where
211+
T: Clone,
212+
{
213+
#[inline]
214+
fn clone(&self) -> Self {
215+
Cursor { inner: self.inner.clone(), pos: self.pos }
216+
}
217+
218+
#[inline]
219+
fn clone_from(&mut self, other: &Self) {
220+
self.inner.clone_from(&other.inner);
221+
self.pos = other.pos;
222+
}
223+
}
224+
208225
#[stable(feature = "rust1", since = "1.0.0")]
209226
impl<T> io::Seek for Cursor<T>
210227
where

0 commit comments

Comments
 (0)