impl CloneToUninit for Path and OsStr

This commit is contained in:
Pavel Grigorenko
2024-06-23 23:05:10 +03:00
parent ec921db289
commit afabc583f7
8 changed files with 95 additions and 0 deletions

View File

@@ -3,6 +3,8 @@ use core::hint::black_box;
use super::*;
use crate::collections::{BTreeSet, HashSet};
use crate::hash::DefaultHasher;
use crate::mem::MaybeUninit;
use crate::ptr;
#[allow(unknown_lints, unused_macro_rules)]
macro_rules! t (
@@ -2054,3 +2056,20 @@ fn bench_hash_path_long(b: &mut test::Bencher) {
black_box(hasher.finish());
}
#[test]
fn clone_to_uninit() {
let a = Path::new("hello.txt");
let mut storage = vec![MaybeUninit::<u8>::uninit(); size_of_val::<Path>(a)];
unsafe { a.clone_to_uninit(ptr::from_mut::<[_]>(storage.as_mut_slice()) as *mut Path) };
assert_eq!(a.as_os_str().as_encoded_bytes(), unsafe {
MaybeUninit::slice_assume_init_ref(&storage)
});
let mut b: Box<Path> = Path::new("world.exe").into();
assert_eq!(size_of_val::<Path>(a), size_of_val::<Path>(&b));
assert_ne!(a, &*b);
unsafe { a.clone_to_uninit(ptr::from_mut::<Path>(&mut b)) };
assert_eq!(a, &*b);
}