PopulatedSlice

Struct PopulatedSlice 

Source
pub struct PopulatedSlice<T>(/* private fields */);

Implementations§

Source§

impl<T> PopulatedSlice<T>

Source

pub fn iter(&self) -> PopulatedIter<'_, T>

Returns a populated iterator over the slice.

Source

pub fn iter_mut(&mut self) -> PopulatedIterMut<'_, T>

Returns a mutable populated iterator over the slice.

Source

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());
Source

pub fn first(&self) -> &T

Returns the first element of the populated slice.

Source

pub fn first_mut(&mut self) -> &mut T

Returns a mutable pointer to the first element of the populated slice.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn last(&self) -> &T

Returns the last element of the populated slice.

Source

pub fn last_mut(&mut self) -> &mut T

Returns a mutable reference to the last item in the populated slice.

Source

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.

Source

pub fn reverse(&mut self)

Reverses the order of elements in the slice, in place.

Source

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.

Source

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).

Source

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.

Source

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).

Source

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.

Source

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.

Source

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.

Source

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)

Source

pub fn select_nth_unstable_by( &mut self, index: usize, compare: impl FnMut(&T, &T) -> Ordering, ) -> (&mut [T], &mut T, &mut [T])

Source

pub fn select_nth_unstable_by_key<K: Ord>( &mut self, index: usize, key: impl FnMut(&T) -> K, ) -> (&mut [T], &mut T, &mut [T])

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn partition_point(&self, f: impl FnMut(&T) -> bool) -> usize

Source

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 == 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.

Source

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.

Source

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>

Source

pub fn fill(&mut self, value: T)

Source

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.

Source

pub fn to_vec(&self) -> PopulatedVec<T>

Copies self into a new Vec.

Source§

impl<T: Copy> PopulatedSlice<T>

Source

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.

Source

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().

Source

pub fn repeat(&self, n: NonZeroUsize) -> PopulatedVec<T>

Source§

impl<T: PartialEq> PopulatedSlice<T>

Source

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.

Source

pub fn starts_with(&self, needle: &[T]) -> bool

Returns true if needle is a prefix of the slice or equal to the slice.

Source

pub fn ends_with(&self, needle: &[T]) -> bool

Returns true if needle is a suffix of the slice or equal to the slice.

Source§

impl<T: Ord> PopulatedSlice<T>

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.

Source

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.

Source

pub fn select_nth_unstable(&mut self, index: usize)

Source

pub fn sort(&mut self)

Sorts the slice.

This sort is stable (i.e., does not reorder equal elements) and $O(n * log(n))$ worst-case.

When applicable, unstable sorting is preferred because it is generally faster than stable sorting and it doesn’t allocate auxiliary memory. See sort_unstable.

Source§

impl PopulatedSlice<u8>

Source

pub const fn is_ascii(&self) -> bool

Checks if all bytes in this slice are within the ASCII range.

Trait Implementations§

Source§

impl<T> AsMut<PopulatedSlice<T>> for PopulatedVec<T>

Source§

fn as_mut(&mut self) -> &mut PopulatedSlice<T>

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T> AsRef<PopulatedSlice<T>> for PopulatedVec<T>

Source§

fn as_ref(&self) -> &PopulatedSlice<T>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> Borrow<PopulatedSlice<T>> for PopulatedVec<T>

Source§

fn borrow(&self) -> &PopulatedSlice<T>

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<PopulatedSlice<T>> for PopulatedVec<T>

Source§

fn borrow_mut(&mut self) -> &mut PopulatedSlice<T>

Mutably borrows from an owned value. Read more
Source§

impl<T: Debug> Debug for PopulatedSlice<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T> From<&'a PopulatedSlice<T>> for &'a [T]

Source§

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>>

Source§

