proptest::sample

Struct Index

Source
pub struct Index(/* private fields */);
Expand description

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

Implementations§

Source§

impl Index

Source

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

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

§Panics

Panics if size == 0.

Source

pub fn get<'a, T>(&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())].

Source

pub fn get_mut<'a, T>(&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())].

Trait Implementations§

Source§

impl Arbitrary for Index

Source§

type Parameters = ()

The type of parameters that arbitrary_with accepts for configuration of the generated Strategy. Parameters must implement Default.
Source§

type Strategy = IndexStrategy

The type of Strategy used to generate values of type Self.
Source§

fn arbitrary_with(_: ()) -> IndexStrategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). The strategy is passed the arguments given in args. Read more
Source§

fn arbitrary() -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). Read more
Source§

impl AsRef<Index> for Index

Source§

fn as_ref(&self) -> &Index

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

impl Clone for Index

Source§

fn clone(&self) -> Index

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Index

Source§

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

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

impl Copy for Index

Auto Trait Implementations§

§

impl Freeze for Index

§

impl RefUnwindSafe for Index

§

impl Send for Index

§

impl Sync for Index

§

impl Unpin for Index

§

impl UnwindSafe for Index

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

impl<T> CloneToUninit for T
where T: Clone,

Source§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

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

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

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V