Description
This is how the language reference describes the cast experssions:
Executing an as expression casts the value on the left-hand side to the type on the right-hand side.
A numeric value can be cast to any numeric type. A raw pointer value can be cast to or from any integral type or raw pointer type. Any other cast is unsupported and will fail to compile.
However, grepping for as .* as
in src/libnative
, for example, finds many instances of cast expressions which deviate from the above description. For example
let mut storage = unsafe { mem::zeroed() };
(...snip...)
let addrp = &storage as *const _ as *const libc::sockaddr;
- It seems like one can cast a refernce into a raw pointer. I assume that a reference is not of an "integral type," though the reference doesn't define what is an "integral type."
- The use of underscore in the cast expression seems to play the role of
const_cast
in C++ but is not described anywhere.
As for the above particular instance, the combination of ref_slice()
and as_ptr()
seems "more standard-compliant" to me and makes me wonder if the above special casts are really essential to Rust (ref_slice()
is built on top of the magical intrinsic transmute()
).