fn from(slice: &'a PopulatedSlice<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Clone> From<&PopulatedSlice<T>> for PopulatedVec<T>

Source§

fn from(value: &PopulatedSlice<T>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T> From<&'a mut PopulatedSlice<T>> for &'a mut [T]

Source§

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>

Source§

fn from(value: &mut PopulatedSlice<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> Index<RangeFull> for PopulatedSlice<T>

Source§

type Output = PopulatedSlice<T>

The returned type after indexing.
Source§

fn index(&self, _index: RangeFull) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> Index<RangeTo<NonZero<usize>>> for PopulatedSlice<T>

Source§

type Output = PopulatedSlice<T>

The returned type after indexing.
Source§

fn index(&self, index: RangeTo<NonZeroUsize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> Index<RangeToInclusive<usize>> for PopulatedSlice<T>

Source§

type Output = PopulatedSlice<T>

The returned type after indexing.
Source§

fn index(&self, index: RangeToInclusive<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> Index<usize> for PopulatedSlice<T>

Source§

fn index(&self, index: usize) -> &Self::Output

Index into the populated slice.

Source§

type Output = T

The returned type after indexing.
Source§

impl<T> IndexMut<RangeFull> for PopulatedSlice<T>

Source§

fn index_mut(&mut self, _index: RangeFull) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<RangeTo<NonZero<usize>>> for PopulatedSlice<T>

Source§

fn index_mut(&mut self, index: RangeTo<NonZeroUsize>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<RangeToInclusive<usize>> for PopulatedSlice<T>

Source§

fn index_mut(&mut self, index: RangeToInclusive<usize>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<usize> for PopulatedSlice<T>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Index into the populated slice mutably.

Source§

impl<'a, T> IntoIterator for &'a PopulatedSlice<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a mut PopulatedSlice<T>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoPopulatedIterator for &'a PopulatedSlice<T>

Source§

type PopulatedIntoIter = PopulatedIter<'a, T>

Source§

fn into_populated_iter(self) -> PopulatedIter<'a, T>

Converts the type into a PopulatedIterator.
Source§

impl<'a, T> IntoPopulatedIterator for &'a mut PopulatedSlice<T>

Source§

type PopulatedIntoIter = PopulatedIterMut<'a, T>

Source§

fn into_populated_iter(self) -> PopulatedIterMut<'a, T>

Converts the type into a PopulatedIterator.
Source§

impl<T: Ord> Ord for PopulatedSlice<T>

Source§

fn cmp(&self, other: &PopulatedSlice<T>) -> Ordering

This method returns an Ordering between self and other. Read more
Source§

impl<T: PartialEq> PartialEq for PopulatedSlice<T>

Source§

fn eq(&self, other: &PopulatedSlice<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd for PopulatedSlice<T>

Source§

fn partial_cmp(&self, other: &PopulatedSlice<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Clone> ToOwned for PopulatedSlice<T>

Source§

type Owned = PopulatedVec<T>

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> Self::Owned

Creates owned data from borrowed data, usually by cloning. Read more
1.63.0 · Source§

fn clone_into(&self, target: &mut Self::Owned)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<'a, T> TryFrom<&'a [T]> for &'a PopulatedSlice<T>

Source§

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());
Source§

type Error = UnpopulatedError

The type returned in the event of a conversion error.
Source§

impl<'a, T> TryFrom<&'a mut [T]> for &'a mut PopulatedSlice<T>

Source§

type Error = UnpopulatedError

The type returned in the event of a conversion error.
Source§

fn try_from( slice: &'a mut [T], ) -> Result<&'a mut PopulatedSlice<T>, Self::Error>

Performs the conversion.
Source§

impl<T: Eq> Eq for PopulatedSlice<T>

Source§

impl<T> StructuralPartialEq for PopulatedSlice<T>

Auto Trait Implementations§

§

impl<T> Freeze for PopulatedSlice<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for PopulatedSlice<T>
where T: RefUnwindSafe,

§

impl<T> Send for PopulatedSlice<T>
where T: Send,

§

impl<T> !Sized for PopulatedSlice<T>

§

impl<T> Sync for PopulatedSlice<T>
where T: Sync,

§

impl<T> Unpin for PopulatedSlice<T>
where T: Unpin,

§

impl<T> UnwindSafe for PopulatedSlice<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more