[−][src]Struct async_std::fs::File
A reference to a file on the filesystem.
An instance of a File
can be read and/or written depending on what options it was opened
with.
Files are automatically closed when they go out of scope. Errors detected on closing are
ignored by the implementation of Drop
. Use the method sync_all
if these errors must be
manually handled.
This type is an async version of std::fs::File
.
Examples
Create a new file and write some bytes to it:
use async_std::fs::File; use async_std::prelude::*; let mut file = File::create("a.txt").await?; file.write_all(b"Hello, world!").await?;
Read the contents of a file into a Vec<u8>
:
use async_std::fs::File; use async_std::prelude::*; let mut file = File::open("a.txt").await?; let mut contents = Vec::new(); file.read_to_end(&mut contents).await?;
Methods
impl File
[src]
pub async fn open<P: AsRef<Path>>(path: P) -> Result<File>
[src]
Opens a file in read-only mode.
See the OpenOptions::open
method for more details.
Errors
This function will return an error if path
does not already exist.
Other errors may also be returned according to OpenOptions::open
.
Examples
use async_std::fs::File; let file = File::open("a.txt").await?;
pub async fn create<P: AsRef<Path>>(path: P) -> Result<File>
[src]
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.
Examples
use async_std::fs::File; let file = File::create("a.txt").await?;
pub async fn sync_all<'_>(&'_ self) -> Result<()>
[src]
Attempts to synchronize all OS-internal metadata to disk.
This function will attempt to ensure that all in-memory data reaches the filesystem before returning.
This can be used to handle errors that would otherwise only be caught when the File
is
closed. Dropping a file will ignore errors in synchronizing this in-memory data.
Examples
use async_std::fs::File; use async_std::prelude::*; let mut file = File::create("a.txt").await?; file.write_all(b"Hello, world!").await?; file.sync_all().await?;
pub async fn sync_data<'_>(&'_ self) -> Result<()>
[src]
Similar to sync_all
, except that it may not synchronize file metadata.
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
.
Examples
use async_std::fs::File; use async_std::prelude::*; let mut file = File::create("a.txt").await?; file.write_all(b"Hello, world!").await?; file.sync_data().await?;
pub async fn set_len<'_>(&'_ self, size: u64) -> Result<()>
[src]
Truncates or extends the underlying file.
If the size
is less than the current file's size, then the file will be truncated. If it
is greater than the current file's size, then the file will be extended to size
and have
all of the intermediate data filled in with zeros.
The file's cursor isn't changed. In particular, if the cursor was at the end and the file is truncated using this operation, the cursor will now be past the end.
Errors
This function will return an error if the file is not opened for writing.
Examples
use async_std::fs::File; let file = File::create("a.txt").await?; file.set_len(10).await?;
pub async fn metadata<'_>(&'_ self) -> Result<Metadata>
[src]
Queries metadata about the file.
Examples
use async_std::fs::File; let file = File::open("a.txt").await?; let metadata = file.metadata().await?;
pub async fn set_permissions<'_>(&'_ self, perm: Permissions) -> Result<()>
[src]
Changes the permissions on the underlying file.
Errors
This function will return an error if the user lacks permission to change attributes on the underlying file, but may also return an error in other OS-specific cases.
Examples
use async_std::fs::File; let file = File::create("a.txt").await?; let mut perms = file.metadata().await?.permissions(); perms.set_readonly(true); file.set_permissions(perms).await?;
Trait Implementations
impl AsRawFd for File
[src]
impl FromRawFd for File
[src]
unsafe fn from_raw_fd(fd: RawFd) -> File
[src]
impl IntoRawFd for File
[src]
fn into_raw_fd(self) -> RawFd
[src]
impl AsRawHandle for File
[src]
fn as_raw_handle(&self) -> RawHandle
[src]
impl FromRawHandle for File
[src]
unsafe fn from_raw_handle(handle: RawHandle) -> File
[src]
impl IntoRawHandle for File
[src]
fn into_raw_handle(self) -> RawHandle
[src]
impl Drop for File
[src]
impl From<File> for File
[src]
impl Debug for File
[src]
impl AsyncRead for File
[src]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize>>
unsafe fn initializer(&self) -> Initializer
[src]
fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize, Error>>
impl<'_> AsyncRead for &'_ File
[src]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize>>
unsafe fn initializer(&self) -> Initializer
[src]
fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize, Error>>
impl AsyncWrite for File
[src]
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<()>>
[src]
fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<()>>
[src]
fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &[IoSlice]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &[IoSlice]
) -> Poll<Result<usize, Error>>
impl<'_> AsyncWrite for &'_ File
[src]
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<()>>
[src]
fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<()>>
[src]
fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &[IoSlice]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &[IoSlice]
) -> Poll<Result<usize, Error>>
impl AsyncSeek for File
[src]
impl<'_> AsyncSeek for &'_ File
[src]
Auto Trait Implementations
impl Send for File
impl Unpin for File
impl Sync for File
impl !RefUnwindSafe for File
impl !UnwindSafe for File
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
ⓘImportant traits for &'_ mut Ffn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,