Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
CAD97 opened this issue Aug 15, 2020 · 0 comments
Open

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

CAD97 opened this issue Aug 15, 2020 · 0 comments

Comments

@CAD97
Copy link

CAD97 commented Aug 15, 2020

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.)

@dtolnay dtolnay changed the title This relies on compiler-internal details, but won't have to in the future Use <*mut T>::set_ptr_value when available Aug 15, 2020
@dtolnay dtolnay changed the title Use <*mut T>::set_ptr_value when available Use <*mut T>::with_metadata_of when available Jul 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant