Idea: types which are repr(transparent) whose can be constructed in the current scope should be coercible via as. Going to post this here because I don't really have the time to write an RFC or file an MCP, but this is something I've thought for a while and haven't really seen concretely mentioned anywhere.
Example:
mod scope {
#[repr(transparent)]
pub struct Wrapper<T>(T);
impl<T> Wrapper<T> {
pub fn wrap(val: &T) -> &Wrapper<T> {
val as &Wrapper<T> // okay because constructor `Wrapper` is accessible here
}
pub fn wrap_slice(slice: &[T]) -> &[Wrapper<T>] {
val as &[Wrapper<T>] // also okay to coerce inside types
}
}
}
fn cant_wrap<T>(val: &T) -> &Wrapper<T> {
val as &Wrapper<T> // will fail because constructor `Wrapper` isn't accessible here
}
Essentially, right now, this is defined to not invoke undefined behaviour, ever, due to the presence of repr(transparent). However, there is no way to actually do this with safe code. The closest you can do is pointer casts, which ultimately still require an unsafe block to convert the pointers back into valid references, or transmutes, which are inherently unsafe.
I know that there are plans to make safe transmutation work, but I feel like this kind of coercion would also be nice to have.
This can basically be done via Trait, other than the part where the as keyword is used.
And we shouldn't be adding more uses of as to the language, we should be adding ways to get away from the as keyword.
That exact trait you mention involves unsafe code, which is why I bring this up. There should be some way to do this with safe code only.
I disagree that we should be moving away from the as operator in all cases; this is a perfectly good use case for it IMHO. How would you suggest a trait to work in all cases, and also manage coercions inside types? The alternative would be to make the coercion automatic, which I don't think is a good idea.
Most helpful comment
That exact trait you mention involves unsafe code, which is why I bring this up. There should be some way to do this with safe code only.
I disagree that we should be moving away from the
asoperator in all cases; this is a perfectly good use case for it IMHO. How would you suggest a trait to work in all cases, and also manage coercions inside types? The alternative would be to make the coercion automatic, which I don't think is a good idea.