Implementing Borrow for Pin<T>

Is there a reason why the Borrow trait is not implemented for Pin<T> types?

Let's say I want to store Pin<Box<str>> objects in the BTreeSet and look up by the &str reference. With the current API, I'm obliged to create a new Pin<Box<str>> by allocating a copy of the string on the heap only to pass a reference to it to the BTreeSet::get method. Is there a way to perform the lookup in this case without allocating new memory?

4 Likes

(NOT A CONTRIBUTION)

There is no reason it is not implemented. This impl would be sound and I expect a welcome addition.

5 Likes

Pin is #[fundamental] though, so it would be a breaking change to add a blanket impl for Pin<T>. We could add specific impls like Pin<Box<str>> though.

e.g. downstream impl Borrow<Foo> for Pin<Foo> is currently allowed.

8 Likes