// SAFETY: const sound because we transmute out the length field as a usize (which it must be)
#[rustc_allow_const_fn_unstable(const_fn_union)]
pub const fn len(&self) -> usize {
- // SAFETY: this is safe because `&[T]` and `FatPtr<T>` have the same layout.
- // Only `std` can make this guarantee.
- unsafe { crate::ptr::Repr { rust: self }.raw.len }
+ #[cfg(bootstrap)]
+ {
+ // SAFETY: this is safe because `&[T]` and `FatPtr<T>` have the same layout.
+ // Only `std` can make this guarantee.
+ unsafe { crate::ptr::Repr { rust: self }.raw.len }
+ }
+ #[cfg(not(bootstrap))]
+ {
+ // FIXME: Replace with `crate::ptr::metadata(self)` when that is const-stable.
+ // As of this writing this causes a "Const-stable functions can only call other
+ // const-stable functions" error.
+
+ // SAFETY: Accessing the value from the `PtrRepr` union is safe since *const T
+ // and PtrComponents<T> have the same memory layouts. Only std can make this
+ // guarantee.
+ unsafe { crate::ptr::PtrRepr { const_ptr: self }.components.metadata }
+ }
}
/// Returns `true` if the slice has a length of 0.
/// [`Result::Err`] is returned, containing the index where a matching
/// element could be inserted while maintaining sorted order.
///
+ /// See also [`binary_search_by`], [`binary_search_by_key`], and [`partition_point`].
+ ///
+ /// [`binary_search_by`]: #method.binary_search_by
+ /// [`binary_search_by_key`]: #method.binary_search_by_key
+ /// [`partition_point`]: #method.partition_point
+ ///
/// # Examples
///
/// Looks up a series of four elements. The first is found, with a
/// [`Result::Err`] is returned, containing the index where a matching
/// element could be inserted while maintaining sorted order.
///
+ /// See also [`binary_search`], [`binary_search_by_key`], and [`partition_point`].
+ ///
+ /// [`binary_search`]: #method.binary_search
+ /// [`binary_search_by_key`]: #method.binary_search_by_key
+ /// [`partition_point`]: #method.partition_point
+ ///
/// # Examples
///
/// Looks up a series of four elements. The first is found, with a
/// [`Result::Err`] is returned, containing the index where a matching
/// element could be inserted while maintaining sorted order.
///
+ /// See also [`binary_search`], [`binary_search_by`], and [`partition_point`].
+ ///
/// [`sort_by_key`]: #method.sort_by_key
+ /// [`binary_search`]: #method.binary_search
+ /// [`binary_search_by`]: #method.binary_search_by
+ /// [`partition_point`]: #method.partition_point
///
/// # Examples
///
/// If this slice is not partitioned, the returned result is unspecified and meaningless,
/// as this method performs a kind of binary search.
///
+ /// See also [`binary_search`], [`binary_search_by`], and [`binary_search_by_key`].
+ ///
+ /// [`binary_search`]: #method.binary_search
+ /// [`binary_search_by`]: #method.binary_search_by
+ /// [`binary_search_by_key`]: #method.binary_search_by_key
+ ///
/// # Examples
///
/// ```
- /// #![feature(partition_point)]
- ///
/// let v = [1, 2, 3, 3, 5, 6, 7];
/// let i = v.partition_point(|&x| x < 5);
///
/// assert!(v[..i].iter().all(|&x| x < 5));
/// assert!(v[i..].iter().all(|&x| !(x < 5)));
/// ```
- #[unstable(feature = "partition_point", reason = "new API", issue = "73831")]
+ #[stable(feature = "partition_point", since = "1.52.0")]
pub fn partition_point<P>(&self, mut pred: P) -> usize
where
P: FnMut(&T) -> bool,