Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use futures::prelude::*;
- use tokio::prelude::*;
- use std::{io, path::PathBuf};
- type BoxedFutureFileSize = Box<dyn Future<Item = u64, Error = io::Error>>;
- fn recursive_size(path: PathBuf) -> impl Future<Item = u64, Error = io::Error> {
- tokio::fs::symlink_metadata(path.clone())
- .and_then::<_, BoxedFutureFileSize>(|metadata| {
- if metadata.is_file() {
- Box::new(future::ok(metadata.len()))
- } else {
- let sizes = tokio::fs::read_dir(path)
- .flatten_stream()
- .and_then(|entry| recursive_size(entry.path()));
- Box::new(sizes.fold(0, |acc, x| future::ok::<_, io::Error>(acc + x)))
- }
- })
- }
Add Comment
Please, Sign In to add comment