Sha256: 055ae8b6ae96ebae2d05f8780e7592bb587a742506c6df8ee8b380fc7d2820ef

Contents?: true

Size: 1.67 KB

Versions: 19

Compression:

Stored size: 1.67 KB

Contents

use crate::fs::asyncify;

use std::{io, path::Path};

/// Reads the entire contents of a file into a bytes vector.
///
/// This is an async version of [`std::fs::read`][std]
///
/// [std]: std::fs::read
///
/// This is a convenience function for using [`File::open`] and [`read_to_end`]
/// with fewer imports and without an intermediate variable. It pre-allocates a
/// buffer based on the file size when available, so it is generally faster than
/// reading into a vector created with `Vec::new()`.
///
/// This operation is implemented by running the equivalent blocking operation
/// on a separate thread pool using [`spawn_blocking`].
///
/// [`File::open`]: super::File::open
/// [`read_to_end`]: crate::io::AsyncReadExt::read_to_end
/// [`spawn_blocking`]: crate::task::spawn_blocking
///
/// # Errors
///
/// This function will return an error if `path` does not already exist.
/// Other errors may also be returned according to [`OpenOptions::open`].
///
/// [`OpenOptions::open`]: super::OpenOptions::open
///
/// It will also return an error if it encounters while reading an error
/// of a kind other than [`ErrorKind::Interrupted`].
///
/// [`ErrorKind::Interrupted`]: std::io::ErrorKind::Interrupted
///
/// # Examples
///
/// ```no_run
/// use tokio::fs;
/// use std::net::SocketAddr;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
///     let contents = fs::read("address.txt").await?;
///     let foo: SocketAddr = String::from_utf8_lossy(&contents).parse()?;
///     Ok(())
/// }
/// ```
pub async fn read(path: impl AsRef<Path>) -> io::Result<Vec<u8>> {
    let path = path.as_ref().to_owned();
    asyncify(move || std::fs::read(path)).await
}

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
wasmtime-14.0.4 ./ext/cargo-vendor/tokio-1.33.0/src/fs/read.rs
wasmtime-14.0.3 ./ext/cargo-vendor/tokio-1.33.0/src/fs/read.rs
wasmtime-14.0.1 ./ext/cargo-vendor/tokio-1.33.0/src/fs/read.rs
wasmtime-14.0.0 ./ext/cargo-vendor/tokio-1.33.0/src/fs/read.rs
wasmtime-13.0.0 ./ext/cargo-vendor/tokio-1.32.0/src/fs/read.rs
wasmtime-12.0.1 ./ext/cargo-vendor/tokio-1.32.0/src/fs/read.rs
wasmtime-12.0.0 ./ext/cargo-vendor/tokio-1.32.0/src/fs/read.rs
wasmtime-11.0.0 ./ext/cargo-vendor/tokio-1.32.0/src/fs/read.rs
wasmtime-10.0.1 ./ext/cargo-vendor/tokio-1.30.0/src/fs/read.rs
wasmtime-10.0.0 ./ext/cargo-vendor/tokio-1.30.0/src/fs/read.rs
wasmtime-9.0.4 ./ext/cargo-vendor/tokio-1.30.0/src/fs/read.rs
wasmtime-9.0.1 ./ext/cargo-vendor/tokio-1.28.1/src/fs/read.rs
wasmtime-8.0.0 ./ext/cargo-vendor/tokio-1.27.0/src/fs/read.rs
wasmtime-7.0.0 ./ext/cargo-vendor/tokio-1.27.0/src/fs/read.rs
wasmtime-6.0.1 ./ext/cargo-vendor/tokio-1.25.0/src/fs/read.rs
wasmtime-6.0.0 ./ext/cargo-vendor/tokio-1.25.0/src/fs/read.rs
wasmtime-5.0.0 ./ext/cargo-vendor/tokio-1.24.2/src/fs/read.rs
wasmtime-0.4.1 ./ext/cargo-vendor/tokio-1.23.0/src/fs/read.rs
wasmtime-0.4.0 ./ext/cargo-vendor/tokio-1.23.0/src/fs/read.rs