Skip to content

Commit f578cff

Browse files
committed
add arc method to RwLock and Mutex
1 parent 8b40853 commit f578cff

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@
320320
#![feature(unwind_attributes)]
321321
#![feature(vec_into_raw_parts)]
322322
#![feature(wake_trait)]
323+
#![feature(mutex_arc)]
323324
// NB: the above list is sorted to minimize merge conflicts.
324325
#![default_lib_allocator]
325326

library/std/src/sync/mutex.rs

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::fmt;
66
use crate::mem;
77
use crate::ops::{Deref, DerefMut};
88
use crate::ptr;
9+
use crate::sync::Arc;
910
use crate::sys_common::mutex as sys;
1011
use crate::sys_common::poison::{self, LockResult, TryLockError, TryLockResult};
1112

@@ -228,6 +229,22 @@ impl<T> Mutex<T> {
228229
}
229230
m
230231
}
232+
233+
/// Creates a new mutex in an unlocked state ready for use, and wraps it in an [`Arc`].
234+
///
235+
/// [`Arc`]: ../../std/sync/struct.Arc.html
236+
///
237+
/// # Examples
238+
///
239+
/// ```
240+
/// use std::sync::Mutex;
241+
///
242+
/// let mutex = Mutex::arc(0);
243+
/// ```
244+
#[unstable(feature = "mutex_arc", issue = "74866")]
245+
pub fn arc(t: T) -> Arc<Mutex<T>> {
246+
Arc::new(Mutex::new(t))
247+
}
231248
}
232249

233250
impl<T: ?Sized> Mutex<T> {

library/std/src/sync/mutex/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ fn test_into_inner_drop() {
8383

8484
#[test]
8585
fn test_into_inner_poison() {
86-
let m = Arc::new(Mutex::new(NonCopy(10)));
86+
let m = Mutex::arc(NonCopy(10));
8787
let m2 = m.clone();
8888
let _ = thread::spawn(move || {
8989
let _lock = m2.lock().unwrap();
@@ -107,7 +107,7 @@ fn test_get_mut() {
107107

108108
#[test]
109109
fn test_get_mut_poison() {
110-
let m = Arc::new(Mutex::new(NonCopy(10)));
110+
let m = Mutex::arc(NonCopy(10));
111111
let m2 = m.clone();
112112
let _ = thread::spawn(move || {
113113
let _lock = m2.lock().unwrap();

library/std/src/sync/rwlock.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::fmt;
66
use crate::mem;
77
use crate::ops::{Deref, DerefMut};
88
use crate::ptr;
9+
use crate::sync::Arc;
910
use crate::sys_common::poison::{self, LockResult, TryLockError, TryLockResult};
1011
use crate::sys_common::rwlock as sys;
1112

@@ -133,6 +134,22 @@ impl<T> RwLock<T> {
133134
data: UnsafeCell::new(t),
134135
}
135136
}
137+
138+
/// Creates a new instance of an `RwLock<T>` which is unlocked, and wraps it in an [`Arc`].
139+
///
140+
/// [`Arc`]: ../../std/sync/struct.Arc.html
141+
///
142+
/// # Examples
143+
///
144+
/// ```
145+
/// use std::sync::RwLock;
146+
///
147+
/// let lock = RwLock::arc(5);
148+
/// ```
149+
#[unstable(feature = "mutex_arc", issue = "74866")]
150+
pub fn arc(t: T) -> Arc<RwLock<T>> {
151+
Arc::new(RwLock::new(t))
152+
}
136153
}
137154

138155
impl<T: ?Sized> RwLock<T> {
@@ -164,7 +181,7 @@ impl<T: ?Sized> RwLock<T> {
164181
/// use std::sync::{Arc, RwLock};
165182
/// use std::thread;
166183
///
167-
/// let lock = Arc::new(RwLock::new(1));
184+
/// let lock = RwLock::arc(1);
168185
/// let c_lock = Arc::clone(&lock);
169186
///
170187
/// let n = lock.read().unwrap();
@@ -320,7 +337,7 @@ impl<T: ?Sized> RwLock<T> {
320337
/// use std::sync::{Arc, RwLock};
321338
/// use std::thread;
322339
///
323-
/// let lock = Arc::new(RwLock::new(0));
340+
/// let lock = RwLock::arc(0);
324341
/// let c_lock = Arc::clone(&lock);
325342
///
326343
/// let _ = thread::spawn(move || {

library/std/src/sync/rwlock/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn frob() {
2121
const N: u32 = 10;
2222
const M: usize = 1000;
2323

24-
let r = Arc::new(RwLock::new(()));
24+
let r = RwLock::arc(());
2525

2626
let (tx, rx) = channel::<()>();
2727
for _ in 0..N {

0 commit comments

Comments
 (0)