Skip to content

Commit 2c16502

Browse files
authored
Rollup merge of rust-lang#46082 - Enet4:mutex_from, r=sfackler
impl From for Mutex and RwLock I felt that these implementations were missing, because doing `x.into()` works for other smart containers (such as `RefCell`), and in general I would say that the conversion makes sense.
2 parents e061383 + 0855ea1 commit 2c16502

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/libstd/sync/mutex.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,17 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Mutex<T> {
382382
}
383383
}
384384

385+
#[stable(feature = "mutex_from", since = "1.22.0")]
386+
impl<T> From<T> for Mutex<T> {
387+
/// Creates a new mutex in an unlocked state ready for use.
388+
/// This is equivalent to [`Mutex::new`].
389+
///
390+
/// [`Mutex::new`]: #method.new
391+
fn from(t: T) -> Self {
392+
Mutex::new(t)
393+
}
394+
}
395+
385396
#[stable(feature = "mutex_default", since = "1.10.0")]
386397
impl<T: ?Sized + Default> Default for Mutex<T> {
387398
/// Creates a `Mutex<T>`, with the `Default` value for T.

src/libstd/sync/rwlock.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,17 @@ impl<T: Default> Default for RwLock<T> {
457457
}
458458
}
459459

460+
#[stable(feature = "rw_lock_from", since = "1.22.0")]
461+
impl<T> From<T> for RwLock<T> {
462+
/// Creates a new instance of an `RwLock<T>` which is unlocked.
463+
/// This is equivalent to [`RwLock::new`].
464+
///
465+
/// [`RwLock::new`]: #method.new
466+
fn from(t: T) -> Self {
467+
RwLock::new(t)
468+
}
469+
}
470+
460471
impl<'rwlock, T: ?Sized> RwLockReadGuard<'rwlock, T> {
461472
unsafe fn new(lock: &'rwlock RwLock<T>)
462473
-> LockResult<RwLockReadGuard<'rwlock, T>> {

0 commit comments

Comments
 (0)