mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
impl CloneToUninit for Path and OsStr
This commit is contained in:
parent
ec921db289
commit
afabc583f7
@ -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;
|
||||
@ -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`]
|
||||
|
@ -1,4 +1,6 @@
|
||||
use super::*;
|
||||
use crate::mem::MaybeUninit;
|
||||
use crate::ptr;
|
||||
|
||||
#[test]
|
||||
fn test_os_string_with_capacity() {
|
||||
@ -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);
|
||||
}
|
||||
|
@ -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)]
|
||||
|
@ -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;
|
||||
@ -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]
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
@ -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)) }
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
@ -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)) }
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
@ -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)) }
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user