Hello.
The following code fails to compile:
fn main() {
let a: Option<u16> = Some(123);
let b: *const u16 = a.as_ref().into();
}
With the following error:
error[E0277]: the trait bound `*const u16: From<Option<&u16>>` is not satisfied
--> src/main.rs:3:36
|
3 | let b: *const u16 = a.as_ref().into();
| ^^^^ the trait `From<Option<&u16>>` is not implemented for `*const u16`
|
= help: the following other types implement trait `From<T>`:
<u16 as From<bool>>
<u16 as From<u8>>
<u16 as From<NonZeroU16>>
= note: required for `Option<&u16>` to implement `Into<*const u16>`
Some time ago I think I saw in some rust code a transparent convertion from Option<&T> to *const T, which was used for passing a reference to a ffi function accepting a pointer. I might be wrong though.
Is there a way to convert Option<&T> to *const T? Is there such convertion in the standard library:
impl<T> From<Option<&T>> for *const T {
fn from(value: Option<&T>) -> Self {
match value {
Some(v) => v as *const T,
None => core::ptr::null(),
}
}
}