Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix tidy errors
  • Loading branch information
Voultapher committed May 16, 2024
commit 1a6b0e410e3b32f8641b9ee2bcf992712c8fa72f
5 changes: 4 additions & 1 deletion library/core/src/slice/sort/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ fn partition_at_index_loop<'a, T, F>(
// slice. Partition the slice into elements equal to and elements greater than the pivot.
// This case is usually hit when the slice contains many duplicate elements.
if let Some(p) = ancestor_pivot {
if !is_less(p, unsafe { v.get_unchecked(pivot_pos) }) {
// SAFETY: choose_pivot promises to return a valid pivot position.
let pivot = unsafe { v.get_unchecked(pivot_pos) };

if !is_less(p, pivot) {
let num_lt = partition(v, pivot_pos, &mut |a, b| !is_less(b, a));

// Continue sorting elements greater than the pivot. We know that `mid` contains
Expand Down
15 changes: 5 additions & 10 deletions library/core/src/slice/sort/shared/smallsort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ fn small_sort_fallback<T, F: FnMut(&T, &T) -> bool>(v: &mut [T], is_less: &mut F
fn small_sort_general<T: FreezeMarker, F: FnMut(&T, &T) -> bool>(v: &mut [T], is_less: &mut F) {
let mut stack_array = MaybeUninit::<[T; SMALL_SORT_GENERAL_SCRATCH_LEN]>::uninit();

// SAFETY: The memory is backed by `stack_array`, and the operation is safe as long as the len
// is the same.
let scratch = unsafe {
slice::from_raw_parts_mut(
stack_array.as_mut_ptr() as *mut MaybeUninit<T>,
Expand Down Expand Up @@ -327,8 +329,9 @@ where
}

// SAFETY: The right side of `v` based on `len_div_2` is guaranteed in-bounds.
region =
unsafe { &mut *ptr::slice_from_raw_parts_mut(v_base.add(len_div_2), len - len_div_2) };
unsafe {
region = &mut *ptr::slice_from_raw_parts_mut(v_base.add(len_div_2), len - len_div_2)
};
}

// SAFETY: We checked that T is Freeze and thus observation safe.
Expand Down Expand Up @@ -812,14 +815,6 @@ pub(crate) const fn has_efficient_in_place_swap<T>() -> bool {
mem::size_of::<T>() <= 8 // mem::size_of::<u64>()
}

#[test]
fn type_info() {
assert!(has_efficient_in_place_swap::<i32>());
assert!(has_efficient_in_place_swap::<u64>());
assert!(!has_efficient_in_place_swap::<u128>());
assert!(!has_efficient_in_place_swap::<String>());
}

/// SAFETY: Only used for run-time optimization heuristic.
#[rustc_unsafe_specialization_marker]
trait CopyMarker {}
Expand Down
9 changes: 0 additions & 9 deletions library/core/src/slice/sort/stable/quicksort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,3 @@ const fn has_direct_interior_mutability<T>() -> bool {
// Otherwise a type like Mutex<Option<Box<str>>> could lead to double free.
!T::is_freeze()
}

#[test]
fn freeze_check() {
assert!(!has_direct_interior_mutability::<u32>());
assert!(!has_direct_interior_mutability::<[u128; 2]>());

assert!(has_direct_interior_mutability::<crate::cell::Cell<u32>>());
assert!(has_direct_interior_mutability::<crate::sync::Mutex<u32>>());
}
4 changes: 4 additions & 0 deletions library/core/src/slice/sort/unstable/quicksort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ struct GapGuard<T> {

impl<T> Drop for GapGuard<T> {
fn drop(&mut self) {
// SAFETY: `self` MUST be constructed in a way that makes copying the gap value into
// `self.pos` sound.
unsafe {
ptr::copy_nonoverlapping(&*self.value, self.pos, 1);
}
Expand All @@ -340,6 +342,8 @@ struct GapGuardRaw<T> {

impl<T> Drop for GapGuardRaw<T> {
fn drop(&mut self) {
// SAFETY: `self` MUST be constructed in a way that makes copying the gap value into
// `self.pos` sound.
unsafe {
ptr::copy_nonoverlapping(self.value, self.pos, 1);
}
Expand Down
1 change: 0 additions & 1 deletion library/core/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1803,7 +1803,6 @@ fn brute_force_rotate_test_1() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn sort_unstable() {
// use core::cmp::Ordering::{Equal, Greater, Less};
use rand::Rng;

// Miri is too slow (but still need to `chain` to make the types match)
Expand Down
5 changes: 0 additions & 5 deletions src/doc/unstable-book/src/library-features/sort-internals.md

This file was deleted.