async-trait | Type erasure for async trait methods | Reactive Programming library
kandi X-RAY | async-trait Summary
kandi X-RAY | async-trait Summary
Type erasure for async trait methods
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of async-trait
async-trait Key Features
async-trait Examples and Code Snippets
Community Discussions
Trending Discussions on async-trait
QUESTION
I'm experimenting with Rocket, Rust and SQLx and I'd like to test what happens when two parallel transactions try to insert a duplicated record on my table.
My insert fn contains nothing special and it works fine:
...ANSWER
Answered 2022-Apr-04 at 16:05You can use a async_std::future::timeout
or tokio::time::timeout
. Example using async_std:
QUESTION
I'm trying to write a chat bot that supports the matrix protocol, and I ran into this problem that I can't wrap my head around. On its own the code compiles without issue, but adding "surf" as a dependency to the Cargo.toml causes a "dyn log::kv::source::Source` cannot be shared between threads safely" error.
This is the minimal code for which this happens:
main.rs:
...ANSWER
Answered 2022-Feb-13 at 10:06A friend of mine figured out what the problem was:
The problem wasn't surf directly, but the tracing crate with the log feature enabled, on which surf indirectly depends. There is already an issue on github on it, but it's not yet resolved.
The problem gets triggered by matrix-sdk because it also uses tracing, but usually without the log feature. The line that triggers it is this:
QUESTION
I'm getting blocked on what I think it's a simple problem. I'm still learning Rust, and I want to do the following:
I want to create an async trait (using async-trait
) that will instantiate a DB connection instance and it will return the struct that is implementing that trait.
mongo.rs
ANSWER
Answered 2022-Feb-05 at 07:39You've declared init
to take 2 generic parameters: T
and E
.
This means that the code that calls init
has to provide the concrete types to fill in those parameters. For example, if someone was using your library, it would be totally feasible for them to write init::()
, and your code should deal with that.
Because of that, when you define your impl DB for FavouritesDB
, you write this:
QUESTION
I'm trying to use the #[async_recursion]
macro on a constructor that takes an impl trait as an argument. The impl trait is just a shim around reqwest so I can insert a mock for testing:
ANSWER
Answered 2022-Jan-25 at 23:01The problem is, like explained by the compiler, that &impl NetFuncs
may not necessarily impl Send
but the async_recursion
macro by default requires it, so, you have two options:
- Require
impl NetFuncs
to beSync
, so that&impl NetFuncs
isSend
. This can be done either with&(impl NetFuncs + Sync)
or by requiring every implementor to implementSend
:trait NetFuncs: Sync
. - Not requiring the resulting future to be
Send
. As documented in theasync_recursion
documentation, this can be done by changing#[async_recursion]
to#[async_recursion(?Send)]
.
Without the macro it works since the compiler make the resulting future Send
depend on whether all types kept across .await
points are Send
: if they are, the future is also Send
. If they are not, it is not too. The macro changes the async fn
to fn ...() -> Pin>
, and unfortunately, it is not possible to have the same behavior as with async fn
- this is something only the compiler can implement. Thus, the macro allows you to choose whether you want the resulting future to be Send
- meaning all types should be too, or not.
QUESTION
I am getting permission denied when trying to run a small rust cli app via a docker container. I can build the image fine, but when I try to run it I get:
...ANSWER
Answered 2021-Dec-13 at 17:09You are trying to execute a directory "/volume/target/x86_64-unknown-linux-musl/release"
. I advise you use cargo install
:
QUESTION
Not sure why is this happening, here's a snippet:
struct:
...ANSWER
Answered 2021-Nov-05 at 01:42The concrete problem is that
QUESTION
Consider the following code which declares a trait with an async
method using the async-trait
crate.
ANSWER
Answered 2021-Aug-29 at 18:07You have to modify your Automobile
definition:
QUESTION
In the example code below, a non-send value, Vec
, is moved into a function that returns something else. At this point, I no longer care about that vector. The returned object stores no reference to it, it no longer exists.
However, when I .await
on the next line I get the error "captured value is not Send
". Which it isn't, but since it should have been destroyed when vector_as_string
exited, it doesn't need to send it across threads when the future restarts, because that variable is never used again.
ANSWER
Answered 2021-Aug-04 at 14:29From the async_trait documentation:
Async fns get transformed into methods that return
Pin>
and delegate to a private async freestanding function.
Not all async traits need futures that are
dyn Future + Send
. To avoid havingSend
andSync
bounds placed on the async trait methods, invoke the async trait macro as#[async_trait(?Send)]
on both the trait and the impl blocks.
Applied to your case:
QUESTION
I was following Michael-F-Bryan's Dynamic Loading & Plugins chapter from his Rust FFI guide, but I am storing the plugins in a HashMap
(HashMap<&'static str, Box
) instead of a Vec
so that I can call the functions of a Plugin
individually.
I would like the plugins to define a asynchronous function with it's own loop that communicates with the main part of the application using channels (std
or tokio
).
Working around the fact that you can't have async
functions in traits was easy thanks to the async_trait crate, but the issue that I am now facing is, that I cannot spawn a new thread with the module because the new thread might outlive the PluginManager
.
I tried to recreate this in a rust playground without the dynamic modules and I'm facing the same error here (note: this does not include any kind of channel communication)
Unlike the first error, I was unable to recreate a rust playground for the second one (as it happens at runtime). Instead of spawning a new thread, I was using tokio's event loop to handle the async
functions. This works in the sandbox, but not when using a shared library as plugin. At runtime it throws:
ANSWER
Answered 2021-Jan-08 at 00:03You've already recognized the problem: a reference to an object owned by the PluginManager
is being moved into a future that is being spawned, and thus may run on another thread than the one which owns the PluginManager
. The compiler cannot know that the future will not outlive the PluginManager
.
There are a number of possible solutions to this, for example:
Store the plugin instances inside
Arc>
, so that they are reference counted in runtime. Then even if the plugin manager did not live as long as the futures you are spawning, it would not matterMake the
PluginManager
an immutable static singleton
In this case I suspect you want the PluginManager
to be a singleton. You can do that by replacing the new
constructor with a get
method that lazily constructs the instance and returns a reference to it:
QUESTION
When I use my fork of async-trait as a dependency, it fails to compile due to syn::*
types equality. All is green in async-trait CI checks. To reproduce, start a new cargo lib project and add to Cargo.toml:
ANSWER
Answered 2020-Aug-31 at 23:17While the type syn::Path
is available when either the feature full
or derive
is enabled, some of the traits implemented for that type aren't.
In particular, as per syn
's documentation of optional features, the extra-traits
feature is required to get PartialEq
:
extra-traits
— Debug, Eq, PartialEq, Hash impls for all syntax tree types.
Therefore you only need adjust your Cargo.toml
with
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install async-trait
Rust is installed and managed by the rustup tool. Rust has a 6-week rapid release process and supports a great number of platforms, so there are many builds of Rust available at any time. Please refer rust-lang.org for more information.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page