Skip to content

Use <*mut T>::with_metadata_of when available #8

Open
@CAD97

Description

@CAD97

The needed API just hit nightly: <*mut T>::set_ptr_value(self, *mut u8) -> *mut T

The problematic section:

dyn-clone/src/lib.rs

Lines 109 to 113 in 754c7c7

unsafe {
let data_ptr = &mut fat_ptr as *mut *const T as *mut *mut ();
assert_eq!(*data_ptr as *const (), t as *const T as *const ());
*data_ptr = <T as DynClone>::clone_box(t);
}

As implemented, this relies on the fact that the layout of fat pointers puts the data pointer first and the fat part second. This is currently true, and the way that the compiler/stdlib implement this functionality, but could potentially change in the future. Whatever form set_ptr_value ends up stabilizing in will be the sound way to accomplish this.

pub fn clone_box<T>(t: &T) -> Box<T>
where
    T: ?Sized + DynClone,
{
    let raw_clone = (t as *const T).set_ptr_value(<T as DynClone>::clone_box(t) as *const u8);
    unsafe {
        Box::from_raw(raw_clone as *mut T)
    }
}

(Since DynClone::clone_box is a private non-public API, it'll probably want to change its signature to match whatever the argument for set_ptr_value settles on.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions