Skip to main content

File

Struct File 

Source
pub struct File { /* private fields */ }
Expand description

Async file handle supporting stateful streams and positioned reads.

Implementations§

Source§

impl File

Source

pub async fn open<P>(path: P) -> Result<File, Error>
where P: AsRef<Path>,

Open a file with default options (read-only)

Source

pub async fn create<P>(path: P) -> Result<File, Error>
where P: AsRef<Path>,

Create a new file for writing (truncates if exists)

Source

pub async fn open_with_options<P>( path: P, options: FileOpenOptions, ) -> Result<File, Error>
where P: AsRef<Path>,

Open a file with custom options

Source

pub async fn read_to_string(&mut self) -> Result<String, Error>

Read entire file contents into a string

Source

pub async fn read_to_end(&mut self) -> Result<Vec<u8>, Error>

Read entire file contents into a byte vector

Source

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

Read data into a buffer

Source

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

Write data from a buffer

Source

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

Write all data from a buffer.

The whole buffer is one pool job: once submitted, it is written in full even if this future is dropped.

Source

pub async fn write_str(&mut self, s: &str) -> Result<(), Error>

Write a string to the file

Source

pub async fn flush(&mut self) -> Result<(), Error>

Flush any buffered data to disk

Source

pub async fn sync_all(&mut self) -> Result<(), Error>

Synchronize all data and metadata to disk

Source

pub async fn sync_data(&mut self) -> Result<(), Error>

Synchronize data (but not metadata) to disk

Source

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

Seek to a specific position in the file

Source

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

Get current position in the file

Source

pub async fn metadata(&self) -> Result<Metadata, Error>

Get file metadata, after every stream operation already queued on this handle has finished.

Source

pub fn path(&self) -> &Path

Get file path

Source

pub fn stats(&self) -> &FileStats

Get statistics for stateful stream operations.

Positioned reads do not advance the stream and are not included.

Trait Implementations§

Source§

impl AsyncLength for File

Source§

async fn len(&self) -> Result<u64, Error>

Return the total number of readable bytes.
Source§

fn is_empty(&self) -> impl Future<Output = Result<bool, Error>> + Send

Whether the source has no readable bytes. Read more
Source§

impl AsyncRead for File

Source§

fn poll_read( self: Pin<&mut File>, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll<Result<usize, Error>>

Attempt to read from the source into the provided buffer.
Source§

impl AsyncReadAt for File

Source§

async fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<(), Error>

Read exactly buf.len() bytes beginning at offset.
Source§

impl AsyncWrite for File

Source§

fn poll_write( self: Pin<&mut File>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize, Error>>

Attempt to write bytes from the buffer to the destination.
Source§

fn poll_flush( self: Pin<&mut File>, cx: &mut Context<'_>, ) -> Poll<Result<(), Error>>

Attempt to flush pending writes.
Source§

fn poll_shutdown( self: Pin<&mut File>, cx: &mut Context<'_>, ) -> Poll<Result<(), Error>>

Attempt to close/shutdown the write side of the stream.
Source§

fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>], ) -> Poll<Result<usize, Error>>

Attempt to write bytes from several buffers in one operation. Read more
Source§

fn is_write_vectored(&self) -> bool

Whether poll_write_vectored is cheaper than one poll_write per buffer.

Auto Trait Implementations§

§

impl !RefUnwindSafe for File

§

impl !UnwindSafe for File

§

impl Freeze for File

§

impl Send for File

§

impl Sync for File

§

impl Unpin for File

§

impl UnsafeUnpin for File

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<R> AsyncReadExt for R
where R: AsyncRead + ?Sized,

Source§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self> ⓘ
where Self: Unpin,

Read some bytes asynchronously from the reader.
Source§

fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self> ⓘ
where Self: Unpin,

Read exactly enough bytes to fill the provided buffer.
Source§

impl<W> AsyncWriteExt for W
where W: AsyncWrite + ?Sized,

Source§

fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self> ⓘ
where Self: Unpin,

Write some bytes asynchronously.
Source§

fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAll<'a, Self> ⓘ
where Self: Unpin,

Write all bytes asynchronously.
Source§

fn flush(&mut self) -> Flush<'_, Self> ⓘ
where Self: Unpin,

Flush pending writes asynchronously.
Source§

fn shutdown(&mut self) -> Shutdown<'_, Self> ⓘ
where Self: Unpin,

Shutdown the write side of the stream.
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<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.