pub struct FileLock<'a>(/* private fields */);
Expand description
A locked reference to the CFile
stream.
Methods from Deref<Target = CFileRef>§
Sourcepub fn reopen(&self, filename: &str, mode: &str) -> Result<CFile>
pub fn reopen(&self, filename: &str, mode: &str) -> Result<CFile>
opens the file whose name is the string pointed to by filename
and associates the stream pointed to by stream with it.
The original stream (if it exists) is closed.
The mode argument is used just as in the open()
function.
Sourcepub fn lock(&mut self) -> FileLock<'_>
pub 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;
let mut f = tmpfile().unwrap();
let mut l = f.lock();
assert_eq!(l.write(b"test").unwrap(), 4);
Sourcepub fn try_lock(&mut self) -> Option<FileLock<'_>>
pub 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;
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");
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for FileLock<'a>
impl<'a> !RefUnwindSafe for FileLock<'a>
impl<'a> Send for FileLock<'a>
impl<'a> !Sync for FileLock<'a>
impl<'a> Unpin for FileLock<'a>
impl<'a> !UnwindSafe for FileLock<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more