Skip to main content

retain_raw

Function retain_raw 

Source
pub(crate) unsafe fn retain_raw<const SINGLE_REGION: bool, T, F>(
    front: *mut T,
    front_len: usize,
    back: *mut T,
    total_len: &mut usize,
    f: F,
)
where F: FnMut(&mut T) -> bool,
Expand description

Retains elements in two contiguous regions according to f.

The *total_len initialized elements are treated as one logical sequence: the first front_len elements start at front, and the remaining elements start at back.

Each element is passed to f in logical order. Elements for which f returns true are compacted toward the beginning of the logical sequence, preserving their relative order. Rejected elements are dropped.

On return, *total_len is updated to the number of retained elements.

On panic, retained and unchecked elements, including the element whose predicate panicked, are compacted and *total_len is updated accordingly.

Uses the specialized SINGLE_REGION in PanicGuard to eliminate unnecessary stack usage and move unchecked elements with a single bulk copy for Vec.

§Safety

  • front_len must equal *total_len if SINGLE_REGION is true.
  • front must point to front_len initialized, valid instances of T.
  • back must point to *total_len - front_len initialized, valid instances of T.
  • front_len <= *total_len.
  • The two regions must not overlap.
  • All elements in both regions must be exclusively accessible for the duration of this call.
  • total_len must not point into either region.