Struct fd_lock::RwLock

source ·
pub struct RwLock<T: AsOpenFile> { /* private fields */ }
Expand description

Advisory reader-writer lock for files.

This type of lock allows a number of readers or at most one writer at any point in time. The write portion of this lock typically allows modification of the underlying data (exclusive access) and the read portion of this lock typically allows for read-only access (shared access).

Implementations§

source§

impl<T: AsOpenFile> RwLock<T>

source

pub fn new(inner: T) -> Self

Create a new instance.

Examples
use fd_lock::RwLock;
use std::fs::File;

fn main() -> std::io::Result<()> {
    let mut f = RwLock::new(File::open("foo.txt")?);
    Ok(())
}
source

pub fn read(&self) -> Result<RwLockReadGuard<'_, T>>

Locks this lock with shared read access, blocking the current thread until it can be acquired.

The calling thread will be blocked until there are no more writers which hold the lock. There may be other readers currently inside the lock when this method returns. This method does not provide any guarantees with respect to the ordering of whether contentious readers or writers will acquire the lock first.

Returns an RAII guard which will release this thread’s shared access once it is dropped.

Errors

On Unix this may return an ErrorKind::Interrupted if the operation was interrupted by a signal handler.

source

pub fn try_read(&self) -> Result<RwLockReadGuard<'_, T>>

Attempts to acquire this lock with shared read access.

If the access could not be granted at this time, then Err is returned. Otherwise, an RAII guard is returned which will release the shared access when it is dropped.

This function does not block.

This function does not provide any guarantees with respect to the ordering of whether contentious readers or writers will acquire the lock first.

Errors

If the lock is already held and ErrorKind::WouldBlock error is returned. On Unix this may return an ErrorKind::Interrupted if the operation was interrupted by a signal handler.

source

pub fn write(&mut self) -> Result<RwLockWriteGuard<'_, T>>

Locks this lock with exclusive write access, blocking the current thread until it can be acquired.

This function will not return while other writers or other readers currently have access to the lock.

Returns an RAII guard which will drop the write access of this rwlock when dropped.

Errors

On Unix this may return an ErrorKind::Interrupted if the operation was interrupted by a signal handler.

source

pub fn try_write(&mut self) -> Result<RwLockWriteGuard<'_, T>>

Attempts to lock this lock with exclusive write access.

If the lock could not be acquired at this time, then Err is returned. Otherwise, an RAII guard is returned which will release the lock when it is dropped.

Errors

If the lock is already held and ErrorKind::WouldBlock error is returned. On Unix this may return an ErrorKind::Interrupted if the operation was interrupted by a signal handler.

source

pub fn into_inner(self) -> T
where T: Sized,

Consumes this RwLock, returning the underlying data.

Trait Implementations§

source§

impl<T: Debug + AsOpenFile> Debug for RwLock<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for RwLock<T>
where T: RefUnwindSafe,

§

impl<T> Send for RwLock<T>
where T: Send,

§

impl<T> Sync for RwLock<T>
where T: Sync,

§

impl<T> Unpin for RwLock<T>
where T: Unpin,

§

impl<T> UnwindSafe for RwLock<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.