I want to write a program which recursively deletes a directory with async functions in Rust 1.39.
As a first step I have tried the following code, but it doesn't compile:
use std::env;
use failure::Error;
use futures::Future;
use std::path::PathBuf;
use tokio::prelude::*;
fn walk(path: PathBuf) -> Box<dyn Future<Item = (), Error = Error> + Send> {
let task = tokio::fs::read_dir(path)
.flatten_stream()
.for_each(move |entry| {
let filepath = entry.path();
if filepath.is_dir() {
future::Either::A(walk(filepath))
} else {
println!("File: {:?}", filepath);
future::Either::B(future::ok(()))
}
})
.map_err(Error::from)
.and_then(|_| {
println!("All tasks done");
});
Box::new(task)
}
fn main() -> Result<(), std::io::Error> {
let args: Vec<String> = env::args().collect();
let dir = &args[1];
let t = walk(PathBuf::from(&dir)).map_err(drop);
tokio::run(t);
Ok(())
}
When I run cargo build
I get the following output:
error[E0220]: associated type `Item` not found for `core::future::future::Future`
--> src\main.rs:10:42
|
10 | fn walk(path: PathBuf) -> Box<dyn Future<Item = (), Error = Error> + Send> {
| ^^^^^^^^^ associated type `Item` not found
error[E0220]: associated type `Error` not found for `core::future::future::Future`
--> src\main.rs:10:53
|
10 | fn walk(path: PathBuf) -> Box<dyn Future<Item = (), Error = Error> + Send> {
| ^^^^^^^^^^^^^ associated type `Error` not found
error[E0191]: the value of the associated type `Output` (from the trait `core::future::future::Future`) must be specified
--> src\main.rs:10:31
|
10 | fn walk(path: PathBuf) -> Box<dyn Future<Item = (), Error = Error> + Send> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ associated type `Output` must be specified
Cargo.toml:
[dependencies]
async-std = "1.0.1"
failure = "0.1.6"
futures = "0.3.1"
tokio = "0.1.22"
Any help?