Skip to content

Commit c229666

Browse files
committed
Make CloneToUninit dyn-compatible
1 parent 33fa870 commit c229666

File tree

11 files changed

+39
-38
lines changed

11 files changed

+39
-38
lines changed

alloc/src/boxed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1735,7 +1735,7 @@ impl<T: Clone, A: Allocator + Clone> Clone for Box<T, A> {
17351735
// Pre-allocate memory to allow writing the cloned value directly.
17361736
let mut boxed = Self::new_uninit_in(self.1.clone());
17371737
unsafe {
1738-
(**self).clone_to_uninit(boxed.as_mut_ptr());
1738+
(**self).clone_to_uninit(boxed.as_mut_ptr().cast());
17391739
boxed.assume_init()
17401740
}
17411741
}

alloc/src/rc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1876,7 +1876,7 @@ impl<T: ?Sized + CloneToUninit, A: Allocator + Clone> Rc<T, A> {
18761876
// Initialize with clone of this.
18771877
let initialized_clone = unsafe {
18781878
// Clone. If the clone panics, `in_progress` will be dropped and clean up.
1879-
this_data_ref.clone_to_uninit(in_progress.data_ptr());
1879+
this_data_ref.clone_to_uninit(in_progress.data_ptr().cast());
18801880
// Cast type of pointer, now that it is initialized.
18811881
in_progress.into_rc()
18821882
};

alloc/src/sync.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2272,7 +2272,7 @@ impl<T: ?Sized + CloneToUninit, A: Allocator + Clone> Arc<T, A> {
22722272

22732273
let initialized_clone = unsafe {
22742274
// Clone. If the clone panics, `in_progress` will be dropped and clean up.
2275-
this_data_ref.clone_to_uninit(in_progress.data_ptr());
2275+
this_data_ref.clone_to_uninit(in_progress.data_ptr().cast());
22762276
// Cast type of pointer, now that it is initialized.
22772277
in_progress.into_arc()
22782278
};

core/src/clone.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -232,20 +232,20 @@ pub struct AssertParamIsCopy<T: Copy + ?Sized> {
232232
pub unsafe trait CloneToUninit {
233233
/// Performs copy-assignment from `self` to `dst`.
234234
///
235-
/// This is analogous to `std::ptr::write(dst, self.clone())`,
235+
/// This is analogous to `std::ptr::write(dst.cast(), self.clone())`,
236236
/// except that `self` may be a dynamically-sized type ([`!Sized`](Sized)).
237237
///
238238
/// Before this function is called, `dst` may point to uninitialized memory.
239239
/// After this function is called, `dst` will point to initialized memory; it will be
240-
/// sound to create a `&Self` reference from the pointer.
240+
/// sound to create a `&Self` reference from the pointer with the [pointer metadata]
241+
/// from `self`.
241242
///
242243
/// # Safety
243244
///
244245
/// Behavior is undefined if any of the following conditions are violated:
245246
///
246-
/// * `dst` must be [valid] for writes.
247-
/// * `dst` must be properly aligned.
248-
/// * `dst` must have the same [pointer metadata] (slice length or `dyn` vtable) as `self`.
247+
/// * `dst` must be [valid] for writes for `std::mem::size_of_val(self)` bytes.
248+
/// * `dst` must be properly aligned to `std::mem::align_of_val(self)`.
249249
///
250250
/// [valid]: crate::ptr#safety
251251
/// [pointer metadata]: crate::ptr::metadata()
@@ -266,23 +266,24 @@ pub unsafe trait CloneToUninit {
266266
/// that might have already been created. (For example, if a `[Foo]` of length 3 is being
267267
/// cloned, and the second of the three calls to `Foo::clone()` unwinds, then the first `Foo`
268268
/// cloned should be dropped.)
269-
unsafe fn clone_to_uninit(&self, dst: *mut Self);
269+
unsafe fn clone_to_uninit(&self, dst: *mut u8);
270270
}
271271

272272
#[unstable(feature = "clone_to_uninit", issue = "126799")]
273273
unsafe impl<T: Clone> CloneToUninit for T {
274274
#[inline]
275-
unsafe fn clone_to_uninit(&self, dst: *mut Self) {
275+
unsafe fn clone_to_uninit(&self, dst: *mut u8) {
276276
// SAFETY: we're calling a specialization with the same contract
277-
unsafe { <T as self::uninit::CopySpec>::clone_one(self, dst) }
277+
unsafe { <T as self::uninit::CopySpec>::clone_one(self, dst.cast::<T>()) }
278278
}
279279
}
280280

281281
#[unstable(feature = "clone_to_uninit", issue = "126799")]
282282
unsafe impl<T: Clone> CloneToUninit for [T] {
283283
#[inline]
284284
#[cfg_attr(debug_assertions, track_caller)]
285-
unsafe fn clone_to_uninit(&self, dst: *mut Self) {
285+
unsafe fn clone_to_uninit(&self, dst: *mut u8) {
286+
let dst: *mut [T] = dst.with_metadata_of(self);
286287
// SAFETY: we're calling a specialization with the same contract
287288
unsafe { <T as self::uninit::CopySpec>::clone_slice(self, dst) }
288289
}
@@ -292,21 +293,21 @@ unsafe impl<T: Clone> CloneToUninit for [T] {
292293
unsafe impl CloneToUninit for str {
293294
#[inline]
294295
#[cfg_attr(debug_assertions, track_caller)]
295-
unsafe fn clone_to_uninit(&self, dst: *mut Self) {
296+
unsafe fn clone_to_uninit(&self, dst: *mut u8) {
296297
// SAFETY: str is just a [u8] with UTF-8 invariant
297-
unsafe { self.as_bytes().clone_to_uninit(dst as *mut [u8]) }
298+
unsafe { self.as_bytes().clone_to_uninit(dst) }
298299
}
299300
}
300301

301302
#[unstable(feature = "clone_to_uninit", issue = "126799")]
302303
unsafe impl CloneToUninit for crate::ffi::CStr {
303304
#[cfg_attr(debug_assertions, track_caller)]
304-
unsafe fn clone_to_uninit(&self, dst: *mut Self) {
305+
unsafe fn clone_to_uninit(&self, dst: *mut u8) {
305306
// SAFETY: For now, CStr is just a #[repr(trasnsparent)] [c_char] with some invariants.
306307
// And we can cast [c_char] to [u8] on all supported platforms (see: to_bytes_with_nul).
307-
// The pointer metadata properly preserves the length (NUL included).
308+
// The pointer metadata properly preserves the length (so NUL is also copied).
308309
// See: `cstr_metadata_is_length_with_nul` in tests.
309-
unsafe { self.to_bytes_with_nul().clone_to_uninit(dst as *mut [u8]) }
310+
unsafe { self.to_bytes_with_nul().clone_to_uninit(dst) }
310311
}
311312
}
312313

std/src/ffi/os_str.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl crate::sealed::Sealed for OsString {}
112112
/// [conversions]: super#conversions
113113
#[cfg_attr(not(test), rustc_diagnostic_item = "OsStr")]
114114
#[stable(feature = "rust1", since = "1.0.0")]
115-
// `OsStr::from_inner` current implementation relies
115+
// `OsStr::from_inner` and `impl CloneToUninit for OsStr` current implementation relies
116116
// on `OsStr` being layout-compatible with `Slice`.
117117
// However, `OsStr` layout is considered an implementation detail and must not be relied upon.
118118
#[repr(transparent)]
@@ -1278,9 +1278,9 @@ impl Clone for Box<OsStr> {
12781278
unsafe impl CloneToUninit for OsStr {
12791279
#[inline]
12801280
#[cfg_attr(debug_assertions, track_caller)]
1281-
unsafe fn clone_to_uninit(&self, dst: *mut Self) {
1282-
// SAFETY: we're just a wrapper around a platform-specific Slice
1283-
unsafe { self.inner.clone_to_uninit(&raw mut (*dst).inner) }
1281+
unsafe fn clone_to_uninit(&self, dst: *mut u8) {
1282+
// SAFETY: we're just a transparent wrapper around a platform-specific Slice
1283+
unsafe { self.inner.clone_to_uninit(dst) }
12841284
}
12851285
}
12861286

std/src/ffi/os_str/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,12 @@ fn clone_to_uninit() {
294294
let a = OsStr::new("hello.txt");
295295

296296
let mut storage = vec![MaybeUninit::<u8>::uninit(); size_of_val::<OsStr>(a)];
297-
unsafe { a.clone_to_uninit(ptr::from_mut::<[_]>(storage.as_mut_slice()) as *mut OsStr) };
297+
unsafe { a.clone_to_uninit(ptr::from_mut::<[_]>(storage.as_mut_slice()).cast()) };
298298
assert_eq!(a.as_encoded_bytes(), unsafe { MaybeUninit::slice_assume_init_ref(&storage) });
299299

300300
let mut b: Box<OsStr> = OsStr::new("world.exe").into();
301301
assert_eq!(size_of_val::<OsStr>(a), size_of_val::<OsStr>(&b));
302302
assert_ne!(a, &*b);
303-
unsafe { a.clone_to_uninit(ptr::from_mut::<OsStr>(&mut b)) };
303+
unsafe { a.clone_to_uninit(ptr::from_mut::<OsStr>(&mut b).cast()) };
304304
assert_eq!(a, &*b);
305305
}

std/src/path.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2128,7 +2128,7 @@ impl AsRef<OsStr> for PathBuf {
21282128
/// ```
21292129
#[cfg_attr(not(test), rustc_diagnostic_item = "Path")]
21302130
#[stable(feature = "rust1", since = "1.0.0")]
2131-
// `Path::new` current implementation relies
2131+
// `Path::new` and `impl CloneToUninit for Path` current implementation relies
21322132
// on `Path` being layout-compatible with `OsStr`.
21332133
// However, `Path` layout is considered an implementation detail and must not be relied upon.
21342134
#[repr(transparent)]
@@ -3170,9 +3170,9 @@ impl Path {
31703170
unsafe impl CloneToUninit for Path {
31713171
#[inline]
31723172
#[cfg_attr(debug_assertions, track_caller)]
3173-
unsafe fn clone_to_uninit(&self, dst: *mut Self) {
3174-
// SAFETY: Path is just a wrapper around OsStr
3175-
unsafe { self.inner.clone_to_uninit(&raw mut (*dst).inner) }
3173+
unsafe fn clone_to_uninit(&self, dst: *mut u8) {
3174+
// SAFETY: Path is just a transparent wrapper around OsStr
3175+
unsafe { self.inner.clone_to_uninit(dst) }
31763176
}
31773177
}
31783178

std/src/path/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2068,14 +2068,14 @@ fn clone_to_uninit() {
20682068
let a = Path::new("hello.txt");
20692069

20702070
let mut storage = vec![MaybeUninit::<u8>::uninit(); size_of_val::<Path>(a)];
2071-
unsafe { a.clone_to_uninit(ptr::from_mut::<[_]>(storage.as_mut_slice()) as *mut Path) };
2071+
unsafe { a.clone_to_uninit(ptr::from_mut::<[_]>(storage.as_mut_slice()).cast()) };
20722072
assert_eq!(a.as_os_str().as_encoded_bytes(), unsafe {
20732073
MaybeUninit::slice_assume_init_ref(&storage)
20742074
});
20752075

20762076
let mut b: Box<Path> = Path::new("world.exe").into();
20772077
assert_eq!(size_of_val::<Path>(a), size_of_val::<Path>(&b));
20782078
assert_ne!(a, &*b);
2079-
unsafe { a.clone_to_uninit(ptr::from_mut::<Path>(&mut b)) };
2079+
unsafe { a.clone_to_uninit(ptr::from_mut::<Path>(&mut b).cast()) };
20802080
assert_eq!(a, &*b);
20812081
}

std/src/sys/os_str/bytes.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ impl Slice {
352352
unsafe impl CloneToUninit for Slice {
353353
#[inline]
354354
#[cfg_attr(debug_assertions, track_caller)]
355-
unsafe fn clone_to_uninit(&self, dst: *mut Self) {
356-
// SAFETY: we're just a wrapper around [u8]
357-
unsafe { self.inner.clone_to_uninit(&raw mut (*dst).inner) }
355+
unsafe fn clone_to_uninit(&self, dst: *mut u8) {
356+
// SAFETY: we're just a transparent wrapper around [u8]
357+
unsafe { self.inner.clone_to_uninit(dst) }
358358
}
359359
}

std/src/sys/os_str/wtf8.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ impl Slice {
275275
unsafe impl CloneToUninit for Slice {
276276
#[inline]
277277
#[cfg_attr(debug_assertions, track_caller)]
278-
unsafe fn clone_to_uninit(&self, dst: *mut Self) {
279-
// SAFETY: we're just a wrapper around Wtf8
280-
unsafe { self.inner.clone_to_uninit(&raw mut (*dst).inner) }
278+
unsafe fn clone_to_uninit(&self, dst: *mut u8) {
279+
// SAFETY: we're just a transparent wrapper around Wtf8
280+
unsafe { self.inner.clone_to_uninit(dst) }
281281
}
282282
}

std/src/sys_common/wtf8.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1052,8 +1052,8 @@ impl Hash for Wtf8 {
10521052
unsafe impl CloneToUninit for Wtf8 {
10531053
#[inline]
10541054
#[cfg_attr(debug_assertions, track_caller)]
1055-
unsafe fn clone_to_uninit(&self, dst: *mut Self) {
1056-
// SAFETY: we're just a wrapper around [u8]
1057-
unsafe { self.bytes.clone_to_uninit(&raw mut (*dst).bytes) }
1055+
unsafe fn clone_to_uninit(&self, dst: *mut u8) {
1056+
// SAFETY: we're just a transparent wrapper around [u8]
1057+
unsafe { self.bytes.clone_to_uninit(dst) }
10581058
}
10591059
}

0 commit comments

Comments
 (0)