1
1
use crate :: cell:: UnsafeCell ;
2
2
use crate :: fmt;
3
3
use crate :: marker:: PhantomData ;
4
- use crate :: mem:: ManuallyDrop ;
4
+ use crate :: mem:: { self , ManuallyDrop } ;
5
5
use crate :: ops:: { Deref , DerefMut } ;
6
6
use crate :: ptr:: NonNull ;
7
7
use crate :: sync:: nonpoison:: { TryLockResult , WouldBlock } ;
8
8
use crate :: sys:: sync as sys;
9
9
10
- /// A mutual exclusion primitive useful for protecting shared data.
10
+ /// A mutual exclusion primitive useful for protecting shared data that does not keep track of
11
+ /// lock poisoning.
11
12
///
12
13
/// For more information about mutexes, check out the documentation for the poisoning variant of
13
- /// this lock (which can be found at [`poison::Mutex`])
14
+ /// this lock (which can be found at [`poison::Mutex`]).
14
15
///
15
16
/// # Examples
16
17
///
@@ -259,6 +260,7 @@ impl<T> Mutex<T> {
259
260
///
260
261
/// ```
261
262
/// #![feature(nonpoison_mutex)]
263
+ ///
262
264
/// use std::sync::nonpoison::Mutex;
263
265
///
264
266
/// let mutex = Mutex::new(0);
@@ -269,7 +271,72 @@ impl<T> Mutex<T> {
269
271
Mutex { inner : sys:: Mutex :: new ( ) , data : UnsafeCell :: new ( t) }
270
272
}
271
273
272
- // FIXME: Add `lock_value_accessors` feature methods.
274
+ /// Returns the contained value by cloning it.
275
+ ///
276
+ /// # Examples
277
+ ///
278
+ /// ```
279
+ /// #![feature(nonpoison_mutex)]
280
+ /// #![feature(lock_value_accessors)]
281
+ ///
282
+ /// use std::sync::nonpoison::Mutex;
283
+ ///
284
+ /// let mut mutex = Mutex::new(7);
285
+ ///
286
+ /// assert_eq!(mutex.get_cloned(), 7);
287
+ /// ```
288
+ #[ unstable( feature = "lock_value_accessors" , issue = "133407" ) ]
289
+ pub fn get_cloned ( & self ) -> T
290
+ where
291
+ T : Clone ,
292
+ {
293
+ self . lock ( ) . clone ( )
294
+ }
295
+
296
+ /// Sets the contained value.
297
+ ///
298
+ /// # Examples
299
+ ///
300
+ /// ```
301
+ /// #![feature(nonpoison_mutex)]
302
+ /// #![feature(lock_value_accessors)]
303
+ ///
304
+ /// use std::sync::nonpoison::Mutex;
305
+ ///
306
+ /// let mut mutex = Mutex::new(7);
307
+ ///
308
+ /// assert_eq!(mutex.get_cloned(), 7);
309
+ /// mutex.set(11);
310
+ /// assert_eq!(mutex.get_cloned(), 11);
311
+ /// ```
312
+ #[ unstable( feature = "lock_value_accessors" , issue = "133407" ) ]
313
+ pub fn set ( & self , value : T ) {
314
+ let old = self . replace ( value) ;
315
+ if mem:: needs_drop :: < T > ( ) {
316
+ drop ( old)
317
+ }
318
+ }
319
+
320
+ /// Replaces the contained value with `value`, and returns the old contained value.
321
+ ///
322
+ /// # Examples
323
+ ///
324
+ /// ```
325
+ /// #![feature(nonpoison_mutex)]
326
+ /// #![feature(lock_value_accessors)]
327
+ ///
328
+ /// use std::sync::nonpoison::Mutex;
329
+ ///
330
+ /// let mut mutex = Mutex::new(7);
331
+ ///
332
+ /// assert_eq!(mutex.replace(11), 7);
333
+ /// assert_eq!(mutex.get_cloned(), 11);
334
+ /// ```
335
+ #[ unstable( feature = "lock_value_accessors" , issue = "133407" ) ]
336
+ pub fn replace ( & self , value : T ) -> T {
337
+ let mut guard = self . lock ( ) ;
338
+ mem:: replace ( & mut * guard, value)
339
+ }
273
340
}
274
341
275
342
impl < T : ?Sized > Mutex < T > {
@@ -293,6 +360,7 @@ impl<T: ?Sized> Mutex<T> {
293
360
///
294
361
/// ```
295
362
/// #![feature(nonpoison_mutex)]
363
+ ///
296
364
/// use std::sync::{Arc, nonpoison::Mutex};
297
365
/// use std::thread;
298
366
///
@@ -329,6 +397,7 @@ impl<T: ?Sized> Mutex<T> {
329
397
///
330
398
/// ```
331
399
/// #![feature(nonpoison_mutex)]
400
+ ///
332
401
/// use std::sync::{Arc, nonpoison::Mutex};
333
402
/// use std::thread;
334
403
///
@@ -356,6 +425,7 @@ impl<T: ?Sized> Mutex<T> {
356
425
///
357
426
/// ```
358
427
/// #![feature(nonpoison_mutex)]
428
+ ///
359
429
/// use std::sync::nonpoison::Mutex;
360
430
///
361
431
/// let mutex = Mutex::new(0);
@@ -378,6 +448,7 @@ impl<T: ?Sized> Mutex<T> {
378
448
///
379
449
/// ```
380
450
/// #![feature(nonpoison_mutex)]
451
+ ///
381
452
/// use std::sync::nonpoison::Mutex;
382
453
///
383
454
/// let mut mutex = Mutex::new(0);
@@ -389,7 +460,16 @@ impl<T: ?Sized> Mutex<T> {
389
460
self . data . get_mut ( )
390
461
}
391
462
392
- // FIXME: Add `mutex_data_ptr` feature method.
463
+ /// Returns a raw pointer to the underlying data.
464
+ ///
465
+ /// The returned pointer is always non-null and properly aligned, but it is
466
+ /// the user's responsibility to ensure that any reads and writes through it
467
+ /// are properly synchronized to avoid data races, and that it is not read
468
+ /// or written through after the mutex is dropped.
469
+ #[ unstable( feature = "mutex_data_ptr" , issue = "140368" ) ]
470
+ pub fn data_ptr ( & self ) -> * mut T {
471
+ self . data . get ( )
472
+ }
393
473
}
394
474
395
475
#[ unstable( feature = "nonpoison_mutex" , issue = "134645" ) ]
0 commit comments