Skip to content

Commit dea6a76

Browse files
committed
Auto merge of #95798 - Dylan-DPC:rollup-51hx1wl, r=Dylan-DPC
Rollup of 7 pull requests Successful merges: - #95102 (Add known-bug for #95034) - #95579 (Add `<[[T; N]]>::flatten{_mut}`) - #95634 (Mailmap update) - #95705 (Promote x86_64-unknown-none target to Tier 2 and distribute build artifacts) - #95761 (Kickstart the inner usage of `macro_metavar_expr`) - #95782 (Windows: Increase a pipe's buffer capacity to 64kb) - #95791 (hide an #[allow] directive from the Arc::new_cyclic doc example) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 6e336a0 + 38be9f8 commit dea6a76

File tree

11 files changed

+184
-119
lines changed

11 files changed

+184
-119
lines changed

alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
#![feature(trusted_len)]
132132
#![feature(trusted_random_access)]
133133
#![feature(try_trait_v2)]
134+
#![feature(unchecked_math)]
134135
#![feature(unicode_internals)]
135136
#![feature(unsize)]
136137
//

alloc/src/sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ impl<T> Arc<T> {
369369
///
370370
/// # Example
371371
/// ```
372-
/// #![allow(dead_code)]
372+
/// # #![allow(dead_code)]
373373
/// use std::sync::{Arc, Weak};
374374
///
375375
/// struct Gadget {

alloc/src/vec/mod.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,6 +2274,51 @@ impl<T: Clone, A: Allocator> Vec<T, A> {
22742274
}
22752275
}
22762276

2277+
impl<T, A: Allocator, const N: usize> Vec<[T; N], A> {
2278+
/// Takes a `Vec<[T; N]>` and flattens it into a `Vec<T>`.
2279+
///
2280+
/// # Panics
2281+
///
2282+
/// Panics if the length of the resulting vector would overflow a `usize`.
2283+
///
2284+
/// This is only possible when flattening a vector of arrays of zero-sized
2285+
/// types, and thus tends to be irrelevant in practice. If
2286+
/// `size_of::<T>() > 0`, this will never panic.
2287+
///
2288+
/// # Examples
2289+
///
2290+
/// ```
2291+
/// #![feature(slice_flatten)]
2292+
///
2293+
/// let mut vec = vec![[1, 2, 3], [4, 5, 6], [7, 8, 9]];
2294+
/// assert_eq!(vec.pop(), Some([7, 8, 9]));
2295+
///
2296+
/// let mut flattened = vec.into_flattened();
2297+
/// assert_eq!(flattened.pop(), Some(6));
2298+
/// ```
2299+
#[unstable(feature = "slice_flatten", issue = "95629")]
2300+
pub fn into_flattened(self) -> Vec<T, A> {
2301+
let (ptr, len, cap, alloc) = self.into_raw_parts_with_alloc();
2302+
let (new_len, new_cap) = if mem::size_of::<T>() == 0 {
2303+
(len.checked_mul(N).expect("vec len overflow"), usize::MAX)
2304+
} else {
2305+
// SAFETY:
2306+
// - `cap * N` cannot overflow because the allocation is already in
2307+
// the address space.
2308+
// - Each `[T; N]` has `N` valid elements, so there are `len * N`
2309+
// valid elements in the allocation.
2310+
unsafe { (len.unchecked_mul(N), cap.unchecked_mul(N)) }
2311+
};
2312+
// SAFETY:
2313+
// - `ptr` was allocated by `self`
2314+
// - `ptr` is well-aligned because `[T; N]` has the same alignment as `T`.
2315+
// - `new_cap` refers to the same sized allocation as `cap` because
2316+
// `new_cap * size_of::<T>()` == `cap * size_of::<[T; N]>()`
2317+
// - `len` <= `cap`, so `len * N` <= `cap * N`.
2318+
unsafe { Vec::<T, A>::from_raw_parts_in(ptr.cast(), new_len, new_cap, alloc) }
2319+
}
2320+
}
2321+
22772322
// This code generalizes `extend_with_{element,default}`.
22782323
trait ExtendWith<T> {
22792324
fn next(&mut self) -> T;

alloc/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#![feature(const_str_from_utf8)]
3939
#![feature(nonnull_slice_from_raw_parts)]
4040
#![feature(panic_update_hook)]
41+
#![feature(slice_flatten)]
4142

4243
use std::collections::hash_map::DefaultHasher;
4344
use std::hash::{Hash, Hasher};

alloc/tests/vec.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,3 +2408,10 @@ fn test_extend_from_within_panicing_clone() {
24082408

24092409
assert_eq!(count.load(Ordering::SeqCst), 4);
24102410
}
2411+
2412+
#[test]
2413+
#[should_panic = "vec len overflow"]
2414+
fn test_into_flattened_size_overflow() {
2415+
let v = vec![[(); usize::MAX]; 2];
2416+
let _ = v.into_flattened();
2417+
}

core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
#![feature(intrinsics)]
182182
#![feature(lang_items)]
183183
#![feature(link_llvm_intrinsics)]
184+
#![feature(macro_metavar_expr)]
184185
#![feature(min_specialization)]
185186
#![feature(mixed_integer_ops)]
186187
#![feature(must_not_suspend)]

core/src/slice/mod.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3992,6 +3992,88 @@ impl<T> [T] {
39923992
}
39933993
}
39943994

