Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions library/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ dependencies = [

[[package]]
name = "libc"
version = "0.2.183"
version = "0.2.184"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5b646652bf6661599e1da8901b3b9522896f01e736bad5f723fe7a3a27f899d"
checksum = "48f5d2a454e16a5ea0f4ced81bd44e4cfc7bd3a507b61887c99fd3538b28e4af"
dependencies = [
"rustc-std-workspace-core",
]
Expand Down
50 changes: 30 additions & 20 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,26 +1006,6 @@ impl<T> Box<[T]> {
};
unsafe { Ok(RawVec::from_raw_parts_in(ptr.as_ptr(), len, Global).into_box(len)) }
}

/// Converts the boxed slice into a boxed array.
///
/// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
///
/// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
#[unstable(feature = "alloc_slice_into_array", issue = "148082")]
#[inline]
#[must_use]
pub fn into_array<const N: usize>(self) -> Option<Box<[T; N]>> {
if self.len() == N {
let ptr = Self::into_raw(self) as *mut [T; N];

// SAFETY: The underlying array of a slice has the exact same layout as an actual array `[T; N]` if `N` is equal to the slice's length.
let me = unsafe { Box::from_raw(ptr) };
Some(me)
} else {
None
}
}
}

impl<T, A: Allocator> Box<[T], A> {
Expand Down Expand Up @@ -1157,6 +1137,36 @@ impl<T, A: Allocator> Box<[T], A> {
};
unsafe { Ok(RawVec::from_raw_parts_in(ptr.as_ptr(), len, alloc).into_box(len)) }
}

/// Converts the boxed slice into a boxed array.
///
/// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
///
/// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
///
/// # Examples
///
/// ```
/// #![feature(alloc_slice_into_array)]
/// let box_slice: Box<[i32]> = Box::new([1, 2, 3]);
///
/// let box_array: Box<[i32; 3]> = box_slice.into_array().unwrap();
/// ```
#[unstable(feature = "alloc_slice_into_array", issue = "148082")]
#[inline]
#[must_use]
pub fn into_array<const N: usize>(self) -> Option<Box<[T; N], A>> {
if self.len() == N {
let (ptr, alloc) = Self::into_raw_with_allocator(self);
let ptr = ptr as *mut [T; N];

// SAFETY: The underlying array of a slice has the exact same layout as an actual array `[T; N]` if `N` is equal to the slice's length.
let me = unsafe { Box::from_raw_in(ptr, alloc) };
Some(me)
} else {
None
}
}
}

impl<T, A: Allocator> Box<mem::MaybeUninit<T>, A> {
Expand Down
52 changes: 32 additions & 20 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,26 +1167,6 @@ impl<T> Rc<[T]> {
))
}
}

/// Converts the reference-counted slice into a reference-counted array.
///
/// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
///
/// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
#[unstable(feature = "alloc_slice_into_array", issue = "148082")]
#[inline]
#[must_use]
pub fn into_array<const N: usize>(self) -> Option<Rc<[T; N]>> {
if self.len() == N {
let ptr = Self::into_raw(self) as *const [T; N];

// SAFETY: The underlying array of a slice has the exact same layout as an actual array `[T; N]` if `N` is equal to the slice's length.
let me = unsafe { Rc::from_raw(ptr) };
Some(me)
} else {
None
}
}
}

impl<T, A: Allocator> Rc<[T], A> {
Expand Down Expand Up @@ -1260,6 +1240,38 @@ impl<T, A: Allocator> Rc<[T], A> {
)
}
}

