#[repr(transparent)]
pub struct TiSlice<K, V> { pub raw: [V], /* private fields */ }
Expand description

A dynamically-sized view into a contiguous sequence of T that only accepts keys of the type K.

TiSlice<K, V> is a wrapper around Rust primitive type slice. The struct mirrors the stable API of Rust slice and forwards to it as much as possible.

TiSlice<K, V> uses K instead of usize for element indices. It also uses Range, RangeTo, RangeFrom, RangeInclusive and RangeToInclusive range types with K indices for get-methods and index expressions. The RangeFull trait is not currently supported.

TiSlice<K, V> require the index to implement From<usize> and Into<usize> traits. Their implementation can be easily done with derive_more crate and #[derive(From, Into)].

There are zero-cost conversions available between types and references:

Added methods:

  • from_ref - Converts a &[V] into a &TiSlice<K, V>.
  • from_mut - Converts a &mut [V] into a &mut TiSlice<K, V>.
  • keys - Returns an iterator over all keys.
  • next_key - Returns the index of the next slice element to be appended and at the same time number of elements in the slice of type K.
  • first_key - Returns the first slice element index of type K, or None if the slice is empty.
  • first_key_value - Returns the first slice element index of type K and the element itself, or None if the slice is empty.
  • first_key_value_mut - Returns the first slice element index of type K and a mutable reference to the element itself, or None if the slice is empty.
  • last_key - Returns the last slice element index of type K, or None if the slice is empty.
  • last_key_value - Returns the last slice element index of type K and the element itself, or None if the slice is empty.
  • last_key_value_mut - Returns the last slice element index of type K and a mutable reference to the element itself, or None if the slice is empty.
  • iter_enumerated - Returns an iterator over all key-value pairs. It acts like self.iter().enumerate(), but use K instead of usize for iteration indices.
  • iter_mut_enumerated - Returns an iterator over all key-value pairs, with mutable references to the values. It acts like self.iter_mut().enumerate(), but use K instead of usize for iteration indices.
  • position - Searches for an element in an iterator, returning its index of type K. It acts like self.iter().position(...), but instead of usize it returns index of type K.
  • rposition - Searches for an element in an iterator from the right, returning its index of type K. It acts like self.iter().rposition(...), but instead of usize it returns index of type K.

Example

use typed_index_collections::TiSlice;
use derive_more::{From, Into};

#[derive(From, Into)]
struct FooId(usize);

let mut foos_raw = [1, 2, 5, 8];
let foos: &mut TiSlice<FooId, usize> = TiSlice::from_mut(&mut foos_raw);
foos[FooId(2)] = 4;
assert_eq!(foos[FooId(2)], 4);

Fields

raw: [V]

Raw slice property

Implementations

Converts a &[V] into a &TiSlice<K, V>.

Example
pub struct Id(usize);
let slice: &TiSlice<Id, usize> = TiSlice::from_ref(&[1, 2, 4]);

Converts a &mut [V] into a &mut TiSlice<K, V>.

Example
pub struct Id(usize);
let slice: &mut TiSlice<Id, usize> = TiSlice::from_mut(&mut [1, 2, 4]);

Returns the number of elements in the slice.

See slice::len for more details.

Returns the index of the next slice element to be appended and at the same time number of elements in the slice of type K.

Example
#[derive(Eq, Debug, From, Into, PartialEq)]
pub struct Id(usize);
let slice: &TiSlice<Id, usize> = TiSlice::from_ref(&[1, 2, 4]);
assert_eq!(slice.next_key(), Id(3));

Returns true if the slice has a length of 0.

See slice::is_empty for more details.

Returns an iterator over all keys.

Example
#[derive(Debug, Eq, From, Into, PartialEq)]
pub struct Id(usize);
let slice: &TiSlice<Id, usize> = TiSlice::from_ref(&[1, 2, 4]);
let mut iterator = slice.keys();
assert_eq!(iterator.next(), Some(Id(0)));
assert_eq!(iterator.next(), Some(Id(1)));
assert_eq!(iterator.next(), Some(Id(2)));
assert_eq!(iterator.next(), None);

Returns the first element of the slice, or None if it is empty.

See slice::first for more details.

Returns a mutable reference to the first element of the slice, or None if it is empty.

See slice::first_mut for more details.

Returns the first slice element index of type K, or None if the slice is empty.

