]> git.lizzy.rs Git - rust.git/commitdiff
Implement `Default` for more types in the standard library
authorTobias Bucher <tobiasbucher5991@gmail.com>
Fri, 15 Apr 2016 15:53:43 +0000 (17:53 +0200)
committerTobias Bucher <tobiasbucher5991@gmail.com>
Fri, 15 Apr 2016 15:53:43 +0000 (17:53 +0200)
Also add `Hash` to `std::cmp::Ordering` and most possible traits to
`fmt::Error`.

src/libcore/cell.rs
src/libcore/cmp.rs
src/libcore/fmt/mod.rs
src/libstd/sync/condvar.rs
src/libstd/sync/mutex.rs
src/libstd/sync/rwlock.rs

index b2cbc29b1c74c146bfef4bb62a6649eb80f6fe54..c3764ed6f07c7fd9c9aad37a5878dceecba24241 100644 (file)
@@ -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<T: Default> Default for UnsafeCell<T> {
+    fn default() -> UnsafeCell<T> {
+        UnsafeCell::new(Default::default())
+    }
+}
index 49aa0238a996a5f236f68749992ecf5236340056..d3481ba3f0523079270fb21ebea95f2416383992 100644 (file)
@@ -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].
index 2f02f5c21f51b8768c837f04fe07d07fe9b2d12e..0c824b5a8e69a0df1283bb4b317e74551400c216 100644 (file)
@@ -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.
index 64468be396f344f2f875fc079aacac2dfbbe5de5..57932f0379683dd6bc63519a9693912a8a6f678e 100644 (file)
@@ -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) {
index e0946a5c12a77d25ccd077757a988e870192473e..857a4cba9c7ad2f6f76fd0bf13989b96cd33f329 100644 (file)
@@ -310,6 +310,13 @@ fn drop(&mut self) {
     }
 }
 
+#[stable(feature = "mutex_default", since = "1.9.0")]
+impl<T: ?Sized + Default> Default for Mutex<T> {
+    fn default() -> Mutex<T> {
+        Mutex::new(Default::default())
+    }
+}
+
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
index a37c1c16a45e98b58d2622ca55b80101d2dcdd01..09b1b0a939dcdad2bcebcfb9b2bfdcfaff3188b5 100644 (file)
@@ -346,6 +346,13 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
     }
 }
 
+#[stable(feature = "rw_lock_default", since = "1.9.0")]
+impl<T: Default> Default for RwLock<T> {
+    fn default() -> RwLock<T> {
+        RwLock::new(Default::default())
+    }
+}
+
 struct Dummy(UnsafeCell<()>);
 unsafe impl Sync for Dummy {}
 static DUMMY: Dummy = Dummy(UnsafeCell::new(()));