Struct DirLock

Source
pub struct DirLock(/* private fields */);
Expand description

A simple file-system-based mutex.

When constructing a value of this type, a directory is created at the specified path. If a directory already exists, the constructor waits until it’s removed. Dropping a DirLock removes the corresponding directory. Since creating a directory if it does not exist is an atomic operation on most operating systems, this can be used as a quick-and-dirty cross-process mutex.

To guard against processes exiting without properly removing the lock, a file containing the current process ID is created inside the lock. If no process with that ID exists, another process may claim the lock for itself. If the file does not exist, the constructor waits until it does (or until the directory is removed).

Of course, this is still not completely fail-proof since the user or other processes could mess with the lock directory.

This type is a RAII lock guard, but unlocking a directory lock uses I/O and can error, so it is recommended to call drop_async.

Implementations§

Source§

impl DirLock

Source

pub async fn new(path: impl AsRef<Path>) -> Result<Self, Error>

Acquires a directory lock at the given path, without blocking the thread.

See the type-level docs for details.

Source

pub fn new_sync(path: &impl AsRef<Path>) -> Result<Self, Error>

Blocks the current thread until the lock can be established.

Source

pub fn path(&self) -> &Path

Return the contained Path.

Source

pub async fn drop_async(self) -> Result<(), Error>

Unlocks this lock without blocking the thread.

Trait Implementations§

Source§

impl Drop for DirLock

Source§

fn drop(&mut self)

Unlocks this lock, blocking the current thread while doing so.

§Panics

Unlocking a directory lock involves I/O. If an error occurs, this method will panic. It is recommended to use drop_async instead, which returns the error.

Auto Trait Implementations§

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>,

Source§

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>,

Source§

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.