Skip to content

Commit 8fc0fc8

Browse files
committed
Auto merge of #44310 - ldr709:master, r=BurntSushi
Additional traits for std::mem::ManuallyDrop The first commit adds `Clone` and `Copy` trait implementations for `ManuallyDrop`. Although `Drop` and `Copy` cannot be used together, this may be useful for generics. The second commit adds implementations common traits. I do not think this is necessary, as they could be implemented in a wrapper type outside the standard library, but it would make `ManuallyDrop` more convenient to use.
2 parents 3cb24bd + 94301c4 commit 8fc0fc8

File tree

1 file changed

+73
-2
lines changed

1 file changed

+73
-2
lines changed

src/libcore/mem.rs

+73-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use hash;
2222
use intrinsics;
2323
use marker::{Copy, PhantomData, Sized};
2424
use ptr;
25+
use ops::{Deref, DerefMut};
2526

2627
#[stable(feature = "rust1", since = "1.0.0")]
2728
pub use intrinsics::transmute;
@@ -821,6 +822,7 @@ pub fn discriminant<T>(v: &T) -> Discriminant<T> {
821822
/// ```
822823
#[stable(feature = "manually_drop", since = "1.20.0")]
823824
#[allow(unions_with_drop_fields)]
825+
#[derive(Copy)]
824826
pub union ManuallyDrop<T>{ value: T }
825827

826828
impl<T> ManuallyDrop<T> {
@@ -870,7 +872,7 @@ impl<T> ManuallyDrop<T> {
870872
}
871873

872874
#[stable(feature = "manually_drop", since = "1.20.0")]
873-
impl<T> ::ops::Deref for ManuallyDrop<T> {
875+
impl<T> Deref for ManuallyDrop<T> {
874876
type Target = T;
875877
#[inline]
876878
fn deref(&self) -> &Self::Target {
@@ -881,7 +883,7 @@ impl<T> ::ops::Deref for ManuallyDrop<T> {
881883
}
882884

883885
#[stable(feature = "manually_drop", since = "1.20.0")]
884-
impl<T> ::ops::DerefMut for ManuallyDrop<T> {
886+
impl<T> DerefMut for ManuallyDrop<T> {
885887
#[inline]
886888
fn deref_mut(&mut self) -> &mut Self::Target {
887889
unsafe {
@@ -899,6 +901,75 @@ impl<T: ::fmt::Debug> ::fmt::Debug for ManuallyDrop<T> {
899901
}
900902
}
901903

904+
#[stable(feature = "manually_drop", since = "1.20.0")]
905+
impl<T: Clone> Clone for ManuallyDrop<T> {
906+
fn clone(&self) -> Self {
907+
ManuallyDrop::new(self.deref().clone())
908+
}
909+
910+
fn clone_from(&mut self, source: &Self) {
911+
self.deref_mut().clone_from(source);
912+
}
913+
}
914+
915+
#[stable(feature = "manually_drop", since = "1.20.0")]
916+
impl<T: Default> Default for ManuallyDrop<T> {
917+
fn default() -> Self {
918+
ManuallyDrop::new(Default::default())
919+
}
920+
}
921+
922+
#[stable(feature = "manually_drop", since = "1.20.0")]
923+
impl<T: PartialEq> PartialEq for ManuallyDrop<T> {
924+
fn eq(&self, other: &Self) -> bool {
925+
self.deref().eq(other)
926+
}
927+
928+
fn ne(&self, other: &Self) -> bool {
929+
self.deref().ne(other)
930+
}
931+
}
932+
933+
#[stable(feature = "manually_drop", since = "1.20.0")]
934+
impl<T: Eq> Eq for ManuallyDrop<T> {}
935+
936+
#[stable(feature = "manually_drop", since = "1.20.0")]
937+
impl<T: PartialOrd> PartialOrd for ManuallyDrop<T> {
938+
fn partial_cmp(&self, other: &Self) -> Option<::cmp::Ordering> {
939+
self.deref().partial_cmp(other)
940+
}
941+
942+
fn lt(&self, other: &Self) -> bool {
943+
self.deref().lt(other)
944+
}
945+
946+
fn le(&self, other: &Self) -> bool {
947+
self.deref().le(other)
948+
}
949+
950+
fn gt(&self, other: &Self) -> bool {
951+
self.deref().gt(other)
952+
}
953+
954+
fn ge(&self, other: &Self) -> bool {
955+
self.deref().ge(other)
956+
}
957+
}
958+
959+
#[stable(feature = "manually_drop", since = "1.20.0")]
960+
impl<T: Ord> Ord for ManuallyDrop<T> {
961+
fn cmp(&self, other: &Self) -> ::cmp::Ordering {
962+
self.deref().cmp(other)
963+
}
964+
}
965+
966+
#[stable(feature = "manually_drop", since = "1.20.0")]
967+
impl<T: ::hash::Hash> ::hash::Hash for ManuallyDrop<T> {
968+
fn hash<H: ::hash::Hasher>(&self, state: &mut H) {
969+
self.deref().hash(state);
970+
}
971+
}
972+
902973
/// Tells LLVM that this point in the code is not reachable, enabling further
903974
/// optimizations.
904975
///

0 commit comments

Comments
 (0)