Why is Borrow not recursive?

Is there some reason why Borrow and BorrowMut do not provide recursive impls like

impl<T: ?Sized, U: Borrow<T>> Borrow<T> for Arc<U> {
    fn borrow(&self) -> &T {
        **self.borrow()
    }
}

I suppose the obvious use cases would be stacks of smart pointers, so the direct impls would look like

impl<T: ?Sized> Borrow<T> for Arc<Box<T>> {
    fn borrow(&self) -> &T {
        &**self
    }
}

And one can avoid these using impl<T: ?Sized> From<Box<T>> for Arc<T> at the cost larger allocations and copies.

We’d want a different answer if we started placing the Arcs into arenas or whatever though… or wanted the T: ?Sized back out frequently.

https://play.integer32.com/?gist=ac65a171687200ead2b633ac21a84949&version=stable&mode=debug&edition=2015

1 Like

These impl overlaps with the reflexive impl<T> Borrow<T> for T (your example overlaps in the case T = Arc<U>).

1 Like

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