[−][src]Struct fslock::LockFile
A handle to a file that is lockable. Does not delete the file.
Multiple Handles/Descriptors To The Same File
Windows will treat each handle as having their own lock, while Unix will have locks on a file for the whole process. This means that on Windows you may open a file, lock it, open it again, and when you try yo lock the second handle, it will block until the first lock is released. Meanwhile, unix will look if your process already owns the look, it will see that you already locked the file, and simply return as you already have it! It will only block if there is a different process holding the lock. Also, unlocking one file descriptor will unlock the file for the whole process.
Example
use fslock::LockFile; let mut file = LockFile::open("mylock.test")?; file.lock()?; do_stuff(); file.unlock()?;
Methods
impl LockFile
[src]
pub fn open<P: ?Sized>(path: &P) -> Result<Self, Error> where
P: ToOsStr,
[src]
P: ToOsStr,
Opens a file for locking. On Unix, if the path is nul-terminated (ends with 0), no extra allocation will be made.
Panics
Panics if the path contains a nul-byte in a place other than the end.
Example
use fslock::LockFile; let mut file = LockFile::open("mylock.test")?;
Panicking Example
use fslock::LockFile; let mut file = LockFile::open("my\0lock")?;
pub fn lock(&mut self) -> Result<(), Error>
[src]
Locks this file. Blocks while it is not possible to lock (i.e. someone else already owns a lock. After locked, if no attempt to unlock is made, it will be automatically unlocked on the file handle drop.
Panics
Panics if this handle already owns the file.
Example
use fslock::LockFile; let mut file = LockFile::open("mylock.test")?; file.lock()?; do_stuff(); file.unlock()?;
Panicking Example
use fslock::LockFile; let mut file = LockFile::open("mylock.test")?; file.lock()?; file.lock()?;
pub fn try_lock(&mut self) -> Result<bool, Error>
[src]
Locks this file. Does NOT block if it is not possible to lock (i.e. someone else already owns a lock. After locked, if no attempt to unlock is made, it will be automatically unlocked on the file handle drop.
Panics
Panics if this handle already owns the file.
Example
use fslock::LockFile; let mut file = LockFile::open("mylock.test")?; if file.try_lock()? { do_stuff(); file.unlock()?; }
Panicking Example
use fslock::LockFile; let mut file = LockFile::open("mylock.test")?; file.lock()?; file.try_lock()?;
pub fn owns_lock(&self) -> bool
[src]
Returns whether this file handle owns the lock.
Example
use fslock::LockFile; let mut file = LockFile::open("mylock.test")?; do_stuff_with_lock(&mut file); if !file.owns_lock() { file.lock()?; do_stuff(); file.unlock()?; }
pub fn unlock(&mut self) -> Result<(), Error>
[src]
Unlocks this file. This file handle must own the file lock. If not
called manually, it is automatically called on drop
.
Panics
Panics if this handle does not own the file.
Example
use fslock::LockFile; let mut file = LockFile::open("mylock.test")?; file.lock()?; do_stuff(); file.unlock()?;
Panicking Example
use fslock::LockFile; let mut file = LockFile::open("mylock.test")?; file.unlock()?;
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for LockFile
impl Send for LockFile
impl Sync for LockFile
impl Unpin for LockFile
impl UnwindSafe for LockFile
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,