Potentially missing `Borrow` implementations

All of the following types implement Deref but not Borrow:

All of the types I've included above I think are candidates for having a Borrow implementation, as they AIUI satisfy the Borrow trait requirements of &Self "behaving like" &T.

Is this trait impl omission deliberate (am I missing a reason why they shouldn't impl Borrow?) or has just nobody seen the need to request/provide these implementations yet? Obviously &T is the canonical Borrow type, and since all of these types impl Deref, getting &T out is trivial.

4 Likes

Note that for each of these except for IoSlice/IoSliceMut, a generic Borrow<T> implementation is technically a blanket impl and thus a breaking change, because it would conflict with something like

struct Foo;

impl std::borrow::Borrow<Foo> for std::cell::Ref<'_, Foo> {
    fn borrow(&self) -> &Foo {
        self
    }
}

(playground)

There is (some occasional) precedent of such impls being added after-the-fact (i.e. after the Self type was stabilized) nonetheless, but this might be a small concern. See also this URLO thread of mine.

3 Likes

There are already some bad interactions between the Borrow traits and the RefCell methods with the same names, like:

Also, existing my_ref.borrow() calls might be trying to get some other Borrow<_> for T, not necessarily just &T. Maybe you could make that a generic pass-through too, but I wouldn't be too surprised if that overlaps impls.

1 Like

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