Struct futures_locks::RwLock [−][src]
pub struct RwLock<T: ?Sized> { /* fields omitted */ }
A Futures-aware RwLock.
std::sync::RwLock
cannot be used in an asynchronous environment like
Tokio, because an acquisition can block an entire reactor. This class can
be used instead. It functions much like std::sync::RwLock
. Unlike that
class, it also has a builtin Arc
, making it accessible from multiple
threads. It's also safe to clone
. Also unlike std::sync::RwLock
, this
class does not detect lock poisoning.
Methods
impl<T> RwLock<T>
[src]
impl<T> RwLock<T>
pub fn new(t: T) -> RwLock<T>
[src]
pub fn new(t: T) -> RwLock<T>
Create a new RwLock
in the unlocked state.
pub fn try_unwrap(self) -> Result<T, RwLock<T>>
[src]
pub fn try_unwrap(self) -> Result<T, RwLock<T>>
Consumes the RwLock
and returns the wrapped data. If the RwLock
still has multiple references (not necessarily locked), returns a copy
of self
instead.
impl<T: ?Sized> RwLock<T>
[src]
impl<T: ?Sized> RwLock<T>
pub fn get_mut(&mut self) -> Option<&mut T>
[src]
pub fn get_mut(&mut self) -> Option<&mut T>
Returns a reference to the underlying data, if there are no other
clones of the RwLock
.
Since this call borrows the RwLock
mutably, no actual locking takes
place -- the mutable borrow statically guarantees no locks exist.
However, if the RwLock
has already been cloned, then None
will be
returned instead.
Examples
let mut lock = RwLock::<u32>::new(0); *lock.get_mut().unwrap() += 5; assert_eq!(lock.try_unwrap().unwrap(), 5);
pub fn read(&self) -> RwLockReadFut<T>
[src]
pub fn read(&self) -> RwLockReadFut<T>
Acquire the RwLock
nonexclusively, read-only, blocking the task in the
meantime.
When the returned Future
is ready, then this task will have read-only
access to the protected data.
Examples
let rwlock = RwLock::<u32>::new(42); let fut = rwlock.read().map(|mut guard| { *guard }); assert_eq!(spawn(fut).wait_future(), Ok(42));
pub fn write(&self) -> RwLockWriteFut<T>
[src]
pub fn write(&self) -> RwLockWriteFut<T>
Acquire the RwLock
exclusively, read-write, blocking the task in the
meantime.
When the returned Future
is ready, then this task will have read-write
access to the protected data.
Examples
let rwlock = RwLock::<u32>::new(42); let fut = rwlock.write().map(|mut guard| { *guard = 5;}); spawn(fut).wait_future().expect("spawn"); assert_eq!(rwlock.try_unwrap().unwrap(), 5);
pub fn try_read(&self) -> Result<RwLockReadGuard<T>, ()>
[src]
pub fn try_read(&self) -> Result<RwLockReadGuard<T>, ()>
Attempts to acquire the RwLock
nonexclusively.
If the operation would block, returns Err
instead. Otherwise, returns
a guard (not a Future
).
Examples
let mut lock = RwLock::<u32>::new(5); let r = match lock.try_read() { Ok(guard) => *guard, Err(()) => panic!("Better luck next time!") }; assert_eq!(5, r);
pub fn try_write(&self) -> Result<RwLockWriteGuard<T>, ()>
[src]
pub fn try_write(&self) -> Result<RwLockWriteGuard<T>, ()>
Attempts to acquire the RwLock
exclusively.
If the operation would block, returns Err
instead. Otherwise, returns
a guard (not a Future
).
Examples
let mut lock = RwLock::<u32>::new(5); match lock.try_write() { Ok(mut guard) => *guard += 5, Err(()) => panic!("Better luck next time!") } assert_eq!(10, lock.try_unwrap().unwrap());
Trait Implementations
impl<T: Debug + ?Sized> Debug for RwLock<T>
[src]
impl<T: Debug + ?Sized> Debug for RwLock<T>
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl<T: ?Sized> Clone for RwLock<T>
[src]
impl<T: ?Sized> Clone for RwLock<T>
fn clone(&self) -> RwLock<T>
[src]
fn clone(&self) -> RwLock<T>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl<T: ?Sized + Send> Send for RwLock<T>
[src]
impl<T: ?Sized + Send> Send for RwLock<T>
impl<T: ?Sized + Send> Sync for RwLock<T>
[src]
impl<T: ?Sized + Send> Sync for RwLock<T>