3995+
#[cfg(not(bootstrap))]
3996+
impl<T, const N: usize> [[T; N]] {
3997+
/// Takes a `&[[T; N]]`, and flattens it to a `&[T]`.
3998+
///
3999+
/// # Panics
4000+
///
4001+
/// This panics if the length of the resulting slice would overflow a `usize`.
4002+
///
4003+
/// This is only possible when flattening a slice of arrays of zero-sized
4004+
/// types, and thus tends to be irrelevant in practice. If
4005+
/// `size_of::<T>() > 0`, this will never panic.
4006+
///
4007+
/// # Examples
4008+
///
4009+
/// ```
4010+
/// #![feature(slice_flatten)]
4011+
///
4012+
/// assert_eq!([[1, 2, 3], [4, 5, 6]].flatten(), &[1, 2, 3, 4, 5, 6]);
4013+
///
4014+
/// assert_eq!(
4015+
/// [[1, 2, 3], [4, 5, 6]].flatten(),
4016+
/// [[1, 2], [3, 4], [5, 6]].flatten(),
4017+
/// );
4018+
///
4019+
/// let slice_of_empty_arrays: &[[i32; 0]] = &[[], [], [], [], []];
4020+
/// assert!(slice_of_empty_arrays.flatten().is_empty());
4021+
///
4022+
/// let empty_slice_of_arrays: &[[u32; 10]] = &[];
4023+
/// assert!(empty_slice_of_arrays.flatten().is_empty());
4024+
/// ```
4025+
#[unstable(feature = "slice_flatten", issue = "95629")]
4026+
pub fn flatten(&self) -> &[T] {
4027+
let len = if crate::mem::size_of::<T>() == 0 {
4028+
self.len().checked_mul(N).expect("slice len overflow")
4029+
} else {
4030+
// SAFETY: `self.len() * N` cannot overflow because `self` is
4031+
// already in the address space.
4032+
unsafe { self.len().unchecked_mul(N) }
4033+
};
4034+
// SAFETY: `[T]` is layout-identical to `[T; N]`
4035+
unsafe { from_raw_parts(self.as_ptr().cast(), len) }
4036+
}
4037+
4038+
/// Takes a `&mut [[T; N]]`, and flattens it to a `&mut [T]`.
4039+
///
4040+
/// # Panics
4041+
///
4042+
/// This panics if the length of the resulting slice would overflow a `usize`.
4043+
///
4044+
/// This is only possible when flattening a slice of arrays of zero-sized
4045+
/// types, and thus tends to be irrelevant in practice. If
4046+
/// `size_of::<T>() > 0`, this will never panic.
4047+
///
4048+
/// # Examples
4049+
///
4050+
/// ```
4051+
/// #![feature(slice_flatten)]
4052+
///
4053+
/// fn add_5_to_all(slice: &mut [i32]) {
4054+
/// for i in slice {
4055+
/// *i += 5;
4056+
/// }
4057+
/// }
4058+
///
4059+
/// let mut array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
4060+
/// add_5_to_all(array.flatten_mut());
4061+
/// assert_eq!(array, [[6, 7, 8], [9, 10, 11], [12, 13, 14]]);
4062+
/// ```
4063+
#[unstable(feature = "slice_flatten", issue = "95629")]
4064+
pub fn flatten_mut(&mut self) -> &mut [T] {
4065+
let len = if crate::mem::size_of::<T>() == 0 {
4066+
self.len().checked_mul(N).expect("slice len overflow")
4067+
} else {
4068+
// SAFETY: `self.len() * N` cannot overflow because `self` is
4069+
// already in the address space.
4070+
unsafe { self.len().unchecked_mul(N) }
4071+
};
4072+
// SAFETY: `[T]` is layout-identical to `[T; N]`
4073+
unsafe { from_raw_parts_mut(self.as_mut_ptr().cast(), len) }
4074+
}
4075+
}
4076+
39954077
trait CloneFromSpec<T> {
39964078
fn spec_clone_from(&mut self, src: &[T]);
39974079
}