Example
#[derive(Debug, Eq, From, Into, PartialEq)]
pub struct Id(usize);
let empty_slice: &TiSlice<Id, usize> = TiSlice::from_ref(&[]);
let slice: &TiSlice<Id, usize> = TiSlice::from_ref(&[1, 2, 4]);
assert_eq!(empty_slice.first_key(), None);
assert_eq!(slice.first_key(), Some(Id(0)));

Returns the first slice element index of type K and the element itself, or None if the slice is empty.

See slice::first for more details.

Example
#[derive(Debug, Eq, From, Into, PartialEq)]
pub struct Id(usize);
let empty_slice: &TiSlice<Id, usize> = TiSlice::from_ref(&[]);
let slice: &TiSlice<Id, usize> = TiSlice::from_ref(&[1, 2, 4]);
assert_eq!(empty_slice.first_key_value(), None);
assert_eq!(slice.first_key_value(), Some((Id(0), &1)));

Returns the first slice element index of type K and a mutable reference to the element itself, or None if the slice is empty.

See slice::first_mut for more details.

Example
#[derive(Debug, Eq, From, Into, PartialEq)]
pub struct Id(usize);
let empty_slice: &mut TiSlice<Id, usize> = TiSlice::from_mut(&mut []);
let mut array = [1, 2, 4];
let slice: &mut TiSlice<Id, usize> = TiSlice::from_mut(&mut array);
assert_eq!(empty_slice.first_key_value_mut(), None);
assert_eq!(slice.first_key_value_mut(), Some((Id(0), &mut 1)));
*slice.first_key_value_mut().unwrap().1 = 123;
assert_eq!(slice.raw, [123, 2, 4]);

Returns the first and all the rest of the elements of the slice, or None if it is empty.

See slice::split_first for more details.

Returns the first and all the rest of the elements of the slice, or None if it is empty.

See slice::split_first_mut for more details.

Returns the last and all the rest of the elements of the slice, or None if it is empty.

See slice::split_last for more details.

Returns the last and all the rest of the elements of the slice, or None if it is empty.

See slice::split_last_mut for more details.

Returns the last element of the slice of type K, or None if it is empty.

See slice::last for more details.

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

See slice::last_mut for more details.

Returns the last slice element index of type K, or None if the slice is empty.

Example
#[derive(Debug, Eq, From, Into, PartialEq)]
pub struct Id(usize);
let empty_slice: &TiSlice<Id, usize> = TiSlice::from_ref(&[]);
let slice: &TiSlice<Id, usize> = TiSlice::from_ref(&[1, 2, 4]);
assert_eq!(empty_slice.last_key(), None);
assert_eq!(slice.last_key(), Some(Id(2)));

Returns the last slice element index of type K and the element itself, or None if the slice is empty.

See slice::last for more details.

Example
#[derive(Debug, Eq, From, Into, PartialEq)]
pub struct Id(usize);
let empty_slice: &TiSlice<Id, usize> = TiSlice::from_ref(&[]);
let slice: &TiSlice<Id, usize> = TiSlice::from_ref(&[1, 2, 4]);
assert_eq!(empty_slice.last_key_value(), None);
assert_eq!(slice.last_key_value(), Some((Id(2), &4)));

Returns the last slice element index of type K and a mutable reference to the element itself, or None if the slice is empty.

See slice::last_mut for more details.

Example
#[derive(Debug, Eq, From, Into, PartialEq)]
pub struct Id(usize);
let empty_slice: &mut TiSlice<Id, usize> = TiSlice::from_mut(&mut []);
let mut array = [1, 2, 4];
let slice: &mut TiSlice<Id, usize> = TiSlice::from_mut(&mut array);
assert_eq!(empty_slice.last_key_value_mut(), None);
assert_eq!(slice.last_key_value_mut(), Some((Id(2), &mut 4)));
*slice.last_key_value_mut().unwrap().1 = 123;
assert_eq!(slice.raw, [1, 2, 123]);

Returns a reference to an element or subslice depending on the type of index or None if the index is out of bounds.

See slice::get for more details.

Returns a mutable reference to an element or subslice depending on the type of index or None if the index is out of bounds.

See slice::get_mut for more details.

Returns a reference to an element or subslice depending on the type of index, without doing bounds checking.

This is generally not recommended, use with caution! Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used. For a safe alternative see get.

See slice::get_unchecked for more details.

Returns a mutable reference to an element or subslice depending on the type of index, without doing bounds checking.

This is generally not recommended, use with caution! Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used. For a safe alternative see get_mut.

See slice::get_unchecked_mut for more details.

Returns a raw pointer to the slice’s buffer.

