#[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
impl File
sourcepub unsafe fn from_raw_fd(fd: int) -> Self
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.
sourcepub fn create_raw(path: &[u8], mode: mode_t) -> Result<Self>
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.
sourcepub fn open_raw(path: &[u8], flags: int, mode: mode_t) -> Result<Self>
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.
sourcepub fn into_raw_fd(self) -> int
pub fn into_raw_fd(self) -> int
Consumes the file object and returns the underlying file descriptor without closing it.
sourcepub fn close(self) -> Result<()>
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.
sourcepub unsafe fn close_mut(&mut self) -> Result<()>
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 implicitclose
in theDrop
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.
sourcepub fn read(&mut self, buf: &mut [u8]) -> Result<usize>
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.
sourcepub unsafe fn read_raw(&mut self, buf: *mut void, count: size_t) -> Result<size_t>
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.
sourcepub fn seek(&mut self, pos: impl Into<SeekFrom>) -> Result<u64>
pub fn seek(&mut self, pos: impl Into<SeekFrom>) -> Result<u64>
Change the current read/write position of the file.
sourcepub fn sync(&mut self) -> Result<()>
pub fn sync(&mut self) -> Result<()>
Tell the kernel to flush any in-memory buffers and caches for the file.
Trait Implementations§
source§impl Drop for File
impl Drop for File
source§fn drop(&mut self)
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
impl FromRawFd for File
source§unsafe fn from_raw_fd(fd: RawFd) -> Self
unsafe fn from_raw_fd(fd: RawFd) -> Self
Self
from the given raw file
descriptor. Read moresource§impl IntoRawFd for File
impl IntoRawFd for File
source§fn into_raw_fd(self) -> RawFd
fn into_raw_fd(self) -> RawFd
source§impl Read for File
impl Read for File
source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moresource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · source§fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
buf
. Read more1.0.0 · source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moresource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere Self: Sized,
Read
. Read moresource§impl Seek for File
impl Seek for File
source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
1.55.0 · source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
source§impl Write for File
impl Write for File
File
implements core::fmt::Write
by passing UTF-8 encoded bytes
directly to the write
method.
source§impl Write for File
impl Write for File
source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)