How to enable Deref coercion when disambiguating a method call?


#1

Playground link

I want to disambiguate a method call (this is for use in a macro), but doing so seems to disable deref coercion, which makes things very painful. Is there any way to call a method on an object (or expression) and have the wonderful deref coercion take place, but to also disambiguate the method?


#2

I hope there’s a better way, but…

In your macro, you could define a local trait with an less ambiguous method name, forwarding to the real thing. It’s cumbersome, but perhaps OK in a macro… playground link


#3

I got it to (somewhat) work by using an identity trait:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e73d60d663ca15993fb0752975ecbf82


#4

I don’t know a good way. A possibly interesting discussion:


#5

That’s brilliant! Thanks so much! I doubt I ever would have thought to create a throwaway local trait like that. I’ll have to keep that trick in mind for the future.

It’s also pretty ugly, but hey, it’s all hidden within the macro and should have no runtime overhead. And if I ever find out a prettier way to implement it there’s no API change.

Speaking of overhead, is there any particular reason why you annotated the conversion as inline? I’m accustomed to trusting the compiler to do such things automatically.


#6

Of course, to really have a throwaway local trait you need to do: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=83911efad2e5946d5aea4045a99c15e8

< /wink>


#7

Yes, that is what I ended up doing. :blush: