Giter Site home page Giter Site logo

Comments (7)

asomers avatar asomers commented on July 21, 2024 1

That message is telling you that the as_any method's return value is not Send. Try adding a + Send trait bound to it.

from mockall.

asomers avatar asomers commented on July 21, 2024

automock does work with async_trait. For example, see https://github.com/asomers/mockall/blob/master/mockall/tests/automock_async_trait.rs . The main thing to be aware of is that the automock attribute must precede the async_trait attribute.

from mockall.

Tockra avatar Tockra commented on July 21, 2024

Okay, my program does not compile if I do it. Maybe somebody could assist to fix the example above.

from mockall.

asomers avatar asomers commented on July 21, 2024

Maybe you could post the error message?

from mockall.

Tockra avatar Tockra commented on July 21, 2024

Of course:

error: future cannot be sent between threads safely
  --> src/db/mod.rs:30:1
   |
30 | #[automock]
   | ^^^^^^^^^^^ future created by async block is not `Send`
   |
note: captured value is not `Send` because `&mut` references cannot be sent unless their referent is `Send`
  --> src/db/mod.rs:45:27
   |
45 |     async fn connect(&mut self, config: &Config) -> Result<(), mongodb::error::Error>;
   |                           ^^^^ has type `&mut MockDatabaseService<T>` which is not `Send`, because `MockDatabaseService<T>` is not `Send`
   = note: required for the cast from `Pin<Box<{async block@src/db/mod.rs:30:1: 30:12}>>` to `Pin<Box<(dyn futures::Future<Output = Result<(), mongodb::error::Error>> + std::marker::Send + 'async_trait)>>`
   = note: this error originates in the attribute macro `automock` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting this bound
   |
32 | pub trait DatabaseService<T: model::Element + std::marker::Send>: Debug + Sync + Send {
   |                                             +++++++++++++++++++

from mockall.

asomers avatar asomers commented on July 21, 2024

Did you try following the compiler's advice?

from mockall.

Tockra avatar Tockra commented on July 21, 2024

After fullfilling the suggestions step for step I arrived at:

#[cfg(test)]
use mockall::{automock, mock, predicate::*};
#[cfg_attr(test, automock)]
#[async_trait]
pub trait DatabaseService<T: model::Element + std::marker::Send + std::marker::Sync>:
    Debug + Sync + Send
    // ...

With error message:

error[E0277]: `(dyn std::any::Any + 'static)` cannot be sent between threads safely
   --> src/db/mod.rs:30:11
    |
30  | pub trait DatabaseService<T: model::Element + std::marker::Send + std::marker::Sync>:
    |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `(dyn std::any::Any + 'static)` cannot be sent between threads safely
    |
    = help: the trait `std::marker::Send` is not implemented for `(dyn std::any::Any + 'static)`, which is required by `MockDatabaseService<T>: std::marker::Send`
    = note: required for `Unique<(dyn std::any::Any + 'static)>` to implement `std::marker::Send`
note: required because it appears within the type `Box<(dyn std::any::Any + 'static)>`
   --> /Users/titan/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/boxed.rs:197:12
    |
197 | pub struct Box<
    |            ^^^
note: required because it appears within the type `__as_any::Rfunc<T>`
   --> src/db/mod.rs:28:18
    |
28  | #[cfg_attr(test, automock)]
    |                  ^^^^^^^^
note: required because it appears within the type `__as_any::Expectation<T>`
   --> src/db/mod.rs:28:18
    |
28  | #[cfg_attr(test, automock)]
    |                  ^^^^^^^^
    = note: required for `Unique<__as_any::Expectation<T>>` to implement `std::marker::Send`
note: required because it appears within the type `alloc::raw_vec::RawVec<__as_any::Expectation<T>>`
   --> /Users/titan/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/raw_vec.rs:69:19
    |
69  | pub(crate) struct RawVec<T, A: Allocator = Global> {
    |                   ^^^^^^
note: required because it appears within the type `Vec<__as_any::Expectation<T>>`
   --> /Users/titan/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:398:12
    |
398 | pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
    |            ^^^
note: required because it appears within the type `__as_any::Expectations<T>`
   --> src/db/mod.rs:28:18
    |
28  | #[cfg_attr(test, automock)]
    |                  ^^^^^^^^
note: required because it appears within the type `MockDatabaseService_DatabaseService_13065831140148901594<T>`
   --> src/db/mod.rs:30:11
    |
30  | pub trait DatabaseService<T: model::Element + std::marker::Send + std::marker::Sync>:
    |           ^^^^^^^^^^^^^^^
note: required because it appears within the type `MockDatabaseService<T>`
   --> src/db/mod.rs:30:11
    |
30  | pub trait DatabaseService<T: model::Element + std::marker::Send + std::marker::Sync>:
    |           ^^^^^^^^^^^^^^^
note: required by a bound in `DatabaseService`
   --> src/db/mod.rs:31:20
    |
30  | pub trait DatabaseService<T: model::Element + std::marker::Send + std::marker::Sync>:
    |           --------------- required by a bound in this trait
31  |     Debug + Sync + Send
    |                    ^^^^ required by this bound in `DatabaseService`
    = note: this error originates in the attribute macro `automock` (in Nightly builds, run with -Z macro-backtrace for more info)


from mockall.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.