Borrow checker failing when providing trait object as type parameter


I have some weird behaviour of the borrow checker when using generic structures. Am I doing something wrong?

struct MyGenericStruct<T>;

impl <T> MyGenericStruct<T> {
    fn bar(&self, _: T) {}

struct MyNonGenericStruct;

impl MyNonGenericStruct {
    fn bar(&self, _: &mut MyTrait) {}

trait MyTrait{}

struct FooBar;
impl MyTrait for FooBar{}

fn test() { 
    let mut foobar = FooBar;

    {    // No problem when using non generic struct
        let foo = MyNonGenericStruct; foobar); foobar);
        let foo = MyGenericStruct::<&mut MyTrait>;
   foobar);   // First borrow passes ...
   foobar);   // This fails as "foobar" is already borrowed
    }   // The previous borrow ends here (lifetime of "foo")


1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.