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