mardi 30 juillet 2019

Rust: Replace function on struct

I have the following code which I'm trying to test. My question is how do I replace the function something that's implemented on B? So that under test B.something does nothing, or returns a specified value?

trait DoSomething {
    fn something(&mut self);
}

struct A {}
struct B {}

impl DoSomething for A {
    fn something(&mut self) {
        println!("doing something on A");
        let mut b = B{};
        b.something();
    }
}

impl DoSomething for B {
    fn something(&mut self) {
        println!("doing something on B");
    }
}

fn main() {
    let mut a = A{};
    a.something();
}

The above code is available in the rust playground

Coming from python I would simply patch.object(b, 'something')

Aucun commentaire:

Enregistrer un commentaire