Skip to content

Mocking 3rd party trait with generic parameter #510

Answered by asomers
zwilcox asked this question in Questions
Discussion options

You must be logged in to vote

You'll have to do the mocking at least partially manually. For instance, you could use mockall for inherent methods, and inner methods, and then manually impl the trait. If you're sure the lifetimes are safe, you can even transmute them , like this:

mockall::mock! {
    pub Socket {
       fn _write_frame<F>(&self, frame: &F) -> std::io::Result<()>
           where F:  std::convert::Into<Self::FrameType> + socketcan::frame::AsPtr + 'static;
    }
}

}
impl socketcan::Socket for MockSocket {
    type FrameType = socketcan::CanAnyFrame;
     fn write_frame<F>(&self, frame: &F) -> std::io::Result<()>
    where
        F:  std::convert::Into<Self::FrameType> + socketcan::frame::AsPtr;
    {

Replies: 2 comments 9 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
9 replies
@zwilcox
Comment options

@asomers
Comment options

@zwilcox
Comment options

@asomers
Comment options

Answer selected by asomers
@zwilcox
Comment options

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