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.