Skip to content

Commit ef8c591

Browse files
committed
impl CloneToUninit for str and CStr
1 parent 618fdd5 commit ef8c591

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

Diff for: core/src/clone.rs

+21
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,27 @@ unsafe impl<T: Copy> CloneToUninit for [T] {
346346
}
347347
}
348348

349+
#[unstable(feature = "clone_to_uninit", issue = "126799")]
350+
unsafe impl CloneToUninit for str {
351+
#[cfg_attr(debug_assertions, track_caller)]
352+
unsafe fn clone_to_uninit(&self, dst: *mut Self) {
353+
// SAFETY: str is just a [u8] with UTF-8 invariant
354+
unsafe { self.as_bytes().clone_to_uninit(dst as *mut [u8]) }
355+
}
356+
}
357+
358+
#[unstable(feature = "clone_to_uninit", issue = "126799")]
359+
unsafe impl CloneToUninit for crate::ffi::CStr {
360+
#[cfg_attr(debug_assertions, track_caller)]
361+
unsafe fn clone_to_uninit(&self, dst: *mut Self) {
362+
// SAFETY: For now, CStr is just a #[repr(trasnsparent)] [c_char] with some invariants.
363+
// And we can cast [c_char] to [u8] on all supported platforms (see: to_bytes_with_nul).
364+
// The pointer metadata properly preserves the length (NUL included).
365+
// See: `cstr_metadata_is_length_with_nul` in tests.
366+
unsafe { self.to_bytes_with_nul().clone_to_uninit(dst as *mut [u8]) }
367+
}
368+
}
369+
349370
/// Ownership of a collection of values stored in a non-owned `[MaybeUninit<T>]`, some of which
350371
/// are not yet initialized. This is sort of like a `Vec` that doesn't own its allocation.
351372
/// Its responsibility is to provide cleanup on unwind by dropping the values that *are*

Diff for: core/tests/clone.rs

+40
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use core::clone::CloneToUninit;
2+
use core::ffi::CStr;
23
use core::mem::MaybeUninit;
4+
use core::ptr;
35

46
#[test]
57
#[allow(suspicious_double_ref_op)]
@@ -81,3 +83,41 @@ fn test_clone_to_uninit_slice_drops_on_panic() {
8183
drop(a);
8284
assert_eq!(COUNTER.load(Relaxed), 0);
8385
}
86+
87+
#[test]
88+
fn test_clone_to_uninit_str() {
89+
let a = "hello";
90+
91+
let mut storage: MaybeUninit<[u8; 5]> = MaybeUninit::uninit();
92+
unsafe { a.clone_to_uninit(storage.as_mut_ptr() as *mut [u8] as *mut str) };
93+
assert_eq!(a.as_bytes(), unsafe { storage.assume_init() }.as_slice());
94+
95+
let mut b: Box<str> = "world".into();
96+
assert_eq!(a.len(), b.len());
97+
assert_ne!(a, &*b);
98+
unsafe { a.clone_to_uninit(ptr::from_mut::<str>(&mut b)) };
99+
assert_eq!(a, &*b);
100+
}
101+
102+
#[test]
103+
fn test_clone_to_uninit_cstr() {
104+
let a = c"hello";
105+
106+
let mut storage: MaybeUninit<[u8; 6]> = MaybeUninit::uninit();
107+
unsafe { a.clone_to_uninit(storage.as_mut_ptr() as *mut [u8] as *mut CStr) };
108+
assert_eq!(a.to_bytes_with_nul(), unsafe { storage.assume_init() }.as_slice());
109+
110+
let mut b: Box<CStr> = c"world".into();
111+
assert_eq!(a.count_bytes(), b.count_bytes());
112+
assert_ne!(a, &*b);
113+
unsafe { a.clone_to_uninit(ptr::from_mut::<CStr>(&mut b)) };
114+
assert_eq!(a, &*b);
115+
}
116+
117+
#[test]
118+
fn cstr_metadata_is_length_with_nul() {
119+
let s: &CStr = c"abcdef";
120+
let p: *const CStr = ptr::from_ref(s);
121+
let bytes: *const [u8] = p as *const [u8];
122+
assert_eq!(s.to_bytes_with_nul().len(), bytes.len());
123+
}

0 commit comments

Comments
 (0)