#Is this usage of transmute sound?

10 messages · Page 1 of 1 (latest)

spring minnow
#
#[repr(transparent)]
pub struct Object(pub(crate) NonNull<uacpi_sys::uacpi_object>);

pub impl Object {

  pub fn new_uninitialized() -> Option<Self> {
    unsafe { transmute(uacpi_sys::uacpi_object_create_uninitialized()) }
  }

}

uacpi_sys::uacpi_object_create_uninitialized() is a extern function created by bindgen

uacpi_sys::uacpi_object is a ptr type to a opaque object in the c code.
The c code only hands out these pointer and interaction with the underlying object is done via function calls.
When creating a new object the c function can return a null ptr, is it safe to do this above?

rigid edge
#

is Self Object? if so, why the transmute?

spring minnow
#

yes, because i cant just cast uacpi_sys::uacpi_object(may be null) to Option<Object> directly

rigid edge
#

but can't you just do Some(Object(uacpi_sys::...()))?

#

or i guess do the appropriate null-handling

#

If you're wrapping a pointer, I don't see why you'd have to transmute anything

spring minnow
#
mismatched types
    expected enum `core::option::Option<types::Object>`
found raw pointer `*mut uacpi_sys::uacpi_object`rustcClick for full compiler diagnostic
types.rs(162, 35): expected `core::option::Option<types::Object>` because of return type
rigid edge
#
Some(Object(NonNull::new(uacpi_sys::...())?))
#

something like that?

#

but to answer your question: Don't transmute here, I don't think what you did is sound, though I only saw half the types.