core/src/tuple.rs

Lines changed: 24 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,17 @@ use crate::cmp::*;
55

66
// macro for implementing n-ary tuple functions and operations
77
macro_rules! tuple_impls {
8-
($(
9-
$Tuple:ident {
10-
$(($idx:tt) -> $T:ident)+
11-
}
12-
)+) => {
8+
( $( $Tuple:ident( $( $T:ident )+ ) )+ ) => {
139
$(
1410
#[stable(feature = "rust1", since = "1.0.0")]
1511
impl<$($T:PartialEq),+> PartialEq for ($($T,)+) where last_type!($($T,)+): ?Sized {
1612
#[inline]
1713
fn eq(&self, other: &($($T,)+)) -> bool {
18-
$(self.$idx == other.$idx)&&+
14+
$( ${ignore(T)} self.${index()} == other.${index()} )&&+
1915
}
2016
#[inline]
2117
fn ne(&self, other: &($($T,)+)) -> bool {
22-
$(self.$idx != other.$idx)||+
18+
$( ${ignore(T)} self.${index()} != other.${index()} )||+
2319
}
2420
}
2521

@@ -28,34 +24,36 @@ macro_rules! tuple_impls {
2824

2925
#[stable(feature = "rust1", since = "1.0.0")]
3026
impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+)
31-
where last_type!($($T,)+): ?Sized {
27+
where
28+
last_type!($($T,)+): ?Sized
29+
{
3230
#[inline]
3331
fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
34-
lexical_partial_cmp!($(self.$idx, other.$idx),+)
32+
lexical_partial_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
3533
}
3634
#[inline]
3735
fn lt(&self, other: &($($T,)+)) -> bool {
38-
lexical_ord!(lt, $(self.$idx, other.$idx),+)
36+
lexical_ord!(lt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
3937
}
4038
#[inline]
4139
fn le(&self, other: &($($T,)+)) -> bool {
42-
lexical_ord!(le, $(self.$idx, other.$idx),+)
40+
lexical_ord!(le, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
4341
}
4442
#[inline]
4543
fn ge(&self, other: &($($T,)+)) -> bool {
46-
lexical_ord!(ge, $(self.$idx, other.$idx),+)
44+
lexical_ord!(ge, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
4745
}
4846
#[inline]
4947
fn gt(&self, other: &($($T,)+)) -> bool {
50-
lexical_ord!(gt, $(self.$idx, other.$idx),+)
48+
lexical_ord!(gt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
5149
}
5250
}
5351

5452
#[stable(feature = "rust1", since = "1.0.0")]
5553
impl<$($T:Ord),+> Ord for ($($T,)+) where last_type!($($T,)+): ?Sized {
5654
#[inline]
5755
fn cmp(&self, other: &($($T,)+)) -> Ordering {
58-
lexical_cmp!($(self.$idx, other.$idx),+)
56+
lexical_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
5957
}
6058
}
6159

@@ -108,106 +106,16 @@ macro_rules! last_type {
108106
}
109107

110108
tuple_impls! {
111-
Tuple1 {
112-
(0) -> A
113-
}
114-
Tuple2 {
115-
(0) -> A
116-
(1) -> B
117-
}
118-
Tuple3 {
119-
(0) -> A
120-
(1) -> B
121-
(2) -> C
122-
}
123-
Tuple4 {
124-
(0) -> A
125-
(1) -> B
126-
(2) -> C
127-
(3) -> D
128-
}
129-
Tuple5 {
130-
(0) -> A
131-
(1) -> B
132-
(2) -> C
133-
(3) -> D
134-
(4) -> E
135-
}
136-
Tuple6 {
137-
(0) -> A
138-
(1) -> B
139-
(2) -> C
140-
(3) -> D
141-
(4) -> E
142-
(5) -> F
143-
}
144-
Tuple7 {
145-
(0) -> A
146-
(1) -> B
147-
(2) -> C
148-
(3) -> D
149-
(4) -> E
150-
(5) -> F
151-
(6) -> G
152-
}
153-
Tuple8 {
154-
(0) -> A
155-
(1) -> B
156-
(2) -> C
157-
(3) -> D
158-
(4) -> E
159-
(5) -> F
160-
(6) -> G
161-
(7) -> H
162-
}
163-
Tuple9 {
164-
(0) -> A
165-
(1) -> B
166-
(2) -> C
167-
(3) -> D
168-
(4) -> E
169-
(5) -> F
170-
(6) -> G
171-
(7) -> H
172-
(8) -> I
173-
}
174-
Tuple10 {
175-
(0) -> A
176-
(1) -> B
177-
(2) -> C
178-
(3) -> D
179-
(4) -> E
180-
(5) -> F
181-
(6) -> G
182-
(7) -> H
183-
(8) -> I
184-
(9) -> J
185-
}
186-
Tuple11 {
187-
(0) -> A
188-
(1) -> B
189-
(2) -> C
190-
(3) -> D
191-
(4) -> E
192-
(5) -> F
193-
(6) -> G
194-
(7) -> H
195-
(8) -> I
196-
(9) -> J
197-
(10) -> K
198-
}
199-
Tuple12 {
200-
(0) -> A
201-
(1) -> B
202-
(2) -> C
203-
(3) -> D
204-
(4) -> E
205-
(5) -> F
206-
(6) -> G
207-
(7) -> H
208-
(8) -> I
209-
(9) -> J
210-
(10) -> K
211-
(11) -> L
212-
}
109+
Tuple1(A)
110+
Tuple2(A B)
111+
Tuple3(A B C)
112+
Tuple4(A B C D)
113+
Tuple5(A B C D E)
114+
Tuple6(A B C D E F)
115+
Tuple7(A B C D E F G)
116+
Tuple8(A B C D E F G H)
117+
Tuple9(A B C D E F G H I)
118+
Tuple10(A B C D E F G H I J)
119+
Tuple11(A B C D E F G H I J K)
120+
Tuple12(A B C D E F G H I J K L)
213121
}

core/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
#![feature(const_array_from_ref)]
9494
#![feature(const_slice_from_ref)]
9595
#![feature(waker_getters)]
96+
#![feature(slice_flatten)]
9697
#![deny(unsafe_op_in_unsafe_fn)]
9798

9899
extern crate test;

0 commit comments

Comments
 (0)