Struct file_lock::FileLock[][src]

pub struct FileLock {
    pub file: File,
}

Represents the actually locked file

Fields

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

Methods

impl 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

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

Examples

extern crate file_lock;

use file_lock::FileLock;
use std::io::prelude::*;

fn main() {
    let should_we_block  = true;
    let lock_for_writing = true;

    let mut filelock = match FileLock::lock("myfile.txt", should_we_block, lock_for_writing) {
        Ok(lock) => lock,
        Err(err) => panic!("Error getting write lock: {}", err),
    };

    filelock.file.write_all(b"Hello, World!").is_ok();
}

Unlock our locked file

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

Examples

extern crate file_lock;

use file_lock::FileLock;
use std::io::prelude::*;

fn main() {
    let should_we_block  = true;
    let lock_for_writing = true;

    let mut filelock = match FileLock::lock("myfile.txt", should_we_block, lock_for_writing) {
        Ok(lock) => lock,
        Err(err) => panic!("Error getting write lock: {}", err),
    };

    filelock.file.write_all(b"Hello, World!").is_ok();

    match filelock.unlock() {
        Ok(_)    => println!("Successfully unlocked the file"),
        Err(err) => panic!("Error unlocking the file: {}", err),
    };
}

Trait Implementations

impl Debug for FileLock
[src]

Formats the value using the given formatter. Read more

impl Drop for FileLock
[src]

Executes the destructor for this type. Read more

Auto Trait Implementations

impl Send for FileLock

impl Sync for FileLock