I have a type (enum) that doesn't store any reference to another type but can be converted to a static reference to it (by matching on variants and returning the appropriate static). Would it still make sense to implement AsRef
?
It feels like AsRef
should be used whenever the reference is stored in the type itself but nothing in the documentation says so. Yeah, it technically works, I'm more concerned about it being potentially surprising/confusing.
Note that in my case a trait is actually needed anyway - some functions should be able to accept both types, internally converting, so the question can be viewed as "use AsRef
or a custom trait?" but I'm also interested in general cases. (A possible answer is "Yeah, it's confusing, you shouldn't do it most of the time but making your own trait is worse".)
One pretty good reason to use AsRef
is it also gives conversions from boxes, arcs and other stuff, including containers defined by external crates (presumably those implement these standard traits).
Do you think you'd be surprised/confused if you saw such code? Do you have any interesting arguments for/against?