@@ -498,6 +498,68 @@ impl<T: ?Sized> RwLock<T> {
498498 pub const fn data_ptr ( & self ) -> * mut T {
499499 self . data . get ( )
500500 }
501+
502+ /// Locks this `RwLock` with shared read access to the underlying data by passing
503+ /// a reference to the given closure.
504+ ///
505+ /// This method acquires the lock, calls the provided closure with a reference
506+ /// to the data, and returns the result of the closure. The lock is released after
507+ /// the closure completes, even if it panics.
508+ ///
509+ /// # Examples
510+ ///
511+ /// ```
512+ /// #![feature(lock_value_accessors, nonpoison_rwlock)]
513+ ///
514+ /// use std::sync::nonpoison::RwLock;
515+ ///
516+ /// let rwlock = RwLock::new(2);
517+ /// let result = rwlock.with(|data| *data + 3);
518+ ///
519+ /// assert_eq!(result, 5);
520+ /// ```
521+ #[ unstable( feature = "lock_value_accessors" , issue = "133407" ) ]
522+ // #[unstable(feature = "nonpoison_rwlock", issue = "134645")]
523+ pub fn with < F , R > ( & self , f : F ) -> R
524+ where
525+ F : FnOnce ( & T ) -> R ,
526+ {
527+ f ( & self . read ( ) )
528+ }
529+
530+ /// Locks this `RwLock` with exclusive write access to the underlying data by passing
531+ /// a mutable reference to the given closure.
532+ ///
533+ /// This method acquires the lock, calls the provided closure with a mutable reference
534+ /// to the data, and returns the result of the closure. The lock is released after
535+ /// the closure completes, even if it panics.
536+ ///
537+ /// # Examples
538+ ///
539+ /// ```
540+ /// #![feature(lock_value_accessors, nonpoison_rwlock)]
541+ ///
542+ /// use std::sync::nonpoison::RwLock;
543+ ///
544+ /// let rwlock = RwLock::new(2);
545+ ///
546+ /// let result = rwlock.with_mut(|data| {
547+ /// *data += 3;
548+ ///
549+ /// *data + 5
550+ /// });
551+ ///
552+ /// assert_eq!(*rwlock.read(), 5);
553+ /// assert_eq!(result, 10);
554+ /// ```
555+ #[ unstable( feature = "lock_value_accessors" , issue = "133407" ) ]
556+ // #[unstable(feature = "nonpoison_rwlock", issue = "134645")]
557+ pub fn with_mut < F , R > ( & self , f : F ) -> R
558+ where
559+ F : FnOnce ( & mut T ) -> R ,
560+ {
561+ f ( & mut self . write ( ) )
562+ }
501563}
502564
503565#[ unstable( feature = "nonpoison_rwlock" , issue = "134645" ) ]
0 commit comments