pub struct PopulatedSlice<T>(/* private fields */);Implementations§
Source§impl<T> PopulatedSlice<T>
impl<T> PopulatedSlice<T>
Sourcepub fn iter(&self) -> PopulatedIter<'_, T>
pub fn iter(&self) -> PopulatedIter<'_, T>
Returns a populated iterator over the slice.
Sourcepub fn iter_mut(&mut self) -> PopulatedIterMut<'_, T>
pub fn iter_mut(&mut self) -> PopulatedIterMut<'_, T>
Returns a mutable populated iterator over the slice.
Sourcepub fn len(&self) -> NonZeroUsize
pub fn len(&self) -> NonZeroUsize
Returns the length of the populated slice.
use populated::PopulatedSlice;
use std::num::NonZeroUsize;
let slice: &[u64] = &[1, 2, 3];
let slice: &PopulatedSlice<u64> = TryFrom::try_from(slice).unwrap();
assert_eq!(slice.len(), NonZeroUsize::new(3).unwrap());Sourcepub fn first_mut(&mut self) -> &mut T
pub fn first_mut(&mut self) -> &mut T
Returns a mutable pointer to the first element of the populated slice.
Sourcepub fn split_first(&self) -> (&T, &[T])
pub fn split_first(&self) -> (&T, &[T])
Returns the first and all the rest of the elements of the populated slice. Note that the returned slice can be empty. It is not a PopulatedSlice.
Sourcepub fn split_first_mut(&mut self) -> (&mut T, &mut [T])
pub fn split_first_mut(&mut self) -> (&mut T, &mut [T])
Returns the first and all the rest of the elements of the populated slice. The returned first element and the rest of the elements are mutable. Note that the returned slice can be empty. It is not a PopulatedSlice.
Sourcepub fn split_last(&self) -> (&T, &[T])
pub fn split_last(&self) -> (&T, &[T])
Returns the last and all the rest of the elements of the populated slice. Note that the returned slice can be empty. It is not a PopulatedSlice.
Sourcepub fn split_last_mut(&mut self) -> (&mut T, &mut [T])
pub fn split_last_mut(&mut self) -> (&mut T, &mut [T])
Returns the mutable last and all the rest of the elements of the populated slice as a mutable slice. Note that the returned slice can be empty. It is not a PopulatedSlice.
Sourcepub fn last_mut(&mut self) -> &mut T
pub fn last_mut(&mut self) -> &mut T
Returns a mutable reference to the last item in the populated slice.
Sourcepub fn swap(&mut self, i: usize, j: usize)
pub fn swap(&mut self, i: usize, j: usize)
Swaps two elements in the slice.
If a equals to b, it’s guaranteed that elements won’t change value.
Sourcepub fn split_at_populated(
&self,
mid: NonZeroUsize,
) -> (&PopulatedSlice<T>, &[T])
pub fn split_at_populated( &self, mid: NonZeroUsize, ) -> (&PopulatedSlice<T>, &[T])
Returns a reference to an element or subslice depending on the type of index. Since mid is a NonZeroUsize, it is guaranteed that the first slice is populated.
Sourcepub fn split_at(&self, mid: usize) -> (&[T], &[T])
pub fn split_at(&self, mid: usize) -> (&[T], &[T])
Divides one slice into two at an index.
The first will contain all indices from [0, mid) (excluding the
index mid itself) and the second will contain all indices from
[mid, len) (excluding the index len itself).
Sourcepub fn split_at_mut_populated(
&mut self,
mid: NonZeroUsize,
) -> (&mut PopulatedSlice<T>, &mut [T])
pub fn split_at_mut_populated( &mut self, mid: NonZeroUsize, ) -> (&mut PopulatedSlice<T>, &mut [T])
Returns a mutable reference to an element or subslice depending on the type of index. Since mid is a NonZeroUsize, it is guaranteed that the first slice is populated.
Sourcepub fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T])
pub fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T])
Divides one mutable slice into two at an index.
The first will contain all indices from [0, mid) (excluding the
index mid itself) and the second will contain all indices from
[mid, len) (excluding the index len itself).
Sourcepub fn binary_search_by(
&self,
f: impl FnMut(&T) -> Ordering,
) -> Result<usize, usize>
pub fn binary_search_by( &self, f: impl FnMut(&T) -> Ordering, ) -> Result<usize, usize>
Binary searches this populated slice with a comparator function.
The comparator function should return an order code that indicates
whether its argument is Less, Equal or Greater the desired target. If
the slice is not sorted or if the comparator function does not
implement an order consistent with the sort order of the underlying
slice, the returned result is unspecified and meaningless.
If the value is found then Result::Ok is returned, containing the index of the matching element. If there are multiple matches, then any one of the matches could be returned. The index is chosen deterministically, but is subject to change in future versions of Rust. If the value is not found then 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.
Sourcepub fn binary_search_by_key<K: Ord>(
&self,
key: &K,
f: impl FnMut(&T) -> K,
) -> Result<usize, usize>
pub fn binary_search_by_key<K: Ord>( &self, key: &K, f: impl FnMut(&T) -> K, ) -> Result<usize, usize>
Binary searches this populated slice with a key extraction function.
Assumes that the slice is sorted by the key, for instance with sort_by_key using the same key extraction function. If the slice is not sorted by the key, the returned result is unspecified and meaningless.
If the value is found then Result::Ok is returned, containing the index of the matching element. If there are multiple matches, then any one of the matches could be returned. The index is chosen deterministically, but is subject to change in future versions of Rust. If the value is not found then 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.
Sourcepub fn sort_unstable_by(&mut self, f: impl FnMut(&T, &T) -> Ordering)
pub fn sort_unstable_by(&mut self, f: impl FnMut(&T, &T) -> Ordering)
Sorts the populated slice with a comparator function, but might not preserve the order of equal elements.
This sort is unstable (i.e., may reorder equal elements), in-place (i.e., does not allocate), and O(n * log(n)) worst-case.
The comparator function must define a total ordering for the elements in the slice. If the ordering is not total, the order of the elements is unspecified. An order is a total order if it is (for all a, b and c):
- total and antisymmetric: exactly one of a < b, a == b or a > b is true, and
- transitive, a < b and b < c implies a < c. The same must hold for both == and >.
For example, while f64 doesn’t implement Ord because NaN != NaN, we can use partial_cmp as our sort function when we know the slice doesn’t contain a NaN.
Sourcepub fn sort_unstable_by_key<K: Ord>(&mut self, f: impl FnMut(&T) -> K)
pub fn sort_unstable_by_key<K: Ord>(&mut self, f: impl FnMut(&T) -> K)
Sorts the populated slice with a key extraction function, but might not preserve the order of equal elements.
This sort is unstable (i.e., may reorder equal elements), in-place (i.e., does not allocate), and O(m * n * log(n)) worst-case, where the key function is O(m)
pub fn select_nth_unstable_by( &mut self, index: usize, compare: impl FnMut(&T, &T) -> Ordering, ) -> (&mut [T], &mut T, &mut [T])
pub fn select_nth_unstable_by_key<K: Ord>( &mut self, index: usize, key: impl FnMut(&T) -> K, ) -> (&mut [T], &mut T, &mut [T])
Sourcepub fn rotate_left(&mut self, mid: usize)
pub fn rotate_left(&mut self, mid: usize)
Rotates the slice in-place such that the first mid elements of
the slice move to the end while the last self.len() - mid
elements move to the front. After calling rotate_left, the
element previously at index mid will become the first element in
the slice.
Sourcepub fn rotate_right(&mut self, mid: usize)
pub fn rotate_right(&mut self, mid: usize)
Rotates the slice in-place such that the first self.len() - k
elements of the slice move to the end while the last k elements
move to the front. After calling rotate_right, the element
previously at index self.len() - k will become the first
element in the slice.
Sourcepub fn fill_with(&mut self, f: impl FnMut() -> T)
pub fn fill_with(&mut self, f: impl FnMut() -> T)
Fills self with elements returned by calling a closure repeatedly.
This method uses a closure to create new values. If you’d rather
Clone a given value, use fill. If you want to use the
Default trait to generate values, you can pass
Default::default as the argument.
Sourcepub fn swap_with_slice(&mut self, other: &mut PopulatedSlice<T>)
pub fn swap_with_slice(&mut self, other: &mut PopulatedSlice<T>)
Swaps all elements in self with those in other.
The length of other must be the same as self.
pub fn partition_point(&self, f: impl FnMut(&T) -> bool) -> usize
Sourcepub fn sort_by(&mut self, compare: impl FnMut(&T, &T) -> Ordering)
pub fn sort_by(&mut self, compare: impl FnMut(&T, &T) -> Ordering)
Sorts the populated slice with a comparator function.
This sort is stable (i.e., does not reorder equal elements) and O(n * log(n)) worst-case.
The comparator function must define a total ordering for the
elements in the slice. If the ordering is not total, the order of
the elements is unspecified. An order is a total order if it is
(for all a, b and c):
- total and antisymmetric: exactly one of
a < b,a == bora > bis true, and - transitive,
a < bandb < cimpliesa < c. The same must hold for both==and>.
For example, while f64 doesn’t implement Ord because NaN != NaN,
we can use partial_cmp as our sort function when we know the
slice doesn’t contain a NaN.
Sourcepub fn sort_by_key<K: Ord>(&mut self, key: impl FnMut(&T) -> K)
pub fn sort_by_key<K: Ord>(&mut self, key: impl FnMut(&T) -> K)
Sorts the slice with a key extraction function.
This sort is stable (i.e., does not reorder equal elements) and O(m * n * log(n)) worst-case, where the key function is O(m).
For expensive key functions (e.g. functions that are not simple
property accesses or basic operations), sort_by_cached_key is
likely to be significantly faster, as it does not recompute
element keys.
When applicable, unstable sorting is preferred because it is
generally faster than stable sorting and it doesn’t allocate
auxiliary memory. See sort_unstable_by_key.
Sourcepub fn sort_by_cached_key<K: Ord>(&mut self, key: impl FnMut(&T) -> K)
pub fn sort_by_cached_key<K: Ord>(&mut self, key: impl FnMut(&T) -> K)
Sorts the slice with a key extraction function.
During sorting, the key function is called at most once per element, by using temporary storage to remember the results of key evaluation. The order of calls to the key function is unspecified and may change in future versions of the standard library.
This sort is stable (i.e., does not reorder equal elements) and O(m * n + n * log(n)) worst-case, where the key function is O(m).
For simple key functions (e.g., functions that are property accesses
or basic operations), sort_by_key is likely to be faster.
Source§impl<T: Clone> PopulatedSlice<T>
impl<T: Clone> PopulatedSlice<T>
pub fn fill(&mut self, value: T)
Sourcepub fn clone_from_slice(&mut self, src: &PopulatedSlice<T>)
pub fn clone_from_slice(&mut self, src: &PopulatedSlice<T>)
Copies the elements from src into self.
The length of src must be the same as self.
Sourcepub fn to_vec(&self) -> PopulatedVec<T>
pub fn to_vec(&self) -> PopulatedVec<T>
Copies self into a new Vec.
Source§impl<T: Copy> PopulatedSlice<T>
impl<T: Copy> PopulatedSlice<T>
Sourcepub fn copy_from_slice(&mut self, src: &PopulatedSlice<T>)
pub fn copy_from_slice(&mut self, src: &PopulatedSlice<T>)
Copies all elements from src into self, using a memcpy.
The length of src must be the same as self.
If T does not implement Copy, use clone_from_slice.
Sourcepub fn copy_within(&mut self, src: impl RangeBounds<usize>, dest: usize)
pub fn copy_within(&mut self, src: impl RangeBounds<usize>, dest: usize)
Copies elements from one part of the slice to another part of itself, using a memmove.
src is the range within self to copy from. dest is the starting
index of the range within self to copy to, which will have the same
length as src. The two ranges may overlap. The ends of the two ranges
must be less than or equal to self.len().
pub fn repeat(&self, n: NonZeroUsize) -> PopulatedVec<T>
Source§impl<T: PartialEq> PopulatedSlice<T>
impl<T: PartialEq> PopulatedSlice<T>
Sourcepub fn contains(&self, x: &T) -> bool
pub fn contains(&self, x: &T) -> bool
Returns true if the slice contains an element with the given value.
This operation is O(n).
Note that if you have a sorted slice, binary_search may be
faster.
Sourcepub fn starts_with(&self, needle: &[T]) -> bool
pub fn starts_with(&self, needle: &[T]) -> bool
Returns true if needle is a prefix of the slice or equal to the slice.
Source§impl<T: Ord> PopulatedSlice<T>
impl<T: Ord> PopulatedSlice<T>
Sourcepub fn binary_search(&self, x: &T) -> Result<usize, usize>
pub fn binary_search(&self, x: &T) -> Result<usize, usize>
Binary searches this slice for a given element. If the slice is not sorted, the returned result is unspecified and meaningless.
If the value is found then Result::Ok is returned, containing the
index of the matching element. If there are multiple matches, then
any one of the matches could be returned. The index is chosen
deterministically, but is subject to change in future versions of
Rust. If the value is not found then 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.
Sourcepub fn sort_unstable(&mut self)
pub fn sort_unstable(&mut self)
Sorts the slice, but might not preserve the order of equal elements.
This sort is unstable (i.e., may reorder equal elements), in-place (i.e., does not allocate), and O(n * log(n)) worst-case.
pub fn select_nth_unstable(&mut self, index: usize)
Trait Implementations§
Source§impl<T> AsMut<PopulatedSlice<T>> for PopulatedVec<T>
impl<T> AsMut<PopulatedSlice<T>> for PopulatedVec<T>
Source§fn as_mut(&mut self) -> &mut PopulatedSlice<T>
fn as_mut(&mut self) -> &mut PopulatedSlice<T>
Source§impl<T> AsRef<PopulatedSlice<T>> for PopulatedVec<T>
impl<T> AsRef<PopulatedSlice<T>> for PopulatedVec<T>
Source§fn as_ref(&self) -> &PopulatedSlice<T>
fn as_ref(&self) -> &PopulatedSlice<T>
Source§impl<T> Borrow<PopulatedSlice<T>> for PopulatedVec<T>
impl<T> Borrow<PopulatedSlice<T>> for PopulatedVec<T>
Source§fn borrow(&self) -> &PopulatedSlice<T>
fn borrow(&self) -> &PopulatedSlice<T>
Source§impl<T> BorrowMut<PopulatedSlice<T>> for PopulatedVec<T>
impl<T> BorrowMut<PopulatedSlice<T>> for PopulatedVec<T>
Source§fn borrow_mut(&mut self) -> &mut PopulatedSlice<T>
fn borrow_mut(&mut self) -> &mut PopulatedSlice<T>
Source§impl<T: Debug> Debug for PopulatedSlice<T>
impl<T: Debug> Debug for PopulatedSlice<T>
Source§impl<'a, T> From<&'a PopulatedSlice<T>> for &'a [T]
impl<'a, T> From<&'a PopulatedSlice<T>> for &'a [T]
Source§fn from(populated_slice: &PopulatedSlice<T>) -> &[T]
fn from(populated_slice: &PopulatedSlice<T>) -> &[T]
Convert a PopulatedSlice to a slice.
Source§impl<'a, T: Clone> From<&'a PopulatedSlice<T>> for Cow<'a, PopulatedSlice<T>>
impl<'a, T: Clone> From<&'a PopulatedSlice<T>> for Cow<'a, PopulatedSlice<T>>
Source§fn from(slice: &'a PopulatedSlice<T>) -> Self
fn from(slice: &'a PopulatedSlice<T>) -> Self
Source§impl<T: Clone> From<&PopulatedSlice<T>> for PopulatedVec<T>
impl<T: Clone> From<&PopulatedSlice<T>> for PopulatedVec<T>
Source§fn from(value: &PopulatedSlice<T>) -> Self
fn from(value: &PopulatedSlice<T>) -> Self
Source§impl<'a, T> From<&'a mut PopulatedSlice<T>> for &'a mut [T]
impl<'a, T> From<&'a mut PopulatedSlice<T>> for &'a mut [T]
Source§fn from(populated_slice: &mut PopulatedSlice<T>) -> &mut [T]
fn from(populated_slice: &mut PopulatedSlice<T>) -> &mut [T]
Convert a mutable PopulatedSlice to a mutable slice.
Source§impl<T: Clone> From<&mut PopulatedSlice<T>> for PopulatedVec<T>
impl<T: Clone> From<&mut PopulatedSlice<T>> for PopulatedVec<T>
Source§fn from(value: &mut PopulatedSlice<T>) -> Self
fn from(value: &mut PopulatedSlice<T>) -> Self
Source§impl<T> Index<RangeFull> for PopulatedSlice<T>
impl<T> Index<RangeFull> for PopulatedSlice<T>
Source§impl<T> Index<RangeToInclusive<usize>> for PopulatedSlice<T>
impl<T> Index<RangeToInclusive<usize>> for PopulatedSlice<T>
Source§impl<T> Index<usize> for PopulatedSlice<T>
impl<T> Index<usize> for PopulatedSlice<T>
Source§impl<T> IndexMut<RangeFull> for PopulatedSlice<T>
impl<T> IndexMut<RangeFull> for PopulatedSlice<T>
Source§impl<T> IndexMut<RangeToInclusive<usize>> for PopulatedSlice<T>
impl<T> IndexMut<RangeToInclusive<usize>> for PopulatedSlice<T>
Source§impl<T> IndexMut<usize> for PopulatedSlice<T>
impl<T> IndexMut<usize> for PopulatedSlice<T>
Source§impl<'a, T> IntoIterator for &'a PopulatedSlice<T>
impl<'a, T> IntoIterator for &'a PopulatedSlice<T>
Source§impl<'a, T> IntoIterator for &'a mut PopulatedSlice<T>
impl<'a, T> IntoIterator for &'a mut PopulatedSlice<T>
Source§impl<'a, T> IntoPopulatedIterator for &'a PopulatedSlice<T>
impl<'a, T> IntoPopulatedIterator for &'a PopulatedSlice<T>
type PopulatedIntoIter = PopulatedIter<'a, T>
Source§fn into_populated_iter(self) -> PopulatedIter<'a, T>
fn into_populated_iter(self) -> PopulatedIter<'a, T>
PopulatedIterator.Source§impl<'a, T> IntoPopulatedIterator for &'a mut PopulatedSlice<T>
impl<'a, T> IntoPopulatedIterator for &'a mut PopulatedSlice<T>
type PopulatedIntoIter = PopulatedIterMut<'a, T>
Source§fn into_populated_iter(self) -> PopulatedIterMut<'a, T>
fn into_populated_iter(self) -> PopulatedIterMut<'a, T>
PopulatedIterator.Source§impl<T: Ord> Ord for PopulatedSlice<T>
impl<T: Ord> Ord for PopulatedSlice<T>
Source§impl<T: PartialEq> PartialEq for PopulatedSlice<T>
impl<T: PartialEq> PartialEq for PopulatedSlice<T>
Source§impl<T: PartialOrd> PartialOrd for PopulatedSlice<T>
impl<T: PartialOrd> PartialOrd for PopulatedSlice<T>
Source§impl<T: Clone> ToOwned for PopulatedSlice<T>
impl<T: Clone> ToOwned for PopulatedSlice<T>
Source§type Owned = PopulatedVec<T>
type Owned = PopulatedVec<T>
Source§fn to_owned(&self) -> Self::Owned
fn to_owned(&self) -> Self::Owned
1.63.0 · Source§fn clone_into(&self, target: &mut Self::Owned)
fn clone_into(&self, target: &mut Self::Owned)
Source§impl<'a, T> TryFrom<&'a [T]> for &'a PopulatedSlice<T>
impl<'a, T> TryFrom<&'a [T]> for &'a PopulatedSlice<T>
Source§fn try_from(slice: &'a [T]) -> Result<&'a PopulatedSlice<T>, Self::Error>
fn try_from(slice: &'a [T]) -> Result<&'a PopulatedSlice<T>, Self::Error>
Convert a slice to a PopulatedSlice. If the slice is empty, an error is returned. If the slice is not empty, a reference to the PopulatedSlice is returned.
§Examples
use populated::PopulatedSlice;
use std::convert::TryFrom;
let slice: &[u64] = &[1, 2, 3];
let populated_slice: &PopulatedSlice<u64> = TryFrom::try_from(slice).unwrap();
assert_eq!(populated_slice.len().get(), 3);use populated::{PopulatedSlice, UnpopulatedError};
use std::convert::TryFrom;
let slice: &[u64] = &[];
let populated_slice: Result<&PopulatedSlice<u64>, UnpopulatedError> = TryFrom::try_from(slice);
assert!(populated_slice.is_err());