pub struct File { /* private fields */ }fs only.Expand description
A reference to an open file on the filesystem.
An instance of a File can be read and/or written depending on what options
it was opened with. The File type provides positional read and write
operations. The file does not maintain an internal cursor. The caller is
required to specify an offset when issuing an operation.
If you’d like to use methods from AsyncRead or
AsyncWrite traits, you can wrap File with
std::io::Cursor.
§Examples
use compio::fs::File;
use compio::buf::BufResult;
use std::io::Cursor;
let file = File::open("foo.txt").await?;
let cursor = Cursor::new(file);
let int = cursor.read_u32().await?;
let float = cursor.read_f32().await?;
let mut string = String::new();
let BufResult(result, string) = cursor.read_to_string(string).await;
let mut buf = vec![0; 1024];
let BufResult(result, buf) = cursor.read_exact(buf).await;Implementations§
Source§impl File
impl File
Sourcepub async fn open(path: impl AsRef<Path>) -> Result<File, Error>
pub async fn open(path: impl AsRef<Path>) -> Result<File, Error>
Attempts to open a file in read-only mode.
See the OpenOptions::open method for more details.
Sourcepub async fn create(path: impl AsRef<Path>) -> Result<File, Error>
pub async fn create(path: impl AsRef<Path>) -> Result<File, Error>
Opens a file in write-only mode.
This function will create a file if it does not exist, and will truncate it if it does.
See the OpenOptions::open function for more details.
Sourcepub fn close(self) -> impl Future<Output = Result<(), Error>>
pub fn close(self) -> impl Future<Output = Result<(), Error>>
Close the file. If the returned future is dropped before polling, the file won’t be closed.
Sourcepub async fn set_len(&self, size: u64) -> Result<(), Error>
Available on Unix only.
pub async fn set_len(&self, size: u64) -> Result<(), Error>
Truncates or extends the underlying file, updating the size of this file
to become size.
NOTE: On Linux kernel <= 6.9 or when io uring is disabled, the operation will be offloaded to the separate blocking thread
Sourcepub async fn metadata(&self) -> Result<Metadata, Error>
Available on Unix only.
pub async fn metadata(&self) -> Result<Metadata, Error>
Queries metadata about the underlying file.
Sourcepub async fn set_permissions(&self, perm: Permissions) -> Result<(), Error>
pub async fn set_permissions(&self, perm: Permissions) -> Result<(), Error>
Changes the permissions on the underlying file.
Sourcepub async fn sync_all(&self) -> Result<(), Error>
pub async fn sync_all(&self) -> Result<(), Error>
Attempts to sync all OS-internal metadata to disk.
This function will attempt to ensure that all in-memory data reaches the filesystem before returning.
Sourcepub async fn sync_data(&self) -> Result<(), Error>
pub async fn sync_data(&self) -> Result<(), Error>
This function is similar to sync_all, except that it might not
synchronize file metadata to the filesystem.
This is intended for use cases that must synchronize content, but don’t need the metadata on disk. The goal of this method is to reduce disk operations.
Note that some platforms may simply implement this in terms of
sync_all.
Trait Implementations§
Source§impl AsFd for File
Available on Unix only.
impl AsFd for File
Source§fn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
Source§impl AsyncReadAt for File
impl AsyncReadAt for File
Source§async fn read_at<T>(&self, buffer: T, pos: u64) -> BufResult<usize, T>where
T: IoBufMut,
async fn read_at<T>(&self, buffer: T, pos: u64) -> BufResult<usize, T>where
T: IoBufMut,
AsyncRead::read, except that it reads at a specified position.Source§async fn read_vectored_at<T>(&self, buffer: T, pos: u64) -> BufResult<usize, T>where
T: IoVectoredBufMut,
async fn read_vectored_at<T>(&self, buffer: T, pos: u64) -> BufResult<usize, T>where
T: IoVectoredBufMut,
AsyncRead::read_vectored, except that it reads at a specified
position.Source§impl AsyncReadManagedAt for File
impl AsyncReadManagedAt for File
Source§type Buffer<'a> = BorrowedBuffer<'a>
type Buffer<'a> = BorrowedBuffer<'a>
Source§type BufferPool = BufferPool
type BufferPool = BufferPool
Source§async fn read_managed_at<'a>(
&self,
buffer_pool: &'a <File as AsyncReadManagedAt>::BufferPool,
len: usize,
pos: u64,
) -> Result<<File as AsyncReadManagedAt>::Buffer<'a>, Error>
async fn read_managed_at<'a>( &self, buffer_pool: &'a <File as AsyncReadManagedAt>::BufferPool, len: usize, pos: u64, ) -> Result<<File as AsyncReadManagedAt>::Buffer<'a>, Error>
Self::BufferPool
and return a Self::Buffer. Read moreSource§impl AsyncWriteAt for &File
impl AsyncWriteAt for &File
Source§async fn write_at<T>(&mut self, buffer: T, pos: u64) -> BufResult<usize, T>where
T: IoBuf,
async fn write_at<T>(&mut self, buffer: T, pos: u64) -> BufResult<usize, T>where
T: IoBuf,
AsyncWrite::write, except that it writes at a specified
position.Source§async fn write_vectored_at<T>(
&mut self,
buffer: T,
pos: u64,
) -> BufResult<usize, T>where
T: IoVectoredBuf,
async fn write_vectored_at<T>(
&mut self,
buffer: T,
pos: u64,
) -> BufResult<usize, T>where
T: IoVectoredBuf,
AsyncWrite::write_vectored, except that it writes at a
specified position.Source§impl AsyncWriteAt for File
impl AsyncWriteAt for File
Source§async fn write_at<T>(&mut self, buf: T, pos: u64) -> BufResult<usize, T>where
T: IoBuf,
async fn write_at<T>(&mut self, buf: T, pos: u64) -> BufResult<usize, T>where
T: IoBuf,
AsyncWrite::write, except that it writes at a specified
position.Source§async fn write_vectored_at<T>(
&mut self,
buf: T,
pos: u64,
) -> BufResult<usize, T>where
T: IoVectoredBuf,
async fn write_vectored_at<T>(
&mut self,
buf: T,
pos: u64,
) -> BufResult<usize, T>where
T: IoVectoredBuf,
AsyncWrite::write_vectored, except that it writes at a
specified position.Source§impl Splittable for &File
impl Splittable for &File
Source§type ReadHalf = File
type ReadHalf = File
AsyncRead or
AsyncReadAt.Source§type WriteHalf = File
type WriteHalf = File
AsyncWrite or
AsyncWriteAt.Source§fn split(
self,
) -> (<&File as Splittable>::ReadHalf, <&File as Splittable>::WriteHalf)
fn split( self, ) -> (<&File as Splittable>::ReadHalf, <&File as Splittable>::WriteHalf)
self and returns a tuple containing separate read and write
halves. Read moreSource§impl Splittable for File
impl Splittable for File
Source§type ReadHalf = File
type ReadHalf = File
AsyncRead or
AsyncReadAt.Source§type WriteHalf = File
type WriteHalf = File
AsyncWrite or
AsyncWriteAt.Source§fn split(
self,
) -> (<File as Splittable>::ReadHalf, <File as Splittable>::WriteHalf)
fn split( self, ) -> (<File as Splittable>::ReadHalf, <File as Splittable>::WriteHalf)
self and returns a tuple containing separate read and write
halves. Read moreSharedFd.Auto Trait Implementations§
impl Freeze for File
impl RefUnwindSafe for File
impl Send for File
impl Sync for File
impl Unpin for File
impl UnwindSafe for File
Blanket Implementations§
§impl<T> AsSource for Twhere
T: AsFd,
impl<T> AsSource for Twhere
T: AsFd,
§fn source(&self) -> BorrowedFd<'_>
fn source(&self) -> BorrowedFd<'_>
§impl<A> AsyncReadAtExt for Awhere
A: AsyncReadAt + ?Sized,
impl<A> AsyncReadAtExt for Awhere
A: AsyncReadAt + ?Sized,
§async fn read_exact_at<T>(&self, buf: T, pos: u64) -> BufResult<(), T>where
T: IoBufMut,
async fn read_exact_at<T>(&self, buf: T, pos: u64) -> BufResult<(), T>where
T: IoBufMut,
buffer. Read more§async fn read_to_string_at(
&mut self,
buf: String,
pos: u64,
) -> BufResult<usize, String>
async fn read_to_string_at( &mut self, buf: String, pos: u64, ) -> BufResult<usize, String>
String until EOF in this source, placing them into
buffer.§async fn read_to_end_at<A>(
&self,
buffer: Vec<u8, A>,
pos: u64,
) -> BufResult<usize, Vec<u8, A>>where
A: Allocator + 'static,
async fn read_to_end_at<A>(
&self,
buffer: Vec<u8, A>,
pos: u64,
) -> BufResult<usize, Vec<u8, A>>where
A: Allocator + 'static,
buffer. Read more§async fn read_vectored_exact_at<T>(&self, buf: T, pos: u64) -> BufResult<(), T>where
T: IoVectoredBufMut,
async fn read_vectored_exact_at<T>(&self, buf: T, pos: u64) -> BufResult<(), T>where
T: IoVectoredBufMut,
AsyncReadExt::read_vectored_exact, expect that it reads at a
specified position.§impl<A> AsyncWriteAtExt for Awhere
A: AsyncWriteAt + ?Sized,
impl<A> AsyncWriteAtExt for Awhere
A: AsyncWriteAt + ?Sized,
§async fn write_all_at<T>(&mut self, buf: T, pos: u64) -> BufResult<(), T>where
T: IoBuf,
async fn write_all_at<T>(&mut self, buf: T, pos: u64) -> BufResult<(), T>where
T: IoBuf,
AsyncWriteAt::write_at, except that it tries to write the
entire contents of the buffer into this writer.§async fn write_vectored_all_at<T>(
&mut self,
buf: T,
pos: u64,
) -> BufResult<(), T>where
T: IoVectoredBuf,
async fn write_vectored_all_at<T>(
&mut self,
buf: T,
pos: u64,
) -> BufResult<(), T>where
T: IoVectoredBuf,
AsyncWriteAt::write_vectored_at, expect that it tries to write
the entire contents of the buffer into this writer.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more