Struct proptest::sample::Index [−][src]
A stand-in for an index into a slice or similar collection or conceptually similar things.
At the lowest level, Index
is a mechanism for generating usize
values
in the range [0..N), for some N whose value is not known until it is
needed. (Contrast with using 0..N
itself as a strategy, where you need to
know N when you define the strategy.)
For any upper bound, the actual index produced by an Index
is the same no
matter how many times it is used. Different upper bounds will produce
different but not independent values.
Shrinking will cause the index to binary search through the underlying collection(s) it is used to sample.
Note that Index
cannot currently be used as a slice index (e.g.,
slice[index]
) due to the trait coherence rules.
Example
If the collection itself being indexed is itself generated by a strategy,
you can make separately define that strategy and a strategy generating one
or more Index
es and then join the two after input generation, avoiding a
call to prop_flat_map()
.
use proptest::prelude::*; proptest! { #[test] fn my_test( names in prop::collection::vec("[a-z]+", 10..20), indices in prop::collection::vec(any::<prop::sample::Index>(), 5..10) ) { // We now have Vec<String> of ten to twenty names, and a Vec<Index> // of five to ten indices and can combine them however we like. for index in &indices { println!("Accessing item by index: {}", names[index.index(names.len())]); println!("Accessing item by convenience method: {}", index.get(&names)); } // Test stuff... } }
Implementations
impl Index
[src][−]
pub fn index(&self, size: usize) -> usize
[src][−]
Return the real index that would be used to index a collection of size size
.
Panics
Panics if size == 0
.
pub fn get<'a, T>(&self, slice: &'a [T]) -> &'a T
[src][−]
Return a reference to the element in slice
that this Index
refers to.
A shortcut for &slice[index.index(slice.len())]
.
pub fn get_mut<'a, T>(&self, slice: &'a mut [T]) -> &'a mut T
[src][−]
Return a mutable reference to the element in slice
that this Index
refers to.
A shortcut for &mut slice[index.index(slice.len())]
.
Trait Implementations
impl Arbitrary for Index
[src][+]
impl Clone for Index
[src][+]
impl Copy for Index
[src]
impl Debug for Index
[src][+]
Auto Trait Implementations
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src][+]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src][+]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src][+]
T: ?Sized,
impl<T> From<T> for T
[src][+]
impl<T, U> Into<U> for T where
U: From<T>,
[src][+]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src][+]
T: Clone,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src][+]
U: Into<T>,
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src][+]
U: TryFrom<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
[+]
V: MultiLane<T>,