Guest User

Untitled

a guest
Dec 14th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. use futures::prelude::*;
  2. use tokio::prelude::*;
  3.  
  4. use std::{io, path::PathBuf};
  5.  
  6. type BoxedFutureFileSize = Box<dyn Future<Item = u64, Error = io::Error>>;
  7.  
  8. fn recursive_size(path: PathBuf) -> impl Future<Item = u64, Error = io::Error> {
  9. tokio::fs::symlink_metadata(path.clone())
  10. .and_then::<_, BoxedFutureFileSize>(|metadata| {
  11. if metadata.is_file() {
  12. Box::new(future::ok(metadata.len()))
  13. } else {
  14. let sizes = tokio::fs::read_dir(path)
  15. .flatten_stream()
  16. .and_then(|entry| recursive_size(entry.path()));
  17. Box::new(sizes.fold(0, |acc, x| future::ok::<_, io::Error>(acc + x)))
  18. }
  19. })
  20. }
Add Comment
Please, Sign In to add comment