Home > Blockchain >  items from traits can only be used if the trait is in scope
items from traits can only be used if the trait is in scope

Time:11-02

I am learning futures and threads in rust with this example, but this does not compile and I do not understand why.

use futures::future::Future;
use futures_cpupool::{CpuFuture, CpuPool};
use notify::{RecursiveMode, Watcher, watcher};

use std::{thread, time};

fn main() {
    let pool = CpuPool::new(10);

    let mut handles = Vec::new();

    for i in 0..10 {
        let i = i.clone();
        let handle: CpuFuture<(), ()> = pool.spawn_fn(move || {
            loop {
                println!("{}", i);

            }
            Ok(())
        });

        handles.push(handle);
    }

    thread::sleep(time::Duration::from_secs(2));

     for handle in handles {
         handle.wait().unwrap();
     }
}

error:

error[E0599]: no method named `wait` found for struct `CpuFuture` in the current scope
   --> src/main.rs:28:17
    |
28  |          handle.wait().unwrap();
    |                 ^^^^ method not found in `CpuFuture<(), ()>`
    |
   ::: /home/placek/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.31/src/future/mod.rs:297:8
    |
297 |     fn wait(self) -> result::Result<Self::Item, Self::Error>
    |        ---- the method is available for `CpuFuture<(), ()>` here
    |
    = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
    |
2   | use futures::future::Future;
    |

I have Future in scope so I think it should work, also I checked that CpuFuture implements Future trait.

Here is my cargo.toml:

[package]
name = "folderWatcher"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
notify = "4.0.12"
futures-cpupool = "0.1.7"
futures = "0.3.25"

Also I want to ask if it is required to explicitly start each Future?

CodePudding user response:

futures-cpupool requires futures version 0.1, and version 0.3.25 is incompatible with it. So downgrade futures:

futures = "0.1"

However, the real problem is futures-cpupool being too old, as @isaactfa said.

  • Related