"arbitrary_self_types": Add Borrow<Self> type

I have a real case that the feature "arbitrary_self_types" will helps a lot, but I found that this feature is somehow not maintained due to its complexity.

Currently, Rust will let me do things like this:

fn test<F: BorrowMut<F>>(mut f: F) -> F {
    let x = f.borrow_mut();
    let y = &f;
    let z = &mut f;
    f
}


fn main() {
    let foo = Foo {};
    let mut foo2 = test(test(foo));
    let foo3 = test(test(&mut foo2));
}

In this case, the return type is depended on the passed in type, so the foo2 is a Foo type, and the foo3 is a &mut Foo type.

I wondered if it is reasonable to just add Borrow<Self> and Borrow<Self>, since they can be derefed to &Self and &mut Self, to do the same thing:

struct Foo {}

impl Foo {
    fn foo<F: BorrowMut<Self>>(self: F) -> F {
        self
    }
}

In this way, it looks to me the complexity could be Minimized.

In this case, the "builder" design will be benefited a lot, you can see the question I asked.

Unfortunately this doesn't do much to reduce the complexity of arbitrary self types, as Borrow and BorrowMut are traits, not types. They can't just be added to the list of allowed self types as such, because they aren't types, they're an extendible set of types.

4 Likes