/// Converts the reference-counted slice into a reference-counted array.
///
/// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
///
/// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
///
/// # Examples
///
/// ```
/// #![feature(alloc_slice_into_array)]
/// use std::rc::Rc;
///
/// let rc_slice: Rc<[i32]> = Rc::new([1, 2, 3]);
///
/// let rc_array: Rc<[i32; 3]> = rc_slice.into_array().unwrap();
/// ```
#[unstable(feature = "alloc_slice_into_array", issue = "148082")]
#[inline]
#[must_use]
pub fn into_array<const N: usize>(self) -> Option<Rc<[T; N], A>> {
if self.len() == N {
let (ptr, alloc) = Self::into_raw_with_allocator(self);
let ptr = ptr as *const [T; N];

// SAFETY: The underlying array of a slice has the exact same layout as an actual array `[T; N]` if `N` is equal to the slice's length.
let me = unsafe { Rc::from_raw_in(ptr, alloc) };
Some(me)
} else {
None
}
}
}

impl<T, A: Allocator> Rc<mem::MaybeUninit<T>, A> {
Expand Down
52 changes: 32 additions & 20 deletions library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1313,26 +1313,6 @@ impl<T> Arc<[T]> {
))
}
}

/// Converts the reference-counted slice into a reference-counted array.
///
/// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
///
/// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
#[unstable(feature = "alloc_slice_into_array", issue = "148082")]
#[inline]
#[must_use]
pub fn into_array<const N: usize>(self) -> Option<Arc<[T; N]>> {
if self.len() == N {
let ptr = Self::into_raw(self) as *const [T; N];

// SAFETY: The underlying array of a slice has the exact same layout as an actual array `[T; N]` if `N` is equal to the slice's length.
let me = unsafe { Arc::from_raw(ptr) };
Some(me)
} else {
None
}
}
}

impl<T, A: Allocator> Arc<[T], A> {
Expand Down Expand Up @@ -1407,6 +1387,38 @@ impl<T, A: Allocator> Arc<[T], A> {
)
}
}

/// Converts the reference-counted slice into a reference-counted array.
///
/// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
///
/// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
///
/// # Examples
///
/// ```
/// #![feature(alloc_slice_into_array)]
/// use std::sync::Arc;
///
/// let arc_slice: Arc<[i32]> = Arc::new([1, 2, 3]);
///
/// let arc_array: Arc<[i32; 3]> = arc_slice.into_array().unwrap();
/// ```
#[unstable(feature = "alloc_slice_into_array", issue = "148082")]
#[inline]
#[must_use]
pub fn into_array<const N: usize>(self) -> Option<Arc<[T; N], A>> {
if self.len() == N {
let (ptr, alloc) = Self::into_raw_with_allocator(self);
let ptr = ptr as *const [T; N];

// SAFETY: The underlying array of a slice has the exact same layout as an actual array `[T; N]` if `N` is equal to the slice's length.
let me = unsafe { Arc::from_raw_in(ptr, alloc) };
Some(me)
} else {
None
}
}
}

impl<T, A: Allocator> Arc<mem::MaybeUninit<T>, A> {
Expand Down
2 changes: 1 addition & 1 deletion library/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ miniz_oxide = { version = "0.8.0", optional = true, default-features = false }
addr2line = { version = "0.25.0", optional = true, default-features = false }

[target.'cfg(not(all(windows, target_env = "msvc")))'.dependencies]
libc = { version = "0.2.183", default-features = false, features = [
libc = { version = "0.2.184", default-features = false, features = [
'rustc-dep-of-std',
], public = true }

Expand Down
8 changes: 6 additions & 2 deletions library/std/tests/sync/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,19 +917,23 @@ fn test_rwlock_max_readers() {
target_os = "fuchsia",
all(target_family = "wasm", target_feature = "atomics"),
target_os = "hermit",
target_os = "motor",
target_os = "motor",
) => {
(1 << 30) - 2
},
any(
target_family = "unix",
all(target_os = "windows", target_vendor = "win7"),
all(target_os = "windows", target_vendor = "win7", target_pointer_width = "64"),
all(target_vendor = "fortanix", target_env = "sgx"),
target_os = "xous",
target_os = "teeos",
) => {
u32::MAX
},
// Otherwise a form of deadlock is observed.
all(target_os = "windows", target_vendor = "win7", target_pointer_width = "32") => {
(1 << 28) - 1
},
target_os = "solid_asp3" => {
(1 << 30)
},
Expand Down
Loading