0

I am trying to create a wrapper type around a cxx::UniquePtr<A>.

I want to implement struct B(UniquePtr<A>) with

Deref<Target=UniquePtr<A>> for B

but also

DerefMut<Target=Pin<&mut A>> for B {}

Is this a terrible idea or even possible? The whole point of B is to hide the type complexity of UniquePtr<A>. Right now I am forcing my users to call .pin_mut() every time they want to do something and it is quite ugly and breaks their code auto completion. Does anyone have any suggestions or resources I should look at to clean up the interface?

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
  • 2
    I don't think that is possible. `DerefMut` does not define a type `Target`, it _inherits_ it from the required `Deref` implementation, so as I understand it, `::Target` and `::Target` must be the same type. – rodrigo Sep 08 '22 at 22:02
  • 1
    It's both impossible and a bad idea. `&*x` and `&mut *x` should never result in a reference to different types. That would be horribly confusing. – cdhowie Sep 08 '22 at 22:23
  • 1
    And `.deref_mut()` must yield a `&mut Target`. So you can't wrap the return type in a `Pin` anyway (unless you did `&mut Pin<&mut A>` which would not be desirable either). – kmdreko Sep 08 '22 at 23:01

0 Answers0