Struct linux_io::File

source ·
#[repr(transparent)]
pub struct File { /* private fields */ }
Expand description

An encapsulated Linux file descriptor.

The methods of File are largely just thin wrappers around Linux system calls that work with file descriptors. Aside from type conversions to make the API safer and more ergonomic there are no additional userspace abstractions such as buffering.

When the std crate feature is enabled, a File also implements the std:io traits Read, Write, and Seek.

Implementations§

source§

impl File

source

pub unsafe fn from_raw_fd(fd: int) -> Self

Wrap an existing raw file descriptor into a File.

Safety:

  • The given file descriptor must not belong to an active standard library file or any similar wrapping abstraction.
  • The file descriptor must remain open and valid for the full lifetime of the File object.
  • The same file descriptor must not be wrapped in instances of File, because the first one to be dropped will close the file descriptor.
source

pub fn create_raw(path: &[u8], mode: mode_t) -> Result<Self>

Create a new file using the creat system call.

This function exposes the raw mode argument from the underlying system call, which the caller must populate appropriately.

source

pub fn open_raw(path: &[u8], flags: int, mode: mode_t) -> Result<Self>

Open a file using the open system call.

This function exposes the raw flags and mode arguments from the underlying system call, which the caller must populate appropriately.

source

pub fn into_raw_fd(self) -> int

Consumes the file object and returns the underlying file descriptor without closing it.

source

pub fn close(self) -> Result<()>

Consumes the file object and closes the underlying file descriptor.

If close fails then the file descriptor is always leaked, because there is no way to recover it once consumed.

source

pub unsafe fn close_mut(&mut self) -> Result<()>

Closes the underlying file descriptor without consuming it.

Safety:

  • Callers must pass the file to core::mem::forget immediately after calling this function to prevent the implicit close in the Drop implementation.
  • Callers must not use the file object again after calling this method; file descriptor will either be dangling or will be referring to some other unrelated file.
source

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

Read some bytes from the file into the given buffer, returning the number of bytes that were read.

source

pub unsafe fn read_raw(&mut self, buf: *mut void, count: size_t) -> Result<size_t>

A thin wrapper around the raw read system call against this file’s file descriptor.

Use File::read as a safe alternative.

source

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

Change the current read/write position of the file.

source

pub fn sync(&mut self) -> Result<()>

Tell the kernel to flush any in-memory buffers and caches for the file.

source

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

Write bytes from the given buffer to the file, returning how many bytes were written.

source

pub unsafe fn write_raw( &mut self, buf: *const void, count: size_t ) -> Result<size_t>

A thin wrapper around the raw write system call against this file’s file descriptor.

Use File::write as a safe alternative.

Trait Implementations§

source§

impl Drop for File

source§

fn drop(&mut self)

Attempts to close the file when it’s no longer in scope.

This implicit close ignores errors, which might cause data loss if the final commit of data to disk fails. Use File::close explicitly if you need to detect errors.

source§

impl FromRawFd for File

source§

unsafe fn from_raw_fd(fd: RawFd) -> Self

Constructs a new instance of Self from the given raw file descriptor. Read more
source§

impl IntoRawFd for File

source§

fn into_raw_fd(self) -> RawFd

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

impl Read for File

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, Global>) -> Result<usize, Error>

Read 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>

Read 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>

Read 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)
Read the exact number of bytes required to fill cursor. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Selfwhere 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 File

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
source§

impl Write for File

File implements core::fmt::Write by passing UTF-8 encoded bytes directly to the write method.

source§

fn write_str(&mut self, s: &str) -> Result

Writes a string slice into this writer, returning whether the write succeeded. Read more
1.1.0 · source§

fn write_char(&mut self, c: char) -> Result<(), Error>

Writes a char into this writer, returning whether the write succeeded. Read more
1.0.0 · source§

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

Glue for usage of the write! macro with implementors of this trait. Read more
source§

impl Write for File

source§

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

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

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

Flush 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, fmt: 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 Selfwhere Self: Sized,

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

Auto Trait Implementations§

§

impl RefUnwindSafe for File

§

impl Send for File

§

impl Sync for File

§

impl Unpin for File

§

impl UnwindSafe for File

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.