Skip to content

Instantly share code, notes, and snippets.

@matthauck
Created March 23, 2020 18:55
Show Gist options
  • Save matthauck/9743152133266c50d5593a90867b34c1 to your computer and use it in GitHub Desktop.
Save matthauck/9743152133266c50d5593a90867b34c1 to your computer and use it in GitHub Desktop.

Revisions

  1. matthauck created this gist Mar 23, 2020.
    9 changes: 9 additions & 0 deletions Cargo.toml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    [package]
    name = "async-trait-75"
    version = "0.1.0"
    authors = []
    edition = "2018"

    [dependencies]
    async-trait = "0.1.17"
    tokio = { version = "0.2.13", features = [ "rt-threaded", "macros" ] }
    12 changes: 12 additions & 0 deletions output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    $ cargo build
    Compiling async-trait-75 v0.1.0 (/Users/user/async-trait-test)
    warning: unused implementer of `std::future::Future` that must be used
    --> src/main.rs:26:5
    |
    26 | thing.do_direct();
    | ^^^^^^^^^^^^^^^^^^
    |
    = note: `#[warn(unused_must_use)]` on by default
    = note: futures do nothing unless you `.await` or poll them

    Finished dev [unoptimized + debuginfo] target(s) in 1.35s
    27 changes: 27 additions & 0 deletions src_main.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #[async_trait::async_trait]
    trait Interface {
    async fn do_trait(&self);
    }

    struct Thing;

    #[async_trait::async_trait]
    impl Interface for Thing {
    async fn do_trait(&self) {
    println!("hello from trait");
    }
    }

    impl Thing {
    async fn do_direct(&self) {
    println!("hello from direct");
    }
    }

    #[tokio::main]
    async fn main() {
    let thing = Thing {};

    thing.do_trait();
    thing.do_direct();
    }