Struct file_lock::FileLock [−][src]
pub struct FileLock { pub file: File, }
Represents the actually locked file
Fields
file: File
the std::fs::File
of the file that's locked
Methods
impl FileLock
[src]
impl FileLock
pub fn lock(
filename: &str,
is_blocking: bool,
is_writable: bool
) -> Result<FileLock, Error>
[src]
pub fn lock(
filename: &str,
is_blocking: bool,
is_writable: bool
) -> Result<FileLock, Error>
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(); }
pub fn unlock(&self) -> Result<(), Error>
[src]
pub fn unlock(&self) -> Result<(), Error>
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]
impl Debug for FileLock
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl Drop for FileLock
[src]
impl Drop for FileLock