-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Add slice take methods #88502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add slice take methods #88502
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2232,3 +2232,112 @@ fn slice_split_array_mut_out_of_bounds() { | |
|
||
v.split_array_mut::<7>(); | ||
} | ||
|
||
macro_rules! take_tests { | ||
(slice: &[], $($tts:tt)*) => { | ||
take_tests!(ty: &[()], slice: &[], $($tts)*); | ||
}; | ||
(slice: &mut [], $($tts:tt)*) => { | ||
take_tests!(ty: &mut [()], slice: &mut [], $($tts)*); | ||
}; | ||
(slice: &$slice:expr, $($tts:tt)*) => { | ||
take_tests!(ty: &[_], slice: &$slice, $($tts)*); | ||
}; | ||
(slice: &mut $slice:expr, $($tts:tt)*) => { | ||
take_tests!(ty: &mut [_], slice: &mut $slice, $($tts)*); | ||
}; | ||
(ty: $ty:ty, slice: $slice:expr, method: $method:ident, $(($test_name:ident, ($($args:expr),*), $output:expr, $remaining:expr),)*) => { | ||
$( | ||
#[test] | ||
fn $test_name() { | ||
let mut slice: $ty = $slice; | ||
assert_eq!($output, slice.$method($($args)*)); | ||
let remaining: $ty = $remaining; | ||
assert_eq!(remaining, slice); | ||
} | ||
)* | ||
}; | ||
} | ||
|
||
take_tests! { | ||
slice: &[0, 1, 2, 3], method: take, | ||
(take_in_bounds_range_to, (..1), Some(&[0] as _), &[1, 2, 3]), | ||
(take_in_bounds_range_to_inclusive, (..=0), Some(&[0] as _), &[1, 2, 3]), | ||
(take_in_bounds_range_from, (2..), Some(&[2, 3] as _), &[0, 1]), | ||
(take_oob_range_to, (..5), None, &[0, 1, 2, 3]), | ||
(take_oob_range_to_inclusive, (..=4), None, &[0, 1, 2, 3]), | ||
(take_oob_range_from, (5..), None, &[0, 1, 2, 3]), | ||
} | ||
|
||
take_tests! { | ||
slice: &mut [0, 1, 2, 3], method: take_mut, | ||
(take_mut_in_bounds_range_to, (..1), Some(&mut [0] as _), &mut [1, 2, 3]), | ||
(take_mut_in_bounds_range_to_inclusive, (..=0), Some(&mut [0] as _), &mut [1, 2, 3]), | ||
(take_mut_in_bounds_range_from, (2..), Some(&mut [2, 3] as _), &mut [0, 1]), | ||
(take_mut_oob_range_to, (..5), None, &mut [0, 1, 2, 3]), | ||
(take_mut_oob_range_to_inclusive, (..=4), None, &mut [0, 1, 2, 3]), | ||
(take_mut_oob_range_from, (5..), None, &mut [0, 1, 2, 3]), | ||
} | ||
|
||
take_tests! { | ||
slice: &[1, 2], method: take_first, | ||
(take_first_nonempty, (), Some(&1), &[2]), | ||
} | ||
|
||
take_tests! { | ||
slice: &mut [1, 2], method: take_first_mut, | ||
(take_first_mut_nonempty, (), Some(&mut 1), &mut [2]), | ||
} | ||
|
||
take_tests! { | ||
slice: &[1, 2], method: take_last, | ||
(take_last_nonempty, (), Some(&2), &[1]), | ||
} | ||
|
||
take_tests! { | ||
slice: &mut [1, 2], method: take_last_mut, | ||
(take_last_mut_nonempty, (), Some(&mut 2), &mut [1]), | ||
} | ||
|
||
take_tests! { | ||
slice: &[], method: take_first, | ||
(take_first_empty, (), None, &[]), | ||
} | ||
|
||
take_tests! { | ||
slice: &mut [], method: take_first_mut, | ||
(take_first_mut_empty, (), None, &mut []), | ||
} | ||
|
||
take_tests! { | ||
slice: &[], method: take_last, | ||
(take_last_empty, (), None, &[]), | ||
} | ||
|
||
take_tests! { | ||
slice: &mut [], method: take_last_mut, | ||
(take_last_mut_empty, (), None, &mut []), | ||
} | ||
|
||
const EMPTY_MAX: &'static [()] = &[(); usize::MAX]; | ||
|
||
// can't be a constant due to const mutability rules | ||
macro_rules! empty_max_mut { | ||
() => { | ||
&mut [(); usize::MAX] as _ | ||
}; | ||
} | ||
|
||
take_tests! { | ||
slice: &[(); usize::MAX], method: take, | ||
(take_in_bounds_max_range_to, (..usize::MAX), Some(EMPTY_MAX), &[(); 0]), | ||
(take_oob_max_range_to_inclusive, (..=usize::MAX), None, EMPTY_MAX), | ||
(take_in_bounds_max_range_from, (usize::MAX..), Some(&[] as _), EMPTY_MAX), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test fails in Miri, and I am trying to find out why... help would be appreciated. Here's the Zulip thread. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, turns out that even with |
||
} | ||
|
||
take_tests! { | ||
slice: &mut [(); usize::MAX], method: take_mut, | ||
(take_mut_in_bounds_max_range_to, (..usize::MAX), Some(empty_max_mut!()), &mut [(); 0]), | ||
(take_mut_oob_max_range_to_inclusive, (..=usize::MAX), None, empty_max_mut!()), | ||
(take_mut_in_bounds_max_range_from, (usize::MAX..), Some(&mut [] as _), empty_max_mut!()), | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really like the presence of panic call here which optimiser will have to remove. Would it make more sense to make this a method of
OneSidedRange
in order to statically eliminate impossible cases?