Using Tokio 0.2 Alpha and Nightly

I'm trying to experiment with the new Tokio 0.2 alpha and async/await in nightly Rust.

In particular, I want to experiment with tokio_net::signal::unix to play around with asynchronous signal handlers, source code here.

Here's my Cargo.toml:

[package]
name = "pman"
version = "0.1.0"
authors = ["Naftuli Kay <me@naftuli.wtf>"]
edition = "2018"

[dependencies]
tokio = "=0.2.0-alpha.2"
tokio-net = "=0.2.0-alpha.2"

And here's my main.rs:

use tokio_net::signal::unix::{Signal, SignalKind};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("Hello, world!");

    let sigterm = Signal::new(SignalKind::terminate())?;

    sigterm.for_each(|s| {
        println!("received signal {:?}", s);
    }).await;


    Ok(())
}

I have set rustup override set nightly to get nightly.

When I attempt to compile this, I get the following:

error[E0433]: failed to resolve: could not find `signal` in `tokio_net`
 --> src/main.rs:1:16
  |
1 | use tokio_net::signal::unix::{Signal, SignalKind};
  |                ^^^^^^ could not find `signal` in `tokio_net`

error[E0433]: failed to resolve: use of undeclared type or module `Signal`
 --> src/main.rs:7:19
  |
7 |     let sigterm = Signal::new(SignalKind::terminate())?;
  |                   ^^^^^^ use of undeclared type or module `Signal`

error[E0433]: failed to resolve: use of undeclared type or module `SignalKind`
 --> src/main.rs:7:31
  |
7 |     let sigterm = Signal::new(SignalKind::terminate())?;
  |                               ^^^^^^^^^^ use of undeclared type or module `SignalKind`

warning: unused imports: `SignalKind`, `Signal`
 --> src/main.rs:1:31
  |
1 | use tokio_net::signal::unix::{Signal, SignalKind};
  |                               ^^^^^^  ^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0433`.
error: Could not compile `pman`.

Interestingly enough, IDEA Rust recognizes that these are valid module paths and does code completion on them properly:

26

If I strip out the use of the signal module and just leave #[tokio::main] wrapping an async fn main which just uses println!, compilation works just fine, which leads me to believe that nightly is being respected here.

I have tried also using [replace] in Cargo.toml to go to the Tokio Git repository directly.

Any ideas?

1 Like

The signal module is feature gated in Tokio - you need your dependency to look like this:

tokio-net = { version = "=0.2.0-alpha.2", features = ["signal"] }
3 Likes

This solved it for me, thank you!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.