Skip to content

Commit

Permalink
impl CloneToUninit for Path and OsStr
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigorenkoPV committed Jul 29, 2024
1 parent ef8c591 commit e8c3718
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 0 deletions.
12 changes: 12 additions & 0 deletions std/src/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
#[cfg(test)]
mod tests;

use core::clone::CloneToUninit;

use crate::borrow::{Borrow, Cow};
use crate::collections::TryReserveError;
use crate::hash::{Hash, Hasher};
use crate::ops::{self, Range};
use crate::ptr::addr_of_mut;
use crate::rc::Rc;
use crate::str::FromStr;
use crate::sync::Arc;
Expand Down Expand Up @@ -1261,6 +1264,15 @@ impl Clone for Box<OsStr> {
}
}

#[unstable(feature = "clone_to_uninit", issue = "126799")]
unsafe impl CloneToUninit for OsStr {
#[cfg_attr(debug_assertions, track_caller)]
unsafe fn clone_to_uninit(&self, dst: *mut Self) {
// SAFETY: we're just a wrapper around a platform-specific Slice
unsafe { self.inner.clone_to_uninit(addr_of_mut!((*dst).inner)) }
}
}

#[stable(feature = "shared_from_slice2", since = "1.24.0")]
impl From<OsString> for Arc<OsStr> {
/// Converts an [`OsString`] into an <code>[Arc]<[OsStr]></code> by moving the [`OsString`]
Expand Down
17 changes: 17 additions & 0 deletions std/src/ffi/os_str/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use super::*;
use crate::mem::MaybeUninit;
use crate::ptr;

#[test]
fn test_os_string_with_capacity() {
Expand Down Expand Up @@ -286,3 +288,18 @@ fn slice_surrogate_edge() {
assert_eq!(post_crab.slice_encoded_bytes(..4), "🦀");
assert_eq!(post_crab.slice_encoded_bytes(4..), surrogate);
}

#[test]
fn clone_to_uninit() {
let a = OsStr::new("hello.txt");

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

let mut b: Box<OsStr> = OsStr::new("world.exe").into();
assert_eq!(size_of_val::<OsStr>(a), size_of_val::<OsStr>(&b));
assert_ne!(a, &*b);
unsafe { a.clone_to_uninit(ptr::from_mut::<OsStr>(&mut b)) };
assert_eq!(a, &*b);
}
1 change: 1 addition & 0 deletions std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@
// tidy-alphabetical-start
#![feature(c_str_module)]
#![feature(char_internals)]
#![feature(clone_to_uninit)]
#![feature(core_intrinsics)]
#![feature(core_io_borrowed_buf)]
#![feature(duration_constants)]
Expand Down
11 changes: 11 additions & 0 deletions std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
#[cfg(test)]
mod tests;

use core::clone::CloneToUninit;

use crate::borrow::{Borrow, Cow};
use crate::collections::TryReserveError;
use crate::error::Error;
Expand Down Expand Up @@ -3109,6 +3111,15 @@ impl Path {
}
}

#[unstable(feature = "clone_to_uninit", issue = "126799")]
unsafe impl CloneToUninit for Path {
#[cfg_attr(debug_assertions, track_caller)]
unsafe fn clone_to_uninit(&self, dst: *mut Self) {
// SAFETY: Path is just a wrapper around OsStr
unsafe { self.inner.clone_to_uninit(core::ptr::addr_of_mut!((*dst).inner)) }
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<OsStr> for Path {
#[inline]
Expand Down
19 changes: 19 additions & 0 deletions std/src/path/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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);
}
12 changes: 12 additions & 0 deletions std/src/sys/os_str/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! The underlying OsString/OsStr implementation on Unix and many other
//! systems: just a `Vec<u8>`/`[u8]`.
use core::clone::CloneToUninit;
use core::ptr::addr_of_mut;

use crate::borrow::Cow;
use crate::collections::TryReserveError;
use crate::fmt::Write;
Expand Down Expand Up @@ -345,3 +348,12 @@ impl Slice {
self.inner.eq_ignore_ascii_case(&other.inner)
}
}

#[unstable(feature = "clone_to_uninit", issue = "126799")]
unsafe impl CloneToUninit for Slice {
#[cfg_attr(debug_assertions, track_caller)]
unsafe fn clone_to_uninit(&self, dst: *mut Self) {
// SAFETY: we're just a wrapper around [u8]
unsafe { self.inner.clone_to_uninit(addr_of_mut!((*dst).inner)) }
}
}
12 changes: 12 additions & 0 deletions std/src/sys/os_str/wtf8.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! The underlying OsString/OsStr implementation on Windows is a
//! wrapper around the "WTF-8" encoding; see the `wtf8` module for more.
use core::clone::CloneToUninit;
use core::ptr::addr_of_mut;

use crate::borrow::Cow;
use crate::collections::TryReserveError;
use crate::rc::Rc;
Expand Down Expand Up @@ -268,3 +271,12 @@ impl Slice {
self.inner.eq_ignore_ascii_case(&other.inner)
}
}

#[unstable(feature = "clone_to_uninit", issue = "126799")]
unsafe impl CloneToUninit for Slice {
#[cfg_attr(debug_assertions, track_caller)]
unsafe fn clone_to_uninit(&self, dst: *mut Self) {
// SAFETY: we're just a wrapper around Wtf8
unsafe { self.inner.clone_to_uninit(addr_of_mut!((*dst).inner)) }
}
}
11 changes: 11 additions & 0 deletions std/src/sys_common/wtf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
mod tests;

use core::char::{encode_utf16_raw, encode_utf8_raw};
use core::clone::CloneToUninit;
use core::str::next_code_point;

use crate::borrow::Cow;
use crate::collections::TryReserveError;
use crate::hash::{Hash, Hasher};
use crate::iter::FusedIterator;
use crate::ptr::addr_of_mut;
use crate::rc::Rc;
use crate::sync::Arc;
use crate::sys_common::AsInner;
Expand Down Expand Up @@ -1046,3 +1048,12 @@ impl Hash for Wtf8 {
0xfeu8.hash(state)
}
}

#[unstable(feature = "clone_to_uninit", issue = "126799")]
unsafe impl CloneToUninit for Wtf8 {
#[cfg_attr(debug_assertions, track_caller)]
unsafe fn clone_to_uninit(&self, dst: *mut Self) {
// SAFETY: we're just a wrapper around [u8]
unsafe { self.bytes.clone_to_uninit(addr_of_mut!((*dst).bytes)) }
}
}

0 comments on commit e8c3718

Please sign in to comment.