[][src]Struct file_locker::FileLock

pub struct FileLock {
    pub file: File,
}

Represents the actually locked file

Fields

file: File

the std::fs::File of the file that's locked

Implementations

impl FileLock[src]

pub fn new<T: AsRef<Path>>(file_path: T) -> FileLockBuilder<T>[src]

Create a FileLockBuilder

blocking and writeable default to false

Examples

use file_locker::FileLock;
use std::io::prelude::*;
use std::io::Result;

fn main() -> Result<()> {
    let mut filelock = FileLock::new("myfile.txt")
                     .writeable(true)
                     .blocking(true)
                     .lock()?;

    filelock.file.write_all(b"Hello, world")?;
    Ok(())
}

pub fn lock(
    file_path: impl AsRef<Path>,
    blocking: bool,
    writeable: bool
) -> Result<FileLock>
[src]

Try to lock the specified file

Parameters

  • filename is the path of the file we want to lock on

  • is_blocking is a flag to indicate if we should block if it's already locked

If set, this call will block until the lock can be obtained.
If not set, this call will return immediately, giving an error if it would block

  • is_writable is a flag to indicate if we want to lock for writing

Examples

use file_locker::FileLock;
use std::io::prelude::*;
use std::io::Result;

fn main() -> Result<()> {
    let mut filelock = FileLock::lock("myfile.txt", false, false)?;

    let mut buf = String::new();
    filelock.file.read_to_string(&mut buf)?;
    Ok(())
}

pub fn unlock(&self) -> Result<()>[src]

Unlock our locked file

Note: This method is optional as the file lock will be unlocked automatically when dropped

Examples

use file_locker::FileLock;
use std::io::prelude::*;
use std::io::Result;

fn main() -> Result<()> {
    let mut filelock = FileLock::new("myfile.txt")
                     .writeable(true)
                     .blocking(true)
                     .lock()?;

    filelock.file.write_all(b"Hello, world")?;

    filelock.unlock()?;
    Ok(())
}

Trait Implementations

impl AsRawFd for FileLock[src]

impl Debug for FileLock[src]

impl Drop for FileLock[src]

impl FileExt for FileLock[src]

impl Read for FileLock[src]

impl Seek for FileLock[src]

impl Write for FileLock[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.