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.
This commit is contained in:
commit
3d31363338
4 changed files with 55 additions and 3 deletions
|
@ -1123,6 +1123,15 @@ impl<T: Clone> Clone for RefCell<T> {
|
||||||
fn clone(&self) -> RefCell<T> {
|
fn clone(&self) -> RefCell<T> {
|
||||||
RefCell::new(self.borrow().clone())
|
RefCell::new(self.borrow().clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics if `other` is currently mutably borrowed.
|
||||||
|
#[inline]
|
||||||
|
#[track_caller]
|
||||||
|
fn clone_from(&mut self, other: &Self) {
|
||||||
|
self.get_mut().clone_from(&other.borrow())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
|
|
@ -578,7 +578,7 @@ impl Ordering {
|
||||||
/// v.sort_by_key(|&num| (num > 3, Reverse(num)));
|
/// v.sort_by_key(|&num| (num > 3, Reverse(num)));
|
||||||
/// assert_eq!(v, vec![3, 2, 1, 6, 5, 4]);
|
/// assert_eq!(v, vec![3, 2, 1, 6, 5, 4]);
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(PartialEq, Eq, Debug, Copy, Clone, Default, Hash)]
|
#[derive(PartialEq, Eq, Debug, Copy, Default, Hash)]
|
||||||
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
|
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
pub struct Reverse<T>(#[stable(feature = "reverse_cmp_key", since = "1.19.0")] pub T);
|
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> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
|
||||||
|
impl<T: Clone> Clone for Reverse<T> {
|
||||||
|
#[inline]
|
||||||
|
fn clone(&self) -> Reverse<T> {
|
||||||
|
Reverse(self.0.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn clone_from(&mut self, other: &Self) {
|
||||||
|
self.0.clone_from(&other.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Trait for types that form a [total order](https://en.wikipedia.org/wiki/Total_order).
|
/// Trait for types that form a [total order](https://en.wikipedia.org/wiki/Total_order).
|
||||||
///
|
///
|
||||||
/// An order is a total order if it is (for all `a`, `b` and `c`):
|
/// An order is a total order if it is (for all `a`, `b` and `c`):
|
||||||
|
|
|
@ -44,7 +44,7 @@ use crate::ptr;
|
||||||
/// [`MaybeUninit<T>`]: crate::mem::MaybeUninit
|
/// [`MaybeUninit<T>`]: crate::mem::MaybeUninit
|
||||||
#[stable(feature = "manually_drop", since = "1.20.0")]
|
#[stable(feature = "manually_drop", since = "1.20.0")]
|
||||||
#[lang = "manually_drop"]
|
#[lang = "manually_drop"]
|
||||||
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
pub struct ManuallyDrop<T: ?Sized> {
|
pub struct ManuallyDrop<T: ?Sized> {
|
||||||
value: T,
|
value: T,
|
||||||
|
@ -160,3 +160,16 @@ impl<T: ?Sized> DerefMut for ManuallyDrop<T> {
|
||||||
&mut self.value
|
&mut self.value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[stable(feature = "manually_drop", since = "1.20.0")]
|
||||||
|
impl<T: Clone> Clone for ManuallyDrop<T> {
|
||||||
|
#[inline]
|
||||||
|
fn clone(&self) -> ManuallyDrop<T> {
|
||||||
|
ManuallyDrop { value: self.value.clone() }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn clone_from(&mut self, other: &Self) {
|
||||||
|
self.value.clone_from(&other.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -71,7 +71,7 @@ use core::convert::TryInto;
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
#[derive(Debug, Default, Eq, PartialEq)]
|
||||||
pub struct Cursor<T> {
|
pub struct Cursor<T> {
|
||||||
inner: T,
|
inner: T,
|
||||||
pos: u64,
|
pos: u64,
|
||||||
|
@ -205,6 +205,23 @@ impl<T> Cursor<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
impl<T> Clone for Cursor<T>
|
||||||
|
where
|
||||||
|
T: Clone,
|
||||||
|
{
|
||||||
|
#[inline]
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Cursor { inner: self.inner.clone(), pos: self.pos }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn clone_from(&mut self, other: &Self) {
|
||||||
|
self.inner.clone_from(&other.inner);
|
||||||
|
self.pos = other.pos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl<T> io::Seek for Cursor<T>
|
impl<T> io::Seek for Cursor<T>
|
||||||
where
|
where
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue