Why do unsized types have unsized pointers?

I'm currently trying to transmute pointer of an unsized type and the compiler is screaming at me for doing so. Does anyone know what this happens and what can be done to fix it?

Here's the code:

#[derive(Copy, Clone)]
pub struct VoidPointer { pub void: *const std::ffi::c_void }
impl VoidPointer { 
    pub fn new<T: 'static + ?Sized>(t: &T) -> Self { Self { void: unsafe { std::mem::transmute::<&T, *const std::ffi::c_void>(t) } }}
unsafe impl Send for VoidPointer {}

The error is:

cannot transmute between types of different sizes, or dependently-sized types
note: source type: `&T` (pointer to `T`)
note: target type: `*const std::ffi::c_void` (64 bits)rustc(E0512)

Does anyone know how this can be fixed?

This is because a pointer/reference to a !Sized type is bigger than a pointer to a Sized type. Consider &[_] as an example; the reference is a (pointer, length) pair.

What you probably want is as *const _ as *const c_void anyway, no transmute needed.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.