• Stars
    star
    1,495
  • Rank 30,220 (Top 0.7 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created almost 5 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Type erasure for async trait methods

Async trait methods

github crates.io docs.rs build status

The stabilization of async functions in traits in Rust 1.75 did not include support for using traits containing async functions as dyn Trait. Trying to use dyn with an async trait produces the following error:

pub trait Trait {
    async fn f(&self);
}

pub fn make() -> Box<dyn Trait> {
    unimplemented!()
}
error[E0038]: the trait `Trait` cannot be made into an object
 --> src/main.rs:5:22
  |
5 | pub fn make() -> Box<dyn Trait> {
  |                      ^^^^^^^^^ `Trait` cannot be made into an object
  |
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
 --> src/main.rs:2:14
  |
1 | pub trait Trait {
  |           ----- this trait cannot be made into an object...
2 |     async fn f(&self);
  |              ^ ...because method `f` is `async`
  = help: consider moving `f` to another trait

This crate provides an attribute macro to make async fn in traits work with dyn traits.

Please refer to why async fn in traits are hard for a deeper analysis of how this implementation differs from what the compiler and language deliver natively.


Example

This example implements the core of a highly effective advertising platform using async fn in a trait.

The only thing to notice here is that we write an #[async_trait] macro on top of traits and trait impls that contain async fn, and then they work. We get to have Vec<Box<dyn Advertisement + Sync>> or &[&dyn Advertisement], for example.

use async_trait::async_trait;

#[async_trait]
trait Advertisement {
    async fn run(&self);
}

struct Modal;

#[async_trait]
impl Advertisement for Modal {
    async fn run(&self) {
        self.render_fullscreen().await;
        for _ in 0..4u16 {
            remind_user_to_join_mailing_list().await;
        }
        self.hide_for_now().await;
    }
}

struct AutoplayingVideo {
    media_url: String,
}

#[async_trait]
impl Advertisement for AutoplayingVideo {
    async fn run(&self) {
        let stream = connect(&self.media_url).await;
        stream.play().await;

        // Video probably persuaded user to join our mailing list!
        Modal.run().await;
    }
}

Supported features

It is the intention that all features of Rust traits should work nicely with #[async_trait], but the edge cases are numerous. Please file an issue if you see unexpected borrow checker errors, type errors, or warnings. There is no use of unsafe in the expanded code, so rest assured that if your code compiles it can't be that badly broken.

  • 👍 Self by value, by reference, by mut reference, or no self;
  • 👍 Any number of arguments, any return value;
  • 👍 Generic type parameters and lifetime parameters;
  • 👍 Associated types;
  • 👍 Having async and non-async functions in the same trait;
  • 👍 Default implementations provided by the trait;
  • 👍 Elided lifetimes.

Explanation

Async fns get transformed into methods that return Pin<Box<dyn Future + Send + 'async_trait>> and delegate to an async block.

For example the impl Advertisement for AutoplayingVideo above would be expanded as:

impl Advertisement for AutoplayingVideo {
    fn run<'async_trait>(
        &'async_trait self,
    ) -> Pin<Box<dyn std::future::Future<Output = ()> + Send + 'async_trait>>
    where
        Self: Sync + 'async_trait,
    {
        Box::pin(async move {
            /* the original method body */
        })
    }
}

Non-threadsafe futures

Not all async traits need futures that are dyn Future + Send. To avoid having Send and Sync bounds placed on the async trait methods, invoke the async trait macro as #[async_trait(?Send)] on both the trait and the impl blocks.


Elided lifetimes

Be aware that async fn syntax does not allow lifetime elision outside of & and &mut references. (This is true even when not using #[async_trait].) Lifetimes must be named or marked by the placeholder '_.

Fortunately the compiler is able to diagnose missing lifetimes with a good error message.

type Elided<'a> = &'a usize;

#[async_trait]
trait Test {
    async fn test(not_okay: Elided, okay: &usize) {}
}
error[E0726]: implicit elided lifetime not allowed here
 --> src/main.rs:9:29
  |
9 |     async fn test(not_okay: Elided, okay: &usize) {}
  |                             ^^^^^^- help: indicate the anonymous lifetime: `<'_>`

The fix is to name the lifetime or use '_.

#[async_trait]
trait Test {
    // either
    async fn test<'e>(elided: Elided<'e>) {}
    // or
    async fn test(elided: Elided<'_>) {}
}

Dyn traits

Traits with async methods can be used as trait objects as long as they meet the usual requirements for dyn -- no methods with type parameters, no self by value, no associated types, etc.

#[async_trait]
pub trait ObjectSafe {
    async fn f(&self);
    async fn g(&mut self);
}

impl ObjectSafe for MyType {...}

let value: MyType = ...;
let object = &value as &dyn ObjectSafe;  // make trait object

The one wrinkle is in traits that provide default implementations of async methods. In order for the default implementation to produce a future that is Send, the async_trait macro must emit a bound of Self: Sync on trait methods that take &self and a bound Self: Send on trait methods that take &mut self. An example of the former is visible in the expanded code in the explanation section above.

If you make a trait with async methods that have default implementations, everything will work except that the trait cannot be used as a trait object. Creating a value of type &dyn Trait will produce an error that looks like this:

error: the trait `Test` cannot be made into an object
 --> src/main.rs:8:5
  |
8 |     async fn cannot_dyn(&self) {}
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For traits that need to be object safe and need to have default implementations for some async methods, there are two resolutions. Either you can add Send and/or Sync as supertraits (Send if there are &mut self methods with default implementations, Sync if there are &self methods with default implementations) to constrain all implementors of the trait such that the default implementations are applicable to them:

#[async_trait]
pub trait ObjectSafe: Sync {  // added supertrait
    async fn can_dyn(&self) {}
}

let object = &value as &dyn ObjectSafe;

or you can strike the problematic methods from your trait object by bounding them with Self: Sized:

#[async_trait]
pub trait ObjectSafe {
    async fn cannot_dyn(&self) where Self: Sized {}

    // presumably other methods
}

let object = &value as &dyn ObjectSafe;

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

More Repositories

1

cxx

Safe interop between Rust and C++
Rust
5,106
star
2

anyhow

Flexible concrete Error type built on std::error::Error
Rust
4,193
star
3

thiserror

derive(Error) for struct and enum error types
Rust
3,352
star
4

proc-macro-workshop

Learn to write Rust procedural macros  [Rust Latam conference, Montevideo Uruguay, March 2019]
Rust
2,988
star
5

syn

Parser for Rust source code
Rust
2,574
star
6

cargo-expand

Subcommand to show result of macro expansion
Rust
2,433
star
7

case-studies

Analysis of various tricky Rust code
Rust
1,340
star
8

rust-quiz

Medium to hard Rust questions with explanations
Rust
1,318
star
9

quote

Rust quasi-quoting
Rust
1,173
star
10

watt

Runtime for executing procedural macros as WebAssembly
Rust
1,062
star
11

typetag

Serde serializable and deserializable trait objects
Rust
888
star
12

paste

Macros for all your token pasting needs
Rust
852
star
13

serde-yaml

Strongly typed YAML library for Rust
Rust
804
star
14

no-panic

Attribute macro to require that the compiler prove a function can't ever panic
Rust
758
star
15

inventory

Typed distributed plugin registration
Rust
714
star
16

rust-toolchain

Concise GitHub Action for installing a Rust toolchain
Shell
621
star
17

trybuild

Test harness for ui tests of compiler diagnostics
Rust
615
star
18

miniserde

Data structure serialization library with several opposite design goals from Serde
Rust
612
star
19

reflect

Compile-time reflection API for developing robust procedural macros (proof of concept)
Rust
602
star
20

request-for-implementation

Crates that don't exist, but should
597
star
21

indoc

Indented document literals for Rust
Rust
537
star
22

prettyplease

A minimal `syn` syntax tree pretty-printer
Rust
517
star
23

erased-serde

Type-erased Serialize, Serializer and Deserializer traits
Rust
503
star
24

semver

Parser and evaluator for Cargo's flavor of Semantic Versioning
Rust
500
star
25

dyn-clone

Clone trait that is object-safe
Rust
486
star
26

ryu

Fast floating point to string conversion
Rust
471
star
27

linkme

Safe cross-platform linker shenanigans
Rust
399
star
28

semver-trick

How to avoid complicated coordinated upgrades
Rust
383
star
29

cargo-llvm-lines

Count lines of LLVM IR per generic function
Rust
368
star
30

efg

Conditional compilation using boolean expression syntax, rather than any(), all(), not()
Rust
297
star
31

rust-faq

Frequently Asked Questions · The Rust Programming Language
262
star
32

rustversion

Conditional compilation according to rustc compiler version
Rust
256
star
33

itoa

Fast function for printing integer primitives to a decimal string
Rust
248
star
34

path-to-error

Find out path at which a deserialization error occurred
Rust
241
star
35

cargo-tally

Graph the number of crates that depend on your crate over time
Rust
212
star
36

proc-macro-hack

Procedural macros in expression position
Rust
203
star
37

monostate

Type that deserializes only from one specific value
Rust
194
star
38

colorous

Color schemes for charts and maps
Rust
193
star
39

readonly

Struct fields that are made read-only accessible to other modules
Rust
187
star
40

dissimilar

Diff library with semantic cleanup, based on Google's diff-match-patch
Rust
175
star
41

star-history

Graph history of GitHub stars of a user or repo over time
Rust
156
star
42

ref-cast

Safely cast &T to &U where the struct U contains a single field of type T.
Rust
154
star
43

automod

Pull in every source file in a directory as a module
Rust
129
star
44

inherent

Make trait methods callable without the trait in scope
Rust
128
star
45

ghost

Define your own PhantomData
Rust
115
star
46

faketty

Wrapper to exec a command in a pty, even if redirecting the output
Rust
113
star
47

dtoa

Fast functions for printing floating-point primitives to a decimal string
Rust
110
star
48

clang-ast

Rust
108
star
49

seq-macro

Macro to repeat sequentially indexed copies of a fragment of code
Rust
102
star
50

remain

Compile-time checks that an enum or match is written in sorted order
Rust
99
star
51

mashup

Concatenate identifiers in a macro invocation
Rust
96
star
52

noisy-clippy

Rust
84
star
53

tt-call

Token tree calling convention
Rust
77
star
54

basic-toml

Minimal TOML library with few dependencies
Rust
76
star
55

squatternaut

A snapshot of name squatting on crates.io
Rust
73
star
56

serde-ignored

Find out about keys that are ignored when deserializing data
Rust
68
star
57

enumn

Convert number to enum
Rust
66
star
58

bootstrap

Bootstrapping rustc from source
Shell
62
star
59

essay

docs.rs as a publishing platform?
Rust
62
star
60

db-dump

Library for scripting analyses against crates.io's database dumps
Rust
60
star
61

scratch

Compile-time temporary directory shared by multiple crates and erased by `cargo clean`
Rust
59
star
62

gflags

Command line flags library that does not require a central list of all the flags
Rust
55
star
63

install

Fast `cargo install` action using a GitHub-based binary cache
Shell
55
star
64

oqueue

Non-interleaving multithreaded output queue
Rust
53
star
65

serde-starlark

Serde serializer for generating Starlark build targets
Rust
53
star
66

build-alert

Rust
51
star
67

unicode-ident

Determine whether characters have the XID_Start or XID_Continue properties
Rust
51
star
68

lalrproc

Proof of concept of procedural macro input parsed by LALRPOP
Rust
50
star
69

dragonbox

Rust
50
star
70

sha1dir

Checksum of a directory tree
Rust
38
star
71

hackfn

Fake implementation of `std::ops::Fn` for user-defined data types
Rust
38
star
72

reduce

iter.reduce(fn) in Rust
Rust
37
star
73

link-cplusplus

Link libstdc++ or libc++ automatically or manually
Rust
36
star
74

argv

Non-allocating iterator over command line arguments
Rust
33
star
75

get-all-crates

Download .crate files of all versions of all crates from crates.io
Rust
31
star
76

threadbound

Make any value Sync but only available on its original thread
Rust
31
star
77

dircnt

Count directory entries—`ls | wc -l` but faster
Rust
27
star
78

unsafe-libyaml

libyaml transpiled to rust by c2rust
Rust
27
star
79

serde-stacker

Serializer and Deserializer adapters that avoid stack overflows by dynamically growing the stack
Rust
27
star
80

cargo-unlock

Remove Cargo.lock lockfile
Rust
25
star
81

respan

Macros to erase scope information from tokens
Rust
24
star
82

isatty

libc::isatty that also works on Windows
Rust
21
star
83

iota

Related constants in Rust: 1 << iota
Rust
20
star
84

foreach

18
star
85

bufsize

bytes::BufMut implementation to count buffer size
Rust
18
star
86

hire

How to hire dtolnay
18
star
87

precise

Full precision decimal representation of f64
Rust
17
star
88

dashboard

15
star
89

rustflags

Parser for CARGO_ENCODED_RUSTFLAGS
Rust
13
star
90

libfyaml-rs

Rust binding for libfyaml
Rust
11
star
91

install-buck2

Install precompiled Buck2 build system
6
star
92

mailingset

Set-algebraic operations on mailing lists
Python
5
star
93

.github

5
star
94

jq-gdb

gdb pretty-printer for jv objects
Python
1
star