dtolnay / dtolnay/dyn-clone

Use <*mut T>::with_metadata_of when available

Open
#8 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Rust
Stars
755
Forks
24
PR merge metrics
No merged PRs in 30d

Description

The needed API just hit nightly: [`<*mut T>::set_ptr_value(self, *mut u8) -> *mut T`](https://doc.rust-lang.org/nightly/std/primitive.pointer.html#method.set_ptr_value-1)

The problematic section: https://github.com/dtolnay/dyn-clone/blob/754c7c769e497cd587ed2a028ec0963fe196e746/src/lib.rs#L109-L113

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.

```rust
pub fn clone_box(t: &T) -> Box
where
T: ?Sized + DynClone,
{
let raw_clone = (t as *const T).set_ptr_value(::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.)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.