Consider this toy function:

fn ptr_as_nonnull<T: ?Sized>(input: &*mut T) -> Option<&NonNull<T>> { if input.is_null() { None } else { // Is it possible to implement this without transmute? Some(unsafe { std::mem::transmute(input) }) } } 

Is that function sound? I know that NonNull is repr(transparent), but that function is transmuting a raw pointer ref as a NonNull ref, not a raw pointer value as a NonNull value.

Is it possible to implement that function without using std::mem::transmute()?

I can restructure my code (playground) to avoid this, but I'm curious.

2 posts - 2 participants

Read full topic