Comments (8)
This is a complicated trait. Does it work if you define that method like this? fn iter_mut(&mut self) -> impl Iterator<Item = &'static mut Self::T>;
from mockall.
with this modification, I have the following error:
Compiling playground-rs v0.1.0 (/Users/vitornesello/code/playground-rs)
error[E0310]: the associated type `<Self as Foo>::T` may not live long enough
--> src/main.rs:8:45
|
8 | fn iter_mut(&mut self) -> impl Iterator<Item = &'static mut Self::T>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| the associated type `<Self as Foo>::T` must be valid for the static lifetime...
| ...so that the reference type `&'static mut <Self as Foo>::T` does not outlive the data it points at
|
= help: consider adding an explicit lifetime bound `<Self as Foo>::T: 'static`...
from mockall.
Did you try following that suggestion and adding the bound?
from mockall.
I could make it work like this, but I not sure if it is exactly what the suggestion was saying:
#[automock(type T=String;)]
trait Foo {
type T: 'static;
fn iter(&self) -> impl Iterator<Item = usize>;
fn iter_mut(&mut self) -> impl Iterator<Item = &'static mut <Self as Foo>::T>;
}
However, it seems odd to me to require a static lifetime in this trait. Am I missing something?
from mockall.
That's actually not as odd as you might think. In order to store the expectation, Mockall requires that the lifetime of return values be either the same as the object itself, or else 'static
. And "the same as the object itself" only works for a few common cases like &T
.
https://docs.rs/mockall/latest/mockall/#reference-return-values
from mockall.
Thanks a lot! I realize that I need a deeper understanding of lifetimes. I am quite new to the concept.
from mockall.
That's actually not as odd as you might think. In order to store the expectation, Mockall requires that the lifetime of return values be either the same as the object itself, or else
'static
. And "the same as the object itself" only works for a few common cases like&T
. https://docs.rs/mockall/latest/mockall/#reference-return-values
Could you consider clarifying what "the same as the object itself" means in the documentation? I stumbled upon the issue a while ago, and clearer documentation would have been helpful. Thanks!
from mockall.
When mocking a function that returns a reference, Mockall stores that referent within the mock object. For example:
#[automock]
pub trait Foo {
fn foo(&self) -> &i32;
}
#[test]
fn t() {
let mut mock = MockFoo::new();
mock.expect_foo()
.return_ref(42i32);
}
As you can see, we store a real i32
within the Mock object. So that i32
must have that same lifetime as the mock object itself. Mockall also has a few hard-coded special cases for methods that return references. For example, a method that returns a &str
reference will take a String
argument in its return_ref
function. And if the function returns a 'static
reference, then you can pass any anything you want to return_const
or returning
.
from mockall.
Related Issues (20)
- Capturing arguments from returning HOT 2
- Struggling to mock trait objects and make assertions on functions that take ownership of mock trait object HOT 4
- Mock Trait that is a composition of two other traits HOT 2
- Struct with different traits that has identically named methods HOT 1
- Can't apply `automock` to traits with default method implementions marked `#[inline]` HOT 1
- Remove ...
- Problem with mocking a trait which uses &Self HOT 1
- Mockall Double of struct in external crate HOT 2
- mocking struct & mockall_double::double not working as expected HOT 10
- Mocking with tokio::sync::Mutex, confused for std::sync::Mutex HOT 4
- Feature request: create a `spy!` macro to provide a transparent mock but that can be asserted upon. HOT 6
- Feature request: support for `const generics` in `mock!` and `automock` HOT 4
- Mocking Default Trait Method Implementations HOT 4
- How to bypass a mock for a particular test and call the original function HOT 3
- automock fails for method taking Option<&u32> HOT 5
- MSRV 1.77.0 TODO
- Mocking a function taking an FnMut closure incompatible with concretize HOT 2
- How to create a "dummy" mock where no expectations are required HOT 4
- Mocking an async trait with generics HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mockall.