file_operation/read/async/
fn.rs1use std::path::Path;
2use tokio::fs::*;
3use tokio::io::AsyncReadExt;
4
5pub async fn async_read_from_file<T>(file_path: &str) -> Result<T, Box<dyn std::error::Error>>
15where
16 T: From<Vec<u8>>,
17{
18 let path: &Path = Path::new(file_path);
19 let mut file: File = File::open(path).await?;
20 let mut content: Vec<u8> = Vec::new();
21 file.read_to_end(&mut content).await?;
22 Ok(T::from(content))
23}
24
25pub async fn async_get_file_size(file_path: &str) -> Option<u64> {
35 metadata(file_path)
36 .await
37 .and_then(|metadata| Ok(Some(metadata.len())))
38 .unwrap_or(None)
39}