I know that the Deref pattern is considered an anti-pattern and that it doesn’t work well with multiple Deref targets (a problem with multiple inheritance anyway).
I was wondering if overloading casting could be a good solution.
Basically this would mean that the following is possible. Yes I know that it is more code then just selecting the field. But if for whatever reason you wanted to do dynamic cast selection this would be able to facilitate that.
The above quote doesn’t mean that Deref itself is an anti-pattern, just that simulating inheritance using Deref is.
Anyway, the standard ways of function-based type conversion are:
Deref::deref (as mentioned above, and which already has language support in the form of coercions);
AsRef::as_ref (which doesn’t have that and is thus always explicit);
Borrow::borrow which has some overlap with as_ref but is specifically designed for conversion between types which are semantically “the same” but one is owned (e.g. String -> &str but not String -> &[u8]);
ToOwned::to_owned when the conversion happens from a reference-like type to an owned/value-like type; and
From::from / Into::into for many general conversions.
I understand that that what you're saying. What I don't understand is why? What motivates this?
Please understand that "it might be a good idea" on its own, without context, is not a very good reason.