aatxe / irc

the irc crate – usable, async IRC for Rust
Mozilla Public License 2.0
535 stars 100 forks source link

run examples code in my lib error #244

Open CppXL opened 2 years ago

CppXL commented 2 years ago

my rust env

I have a rust library which create by cargo new circ --bin I try to copy irc/examples/simple_plaintext.rs source code to circ/src/main.rs

when I try to build it with cargo build, cargo give three errors

Compiling circ v0.1.0 (F:\code\rust\virc\circ)
error[E0433]: failed to resolve: could not find `main` in `tokio`
 --> src\main.rs:7:10
  |
7 | #[tokio::main]
  |          ^^^^ could not find `main` in `tokio`

error[E0560]: struct `irc::client::data::Config` has no field named `use_tls`
  --> src\main.rs:13:9
   |
13 |         use_tls: Some(false),
   |         ^^^^^^^ `irc::client::data::Config` does not have this field
   |
   = note: available fields are: `owners`, `nickname`, `nick_password`, `alt_nicks`, `username` ... and 24 others

error[E0752]: `main` function is not allowed to be `async`
 --> src\main.rs:8:1
  |
8 | async fn main() -> irc::error::Result<()> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `main` function is not allowed to be `async`

Some errors have detailed explanations: E0433, E0560, E0752.
For more information about an error, try `rustc --explain E0433`.
error: could not compile `circ` due to 3 previous errors

This is src/main.rs

extern crate futures;
extern crate irc;

use futures::prelude::*;
use irc::client::prelude::*;

#[tokio::main]
async fn main() -> irc::error::Result<()> {
    let config = Config {
        nickname: Some("pickles".to_owned()),
        server: Some("chat.freenode.net".to_owned()),
        channels: vec!["#rust-spam".to_owned()],
        use_tls: Some(false),
        ..Default::default()
    };

    let mut client = Client::from_config(config).await?;
    client.identify()?;

    let mut stream = client.stream()?;
    let sender = client.sender();

    while let Some(message) = stream.next().await.transpose()? {
        print!("{}", message);

        match message.command {
            Command::PRIVMSG(ref target, ref msg) => {
                if msg.contains(client.current_nickname()) {
                    sender.send_privmsg(target, "Hi!")?;
                }
            }
            _ => (),
        }
    }

    Ok(())
}

This is Cargo.toml

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

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

[dependencies]

irc-proto = { version = "0.14.0" }

# Feature - TLS
irc = { git = "https://github.com/aatxe/irc", branch = "0.14", features = [
    "ctcp",
    "toml",
] }

native-tls = { version = "0.2.0", optional = true }
tokio-rustls = { version = "0.22.0", features = [
    "dangerous_configuration",
], optional = true }
tokio-native-tls = { version = "0.3.0", optional = true }
tokio = { version = "1.0.0", features = ["net", "time", "sync"] }
tokio-stream = "0.1.0"
tokio-util = { version = "0.6.0", features = ["codec"] }
futures = "0.3.0"
CppXL commented 2 years ago

I just can only build examples code in git clone https://github.com/aatxe/irc.git directory

aatxe commented 2 years ago

It looks like the #[tokio::main] macro is locked behind a feature now, so you'd need to add rt and macros to the feature list for the tokio crate.

CppXL commented 2 years ago

It looks like the #[tokio::main] macro is locked behind a feature now, so you'd need to add rt and macros to the feature list for the tokio crate.

thanks for you response, now I can compile fine, but a panic is triggered in the tokio library, I will continue to investigate how this error is generated

GTP95 commented 1 year ago

It looks like the #[tokio::main] macro is locked behind a feature now, so you'd need to add rt and macros to the feature list for the tokio crate.

Sorry, I'm new to Rust, how do I do this?

8573 commented 1 year ago

@GTP95:

tokio = { version = "1.28.0", features = ["rt", "macros"] }

See https://doc.rust-lang.org/stable/cargo/reference/features.html#dependency-features.