State of postfix borrow/deref

One situation I had quite recently where a postfix borrow would have been nice, is when you want to call a trait method on a field of &self with type T that is implemented both for T and &T and you need to specify that you want to use the latter implementation, because you can't move out of self. The only way to do this right now is with quite a lot of parentheses.

struct Foo(Bar);

struct Bar(i32);

impl From<Bar> for i32 {
    fn from(val: Bar) -> Self {
        val.0
    }
}

impl From<&Bar> for i32 {
    fn from(val: &Bar) -> Self {
        val.0
    }
}

impl Foo {
    fn plus_one(&self) -> i32 {
        // I think this could be: self.0&.into();
        let val: i32 = (&(self.0)).into();
        val + 1
    }
}

This came up with strum_macros::IntoStaticStr where I wanted a method that uses the generated string and returns it in a different casing.

I remember a case where I had a trait implemented for both a type and its reference. In this case both owned.borrow().foo() (after importing Borrow) and (&owned).foo() work to call the trait on the reference, and it was rare enough that it did not really need to be more concise.

1 Like

Really want postfix dereferences due to 2 use cases:

  • unsafe code
  • yew used antipattern with implementing Deref and something else on a type (Clone for example) so I am constantly writing stuff like (*var).clone() (in yew you need to clone very often, as stuff is reference counted and rust does not have something like Capture or Claim trait that will clone automatically when captured to some closure)