How do I solve the error “the precise format of `Fn`-family traits' type parameters is subject to change”?

后端 未结 1 340
猫巷女王i
猫巷女王i 2021-01-05 20:43

I have written a problem solver in Rust which as a subroutine needs to make calls to a function which is given as a black box (essentially I would like to give an argument o

相关标签:
1条回答
  • 2021-01-05 21:04

    The direct answer is to do exactly as the error message says:

    Use parenthetical notation instead

    That is, instead of Fn<(A, B)>, use Fn(A, B)

    The real problem is that you are not allowed to implement the Fn* family of traits yourself in stable Rust.

    The real question you are asking is harder to be sure of because you haven't provided a MCVE, so we are reduced to guessing. I'd say you should flip it around the other way; create a new trait, implement it for closures and your type:

    trait Solve {
        type Output;
        fn solve(&mut self) -> Self::Output;
    }
    
    impl<F, T> Solve for F
    where
        F: FnMut() -> T,
    {
        type Output = T;
    
        fn solve(&mut self) -> Self::Output {
            (self)()
        }
    }
    
    struct Test;
    impl Solve for Test {
        // interesting things
    }
    
    fn main() {}
    
    0 讨论(0)
提交回复
热议问题