async-trait | Type erasure for async trait methods | Reactive Programming library

 by   dtolnay Rust Version: 0.1.68 License: Apache-2.0

kandi X-RAY | async-trait Summary

kandi X-RAY | async-trait Summary

async-trait is a Rust library typically used in Programming Style, Reactive Programming applications. async-trait has no bugs, it has no vulnerabilities, it has a Permissive License and it has medium support. You can download it from GitHub.

Type erasure for async trait methods
Support
    Quality
      Security
        License
          Reuse

            kandi-support Support

              async-trait has a medium active ecosystem.
              It has 1478 star(s) with 67 fork(s). There are 16 watchers for this library.
              OutlinedDot
              It had no major release in the last 12 months.
              There are 5 open issues and 142 have been closed. On average issues are closed in 307 days. There are no pull requests.
              It has a neutral sentiment in the developer community.
              The latest version of async-trait is 0.1.68

            kandi-Quality Quality

              async-trait has 0 bugs and 0 code smells.

            kandi-Security Security

              async-trait has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
              async-trait code analysis shows 0 unresolved vulnerabilities.
              There are 0 security hotspots that need review.

            kandi-License License

              async-trait is licensed under the Apache-2.0 License. This license is Permissive.
              Permissive licenses have the least restrictions, and you can use them in most projects.

            kandi-Reuse Reuse

              async-trait releases are available to install and integrate.
              Installation instructions are not available. Examples and code snippets are available.

            Top functions reviewed by kandi - BETA

            kandi's functional review helps you automatically verify the functionalities of the libraries and avoid rework.
            Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of async-trait
            Get all kandi verified functions for this library.

            async-trait Key Features

            No Key Features are available at this moment for async-trait.

            async-trait Examples and Code Snippets

            No Code Snippets are available at this moment for async-trait.

            Community Discussions

            QUESTION

            How to test two parallel transactions in Rust SQLx?
            Asked 2022-Apr-04 at 18:26

            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:05

            QUESTION

            Surf dependency causes "cannot be shared between threads safely" error in previously compiling program with matrix_sdk and async-trait
            Asked 2022-Feb-13 at 10:06

            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:06

            A 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:

            Source https://stackoverflow.com/questions/71077261

            QUESTION

            Generic async trait that returns the implemented Struct
            Asked 2022-Feb-05 at 07:39

            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:39

            You'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:

            Source https://stackoverflow.com/questions/70995999

            QUESTION

            Using async_recursion macro with an impl trait reference as argument
            Asked 2022-Jan-25 at 23:01

            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:01

            The 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 be Sync, so that &impl NetFuncs is Send. This can be done either with &(impl NetFuncs + Sync) or by requiring every implementor to implement Send: trait NetFuncs: Sync.
            • Not requiring the resulting future to be Send. As documented in the async_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.

            Source https://stackoverflow.com/questions/70855298

            QUESTION

            Permission denied error when running docker image with rust binary
            Asked 2021-Dec-21 at 19:06

            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:09

            You are trying to execute a directory "/volume/target/x86_64-unknown-linux-musl/release". I advise you use cargo install:

            Source https://stackoverflow.com/questions/70336487

            QUESTION

            Deserialize reqwest query error rustc(E0308)
            Asked 2021-Nov-05 at 01:42

            Not sure why is this happening, here's a snippet:

            struct:

            ...

            ANSWER

            Answered 2021-Nov-05 at 01:42

            The concrete problem is that

            Source https://stackoverflow.com/questions/69832580

            QUESTION

            How to adopt Send and Sync for a struct with a dynamic member (future cannot be sent between threads safely)
            Asked 2021-Sep-02 at 18:00

            Consider the following code which declares a trait with an async method using the async-trait crate.

            ...

            ANSWER

            Answered 2021-Aug-29 at 18:07

            You have to modify your Automobile definition:

            Source https://stackoverflow.com/questions/68975454

            QUESTION

            How to tell the compiler a value is not used after awaiting in an async_trait method?
            Asked 2021-Aug-04 at 14:29

            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:29

            From 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 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.

            Applied to your case:

            Source https://stackoverflow.com/questions/68650104

            QUESTION

            Execute asyncronus function from shared library in a new thread (Rust)
            Asked 2021-Jan-08 at 00:03

            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:03

            You'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 matter

            • Make 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:

            Source https://stackoverflow.com/questions/65620630

            QUESTION

            Binary operation == cannot be applied to type syn::Path
            Asked 2020-Sep-01 at 00:21

            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:17

            While 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

            Source https://stackoverflow.com/questions/63677722

            Community Discussions, Code Snippets contain sources that include Stack Exchange Network

            Vulnerabilities

            No vulnerabilities reported

            Install async-trait

            You can download it from GitHub.
            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

            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.
            Find more information at:

            Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items

            Find more libraries
            CLONE
          • HTTPS

            https://github.com/dtolnay/async-trait.git

          • CLI

            gh repo clone dtolnay/async-trait

          • sshUrl

            git@github.com:dtolnay/async-trait.git

          • Stay Updated

            Subscribe to our newsletter for trending solutions and developer bootcamps

            Agree to Sign up and Terms & Conditions

            Share this Page

            share link

            Consider Popular Reactive Programming Libraries

            axios

            by axios

            RxJava

            by ReactiveX

            async

            by caolan

            rxjs

            by ReactiveX

            fetch

            by github

            Try Top Libraries by dtolnay

            cxx

            by dtolnayRust

            anyhow

            by dtolnayRust

            thiserror

            by dtolnayRust

            proc-macro-workshop

            by dtolnayRust

            syn

            by dtolnayRust