Sha256: 6ae2944ce189a61e7dfc2a6d86aa6e23f4c22d8527f8f0215f72e4687e609d33

Contents?: true

Size: 1.64 KB

Versions: 19

Compression:

Stored size: 1.64 KB

Contents

use rustix::fd::AsFd;
use rustix::fs::FlockOperation;
use std::io::{self, Error, ErrorKind};

use super::{compatible_unix_lock, RwLockReadGuard, RwLockWriteGuard};

#[derive(Debug)]
pub struct RwLock<T: AsFd> {
    pub(crate) inner: T,
}

impl<T: AsFd> RwLock<T> {
    #[inline]
    pub fn new(inner: T) -> Self {
        RwLock { inner }
    }

    #[inline]
    pub fn write(&mut self) -> io::Result<RwLockWriteGuard<'_, T>> {
        compatible_unix_lock(self.inner.as_fd(), FlockOperation::LockExclusive)?;
        Ok(RwLockWriteGuard::new(self))
    }

    #[inline]
    pub fn try_write(&mut self) -> Result<RwLockWriteGuard<'_, T>, Error> {
        compatible_unix_lock(self.inner.as_fd(), FlockOperation::NonBlockingLockExclusive)
            .map_err(|err| match err.kind() {
                ErrorKind::AlreadyExists => ErrorKind::WouldBlock.into(),
                _ => Error::from(err),
            })?;
        Ok(RwLockWriteGuard::new(self))
    }

    #[inline]
    pub fn read(&self) -> io::Result<RwLockReadGuard<'_, T>> {
        compatible_unix_lock(self.inner.as_fd(), FlockOperation::LockShared)?;
        Ok(RwLockReadGuard::new(self))
    }

    #[inline]
    pub fn try_read(&self) -> Result<RwLockReadGuard<'_, T>, Error> {
        compatible_unix_lock(self.inner.as_fd(), FlockOperation::NonBlockingLockShared).map_err(
            |err| match err.kind() {
                ErrorKind::AlreadyExists => ErrorKind::WouldBlock.into(),
                _ => Error::from(err),
            },
        )?;
        Ok(RwLockReadGuard::new(self))
    }

    #[inline]
    pub fn into_inner(self) -> T
    where
        T: Sized,
    {
        self.inner
    }
}

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/fd-lock-4.0.2/src/sys/unix/rw_lock.rs
wasmtime-28.0.0 ./ext/cargo-vendor/fd-lock-4.0.2/src/sys/unix/rw_lock.rs
wasmtime-27.0.0 ./ext/cargo-vendor/fd-lock-4.0.2/src/sys/unix/rw_lock.rs
wasmtime-26.0.0 ./ext/cargo-vendor/fd-lock-4.0.2/src/sys/unix/rw_lock.rs
wasmtime-25.0.2 ./ext/cargo-vendor/fd-lock-4.0.2/src/sys/unix/rw_lock.rs
wasmtime-25.0.1 ./ext/cargo-vendor/fd-lock-4.0.2/src/sys/unix/rw_lock.rs
wasmtime-25.0.0 ./ext/cargo-vendor/fd-lock-4.0.2/src/sys/unix/rw_lock.rs
wasmtime-24.0.0 ./ext/cargo-vendor/fd-lock-4.0.2/src/sys/unix/rw_lock.rs
wasmtime-23.0.2 ./ext/cargo-vendor/fd-lock-4.0.2/src/sys/unix/rw_lock.rs
wasmtime-22.0.0 ./ext/cargo-vendor/fd-lock-4.0.2/src/sys/unix/rw_lock.rs
wasmtime-21.0.1 ./ext/cargo-vendor/fd-lock-4.0.2/src/sys/unix/rw_lock.rs
wasmtime-20.0.2 ./ext/cargo-vendor/fd-lock-4.0.2/src/sys/unix/rw_lock.rs
wasmtime-20.0.0 ./ext/cargo-vendor/fd-lock-4.0.2/src/sys/unix/rw_lock.rs
wasmtime-18.0.3 ./ext/cargo-vendor/fd-lock-4.0.2/src/sys/unix/rw_lock.rs
wasmtime-17.0.1 ./ext/cargo-vendor/fd-lock-4.0.2/src/sys/unix/rw_lock.rs
wasmtime-17.0.0 ./ext/cargo-vendor/fd-lock-4.0.2/src/sys/unix/rw_lock.rs
wasmtime-16.0.0 ./ext/cargo-vendor/fd-lock-4.0.2/src/sys/unix/rw_lock.rs
wasmtime-15.0.1 ./ext/cargo-vendor/fd-lock-4.0.2/src/sys/unix/rw_lock.rs
wasmtime-15.0.0 ./ext/cargo-vendor/fd-lock-4.0.2/src/sys/unix/rw_lock.rs