Trait cfile::FileLockExt [] [src]

pub trait FileLockExt {
    fn lock(&mut self) -> FileLock;
    fn try_lock(&mut self) -> Option<FileLock>;
    fn unlock(&self);
}

Extension methods for CFile lock.

Required Methods

fn lock(&mut self) -> FileLock

acquires an exclusive lock on the specified object.

If another thread has already locked the object, will block until the lock is released.

Examples

use std::io::Write;
use cfile::{tmpfile, FileLockExt};

let mut f = tmpfile().unwrap();
let mut l = f.lock();

assert_eq!(l.write(b"test").unwrap(), 4);

fn try_lock(&mut self) -> Option<FileLock>

a non-blocking version of lock();

if the lock cannot be acquired immediately, try_lock() returns None instead of blocking.

Examples

use std::io::{Read, Write, BufRead, BufReader, Seek, SeekFrom};
use cfile::{tmpfile, FileLockExt};

let mut f = tmpfile().unwrap();

if let Some(mut c) = f.try_lock() {
    assert_eq!(c.write(b"test").unwrap(), 4);
}

assert_eq!(f.seek(SeekFrom::Start(0)).unwrap(), 0); // seek to the beginning of stream

let mut r = BufReader::new(f);
let mut s = String::new();
assert_eq!(r.read_line(&mut s).unwrap(), 4); // read back the text
assert_eq!(s, "test");

fn unlock(&self)

releases the lock on an object acquired by an earlier call to lock() or try_lock().

Implementors