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.
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)
) {
for index in &indices {
println!("Accessing item by index: {}", names[index.index(names.len())]);
println!("Accessing item by convenience method: {}", index.get(&names));
}
}
}
Return the real index that would be used to index a collection of size size
.
Panics if size == 0
.
Return a reference to the element in slice
that this Index
refers to.
A shortcut for &slice[index.index(slice.len())]
.
Return a mutable reference to the element in slice
that this Index
refers to.
A shortcut for &mut slice[index.index(slice.len())]
.
The type of parameters that [arbitrary_with
] accepts for configuration of the generated [Strategy
]. Parameters must implement [Default
]. Read more
The type of [Strategy
] used to generate values of type Self
. Read more
Generates a [Strategy
] for producing arbitrary values of type the implementing type (Self
). The strategy is passed the arguments given in args. Read more
Generates a [Strategy
] for producing arbitrary values of type the implementing type (Self
). Read more
Formats the value using the given formatter. Read more
Performs copy-assignment from source
. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more