See slice::as_ptr for more details.

Returns an unsafe mutable reference to the slice’s buffer.

See slice::as_mut_ptr for more details.

Swaps two elements in the slice.

See slice::swap for more details.

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

See slice::reverse for more details.

Returns an iterator over the slice.

See slice::iter for more details.

Returns an iterator over all key-value pairs.

It acts like self.iter().enumerate(), but use K instead of usize for iteration indices.

See slice::iter for more details.

Example
#[derive(Debug, Eq, From, Into, PartialEq)]
pub struct Id(usize);
let slice: &TiSlice<Id, usize> = TiSlice::from_ref(&[1, 2, 4]);
let mut iterator = slice.iter_enumerated();
assert_eq!(iterator.next(), Some((Id(0), &1)));
assert_eq!(iterator.next(), Some((Id(1), &2)));
assert_eq!(iterator.next(), Some((Id(2), &4)));
assert_eq!(iterator.next(), None);

Returns an iterator that allows modifying each value.

See slice::iter_mut for more details.

Returns an iterator over all key-value pairs, with mutable references to the values.

It acts like self.iter_mut().enumerate(), but use K instead of usize for iteration indices.

Example
#[derive(Debug, Eq, From, Into, PartialEq)]
pub struct Id(usize);
let mut array = [1, 2, 4];
let slice: &mut TiSlice<Id, usize> = TiSlice::from_mut(&mut array);
for (key, value) in slice.iter_mut_enumerated() {
    *value += key.0;
}
assert_eq!(array, [1, 3, 6]);

Searches for an element in an iterator, returning its index of type K.

It acts like self.iter().position(...), but instead of usize it returns index of type K.

See slice::iter and Iterator::position for more details.

Example
#[derive(Debug, Eq, From, Into, PartialEq)]
pub struct Id(usize);
let slice: &TiSlice<Id, usize> = TiSlice::from_ref(&[1, 2, 4, 2, 1]);
assert_eq!(slice.position(|&value| value == 1), Some(Id(0)));
assert_eq!(slice.position(|&value| value == 2), Some(Id(1)));
assert_eq!(slice.position(|&value| value == 3), None);
assert_eq!(slice.position(|&value| value == 4), Some(Id(2)));

Searches for an element in an iterator from the right, returning its index of type K.

It acts like self.iter().rposition(...), but instead of usize it returns index of type K.

See slice::iter and Iterator::position for more details.

Example
#[derive(Debug, Eq, From, Into, PartialEq)]
pub struct Id(usize);
let slice: &TiSlice<Id, usize> = TiSlice::from_ref(&[1, 2, 4, 2, 1]);
assert_eq!(slice.rposition(|&value| value == 1), Some(Id(4)));
assert_eq!(slice.rposition(|&value| value == 2), Some(Id(3)));
assert_eq!(slice.rposition(|&value| value == 3), None);
assert_eq!(slice.rposition(|&value| value == 4), Some(Id(2)));

Returns an iterator over all contiguous windows of length size. The windows overlap. If the slice is shorter than size, the iterator returns no values.

See slice::windows for more details.

Returns an iterator over chunk_size elements of the slice at a time, starting at the beginning of the slice.

See slice::chunks for more details.

Returns an iterator over chunk_size elements of the slice at a time, starting at the beginning of the slice.

See slice::chunks_mut for more details.

Returns an iterator over chunk_size elements of the slice at a time, starting at the beginning of the slice.

See slice::chunks_exact for more details.

Returns an iterator over chunk_size elements of the slice at a time, starting at the beginning of the slice.

See slice::chunks_exact_mut for more details.

Returns an iterator over chunk_size elements of the slice at a time, starting at the end of the slice.

See slice::rchunks for more details.

Returns an iterator over chunk_size elements of the slice at a time, starting at the end of the slice.

See slice::rchunks_mut for more details.

Returns an iterator over chunk_size elements of the slice at a time, starting at the end of the slice.

See slice::rchunks_exact for more details.

Returns an iterator over chunk_size elements of the slice at a time, starting at the end of the slice.

See slice::rchunks_exact_mut for more details.

Divides one slice into two at an index.

See slice::split_at for more details.

Divides one mutable slice into two at an index.

See slice::split_at_mut for more details.

Returns an iterator over subslices separated by elements that match pred. The matched element is not contained in the subslices.

See slice::split for more details.

Returns an iterator over mutable subslices separated by elements that match pred. The matched element is not contained in the subslices.

See slice::split_mut for more details.

