Struct FileLock

Source
pub struct FileLock<'a>(/* private fields */);
Expand description

A locked reference to the CFile stream.

Methods from Deref<Target = CFileRef>§

Source

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.

Source

pub fn bytes(&self) -> Bytes<'_>

An iterator over the bytes of a *FILE stream.

Source

pub fn lines(&self) -> Lines<Bytes<'_>>

An iterator over the lines of a *FILE stream.

Source

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);
Source

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§

Source§

impl<'a> Deref for FileLock<'a>

Source§

type Target = CFileRef

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'a> DerefMut for FileLock<'a>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<'a> Drop for FileLock<'a>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.