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 b2cbc29b1c7..c3764ed6f07 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -863,3 +863,10 @@ pub fn get(&self) -> *mut T { &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 49aa0238a99..d3481ba3f05 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -128,7 +128,7 @@ fn assert_receiver_is_total_eq(&self) {} /// 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 2f02f5c21f5..0c824b5a8e6 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -60,7 +60,7 @@ pub mod rt { /// 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 64468be396f..57932f03796 100644 --- a/src/libstd/sync/condvar.rs +++ b/src/libstd/sync/condvar.rs @@ -220,6 +220,13 @@ pub fn notify_one(&self) { unsafe { self.inner.inner.notify_one() } } 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 e0946a5c12a..857a4cba9c7 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -310,6 +310,13 @@ fn drop(&mut self) { } } +#[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 a37c1c16a45..09b1b0a939d 100644 --- a/src/libstd/sync/rwlock.rs +++ b/src/libstd/sync/rwlock.rs @@ -346,6 +346,13 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { } } +#[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(())); -- 2.44.0