Skip to content

Looking for a workaround for an FnOnce parameter with a non-static lifetime #613

Answered by asomers
pentastich asked this question in Questions
Discussion options

You must be logged in to vote

Is this function part of a public trait, part of a trait that you control, or not part of a trait at all? In the latter two cases, you can conditionally redefine the fuction to use 'static during #[cfg(test)] only. That's what I do in similar cases.
Another option is to use mock! to define a helper function that doesn't have the lifetime, like and then manually define a compliant mock method that wraps the helper. It's more work, but it will at least allow you to write expectations that your closure was called. For example:

mock!{
    pub MyStuct {
        fn __eval<T> -> Result<T, EvalError>;
    }
}
impl MockMyStruct {
    fn eval<'a, T, F>(&self, formula: F) -> Result<T, EvalError>
    w…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@pentastich
Comment options

Answer selected by pentastich
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants