Struct CFile

Source
pub struct CFile(/* private fields */);
Expand description

A reference to an open stream on the filesystem.

An instance of a CFile can be read and/or written depending on what modes it was opened with. CFile also implement Seek to alter the logical cursor that the file contains internally.

Implementations§

Source§

impl CFile

Source

pub fn fdopen<S: AsRef<str>>(fd: RawFd, mode: S) -> Result<CFile>

Source§

impl CFile

Source

pub fn unlocked(self) -> Unlocked

Returns a unlocked CFile stream except that they do not use locking.

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 AsMut<CFileRef> for CFile

Source§

fn as_mut(&mut self) -> &mut CFileRef

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsRef<CFileRef> for CFile

Source§

fn as_ref(&self) -> &CFileRef

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Borrow<CFileRef> for CFile

Source§

fn borrow(&self) -> &CFileRef

Immutably borrows from an owned value. Read more
Source§

impl BorrowMut<CFileRef> for CFile

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl Deref for CFile

Source§

type Target = CFileRef

The resulting type after dereferencing.
Source§

fn deref(&self) -> &CFileRef

Dereferences the value.
Source§

impl DerefMut for CFile

Source§

fn deref_mut(&mut self) -> &mut CFileRef

Mutably dereferences the value.
Source§

impl Drop for CFile

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl ForeignType for CFile

Source§

type CType = FILE

The raw C type.
Source§

type Ref = CFileRef

The type representing a reference to this type.
Source§

unsafe fn from_ptr(ptr: *mut FILE) -> CFile

Constructs an instance of this type from its raw type. Read more
Source§

fn as_ptr(&self) -> *mut FILE

Returns a raw pointer to the wrapped value.
Source§

fn into_ptr(self) -> *mut Self::CType

Consumes the wrapper and returns the raw pointer.
Source§

impl IntoRawFd for CFile

Source§

fn into_raw_fd(self) -> RawFd

Consumes this object, returning the raw underlying file descriptor. Read more
Source§

impl Read for CFile

Source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · Source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
Source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · Source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · Source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · Source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Reads the exact number of bytes required to fill buf. Read more
Source§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
Source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Reads the exact number of bytes required to fill cursor. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · Source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · Source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · Source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
Source§

impl Seek for CFile

Source§

fn seek(&mut self, pos: SeekFrom) -> Result<u64>

Seek to an offset, in bytes, in a stream. Read more
1.55.0 · Source§

fn rewind(&mut self) -> Result<(), Error>

Rewind to the beginning of a stream. Read more
Source§

fn stream_len(&mut self) -> Result<u64, Error>

🔬This is a nightly-only experimental API. (seek_stream_len)
Returns the length of this stream (in bytes). Read more
1.51.0 · Source§

fn stream_position(&mut self) -> Result<u64, Error>

Returns the current seek position from the start of the stream. Read more
1.80.0 · Source§

fn seek_relative(&mut self, offset: i64) -> Result<(), Error>

Seeks relative to the current position. Read more
Source§

impl Write for CFile

Source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<()>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more
Source§

impl Send for CFile

Auto Trait Implementations§

§

impl Freeze for CFile

§

impl RefUnwindSafe for CFile

§

impl !Sync for CFile

§

impl Unpin for CFile

§

impl UnwindSafe for CFile

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<S> IntoStream for S
where S: IntoRawFd,

Source§

fn into_stream<S: AsRef<str>>(self, mode: S) -> Result<CFile>

Consumes this raw fd, returning the raw underlying C *FILE stream.
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.