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]

Create a new RwLock in the unlocked state.

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]

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);

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));

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);

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);

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]

Formats the value using the given formatter. Read more

impl<T: ?Sized> Clone for RwLock<T>
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<T: ?Sized + Send> Send for RwLock<T>
[src]

impl<T: ?Sized + Send> Sync for RwLock<T>
[src]