Struct Index

struct Index(_)

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 Indexes 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...
    }
}
#
# fn main() { my_test(); }

Implementations

impl Index

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

Return the real index that would be used to index a collection of size size.

Panics

Panics if size == 0.

fn get<'a, T>(self: &Self, slice: &'a [T]) -> &'a T

Return a reference to the element in slice that this Index refers to.

A shortcut for &slice[index.index(slice.len())].

fn get_mut<'a, T>(self: &Self, slice: &'a mut [T]) -> &'a mut T

Return a mutable reference to the element in slice that this Index refers to.

A shortcut for &mut slice[index.index(slice.len())].

impl Arbitrary for crate::sample::Index

fn arbitrary_with(_: ()) -> IndexStrategy

impl AsRef for Index

fn as_ref(self: &Self) -> &Index

impl Clone for Index

fn clone(self: &Self) -> Index

impl Copy for Index

impl Debug for Index

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

impl Freeze for Index

impl RefUnwindSafe for Index

impl Send for Index

impl Sync for Index

impl Unpin for Index

impl UnwindSafe for Index

impl<T> Any for Index

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Index

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Index

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

impl<T> CloneToUninit for Index

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for Index

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Index

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for Index

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for Index

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Index

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>

impl<V, T> VZip for Index

fn vzip(self: Self) -> V