From 3df35a01e9825bbdebb986f980095e468357975f Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Fri, 15 Apr 2016 17:53:43 +0200 Subject: [PATCH] Implement `Default` for more types in the standard library Also add `Hash` to `std::cmp::Ordering` and most possible traits to `fmt::Error`. --- src/libcore/cell.rs | 7 +++++++ src/libcore/cmp.rs | 2 +- src/libcore/fmt/mod.rs | 2 +- src/libstd/sync/condvar.rs | 7 +++++++ src/libstd/sync/mutex.rs | 7 +++++++ src/libstd/sync/rwlock.rs | 7 +++++++ 6 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index b2cbc29b1c74c..c3764ed6f07c7 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -863,3 +863,10 @@ impl UnsafeCell { &self.value as *const T as *mut T } } + +#[stable(feature = "unsafe_cell_default", since = "1.9.0")] +impl Default for UnsafeCell { + fn default() -> UnsafeCell { + UnsafeCell::new(Default::default()) + } +} diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 49aa0238a996a..d3481ba3f0523 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -128,7 +128,7 @@ pub trait Eq: PartialEq { /// let result = 2.cmp(&1); /// assert_eq!(Ordering::Greater, result); /// ``` -#[derive(Clone, Copy, PartialEq, Debug)] +#[derive(Clone, Copy, PartialEq, Debug, Hash)] #[stable(feature = "rust1", since = "1.0.0")] pub enum Ordering { /// An ordering where a compared value is less [than another]. diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 2f02f5c21f51b..0c824b5a8e69a 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -60,7 +60,7 @@ pub type Result = result::Result<(), Error>; /// occurred. Any extra information must be arranged to be transmitted through /// some other means. #[stable(feature = "rust1", since = "1.0.0")] -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct Error; /// A collection of methods that are required to format a message into a stream. diff --git a/src/libstd/sync/condvar.rs b/src/libstd/sync/condvar.rs index 64468be396f34..57932f0379683 100644 --- a/src/libstd/sync/condvar.rs +++ b/src/libstd/sync/condvar.rs @@ -220,6 +220,13 @@ impl Condvar { pub fn notify_all(&self) { unsafe { self.inner.inner.notify_all() } } } +#[stable(feature = "condvar_default", since = "1.9.0")] +impl Default for Condvar { + fn default() -> Condvar { + Condvar::new() + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl Drop for Condvar { fn drop(&mut self) { diff --git a/src/libstd/sync/mutex.rs b/src/libstd/sync/mutex.rs index e0946a5c12a77..857a4cba9c7ad 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -310,6 +310,13 @@ impl Drop for Mutex { } } +#[stable(feature = "mutex_default", since = "1.9.0")] +impl Default for Mutex { + fn default() -> Mutex { + Mutex::new(Default::default()) + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Debug for Mutex { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { diff --git a/src/libstd/sync/rwlock.rs b/src/libstd/sync/rwlock.rs index a37c1c16a45e9..09b1b0a939dcd 100644 --- a/src/libstd/sync/rwlock.rs +++ b/src/libstd/sync/rwlock.rs @@ -346,6 +346,13 @@ impl fmt::Debug for RwLock { } } +#[stable(feature = "rw_lock_default", since = "1.9.0")] +impl Default for RwLock { + fn default() -> RwLock { + RwLock::new(Default::default()) + } +} + struct Dummy(UnsafeCell<()>); unsafe impl Sync for Dummy {} static DUMMY: Dummy = Dummy(UnsafeCell::new(()));