Returns an iterator over subslices separated by elements that match pred, starting at the end of the slice and working backwards. The matched element is not contained in the subslices.

See slice::rsplit for more details.

Returns an iterator over mutable subslices separated by elements that match pred, starting at the end of the slice and working backwards. The matched element is not contained in the subslices.

See slice::rsplit_mut for more details.

Returns an iterator over subslices separated by elements that match pred, limited to returning at most n items. The matched element is not contained in the subslices.

See slice::splitn for more details.

Returns an iterator over subslices separated by elements that match pred, limited to returning at most n items. The matched element is not contained in the subslices.

See slice::splitn_mut for more details.

Returns an iterator over subslices separated by elements that match pred limited to returning at most n items. This starts at the end of the slice and works backwards. The matched element is not contained in the subslices.

See slice::rsplitn for more details.

Returns an iterator over subslices separated by elements that match pred limited to returning at most n items. This starts at the end of the slice and works backwards. The matched element is not contained in the subslices.

See slice::rsplitn_mut for more details.

Returns true if the slice contains an element with the given value.

See slice::contains for more details.

Returns true if needle is a prefix of the slice.

See slice::starts_with for more details.

Returns true if needle is a suffix of the slice.

See slice::ends_with for more details.

Binary searches this sorted slice for a given element.

See slice::binary_search for more details.

Binary searches this sorted slice with a comparator function.

See slice::binary_search_by for more details.

Binary searches this sorted slice with a key extraction function.

See slice::binary_search_by_key for more details.

Sorts the slice, but may not preserve the order of equal elements.

See slice::sort_unstable for more details.

Sorts the slice with a comparator function, but may not preserve the order of equal elements.

See slice::sort_unstable_by for more details.

Sorts the slice with a key extraction function, but may not preserve the order of equal elements.

See slice::sort_unstable_by_key for more details.

Rotates the slice in-place such that the first mid elements of the slice move to the end while the last self.next_key() - mid elements move to the front. After calling rotate_left, the element previously at index mid will become the first element in the slice.

See slice::rotate_left for more details.

Rotates the slice in-place such that the first self.next_key() - 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.next_key() - k will become the first element in the slice.

See slice::rotate_right for more details.

Copies the elements from src into self.

See slice::clone_from_slice for more details.

Copies all elements from src into self, using a memcpy.

See slice::copy_from_slice for more details.

Copies elements from one part of the slice to another part of itself, using a memmove.

See slice::copy_within for more details.

Swaps all elements in self with those in other.

See slice::swap_with_slice for more details.

Transmute the slice to a slice of another type, ensuring alignment of the types is maintained.

See slice::align_to for more details.

Transmute the slice to a slice of another type, ensuring alignment of the types is maintained.

See slice::align_to_mut for more details.

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

See slice::is_ascii for more details.

Checks that two slices are an ASCII case-insensitive match.

See slice::eq_ignore_ascii_case for more details.

Converts this slice to its ASCII upper case equivalent in-place.

See slice::make_ascii_uppercase for more details.

Converts this slice to its ASCII lower case equivalent in-place.

See slice::make_ascii_lowercase for more details.

Sorts the slice.

See slice::sort for more details.

Sorts the slice with a comparator function.

See slice::sort_by for more details.

Sorts the slice with a key extraction function.

See slice::sort_by_key for more details.

Sorts the slice with a key extraction function.

See slice::sort_by_cached_key for more details.

Copies self into a new TiVec.

See slice::to_vec for more details.

Converts self into a vector without clones or allocation.

See slice::into_vec for more details.

Creates a vector by repeating a slice n times.

See slice::repeat for more details.

Flattens a slice of T into a single value Self::Output.

See slice::concat for more details.

Flattens a slice of T into a single value Self::Output, placing a given separator between each.

See slice::join for more details.

Returns a vector containing a copy of this slice where each byte is mapped to its ASCII upper case equivalent.

See slice::to_ascii_uppercase for more details.

Returns a vector containing a copy of this slice where each byte is mapped to its ASCII lower case equivalent.

See slice::to_ascii_lowercase for more details.

Trait Implementations

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

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

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

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

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

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

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

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

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Returns the “default value” for a type. Read more

Returns the “default value” for a type. Read more

Deserialize this value from the given Serde deserializer. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Creates a value from an iterator. Read more

Feeds this value into the given Hasher. Read more

The returned type after indexing.

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

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

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

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

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

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Serialize this value into the given Serde serializer. Read more

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.