Skip to content

Commit

Permalink
Auto merge of #32785 - tbu-:pr_more_defaults, r=alexcrichton
Browse files Browse the repository at this point in the history
Implement `Default` for more types in the standard library

Also add `Hash` to `std::cmp::Ordering` and most possible traits to
`fmt::Error`.
  • Loading branch information
bors committed Apr 16, 2016
2 parents fef6c64 + 3df35a0 commit 6fa61b8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,3 +859,10 @@ impl<T: ?Sized> UnsafeCell<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())
}
}
2 changes: 1 addition & 1 deletion src/libcore/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub trait Eq: PartialEq<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].
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions src/libstd/sync/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions src/libstd/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,13 @@ impl<T: ?Sized> Drop for Mutex<T> {
}
}

#[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 {
Expand Down
7 changes: 7 additions & 0 deletions src/libstd/sync/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,13 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> {
}
}

#[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(()));
Expand Down

0 comments on commit 6fa61b8

Please sign in to comment.