1use crate::{produce, TypeGenerator, ValueGenerator};
2
3#[cfg(not(kani))]
4mod default;
5
6#[cfg(any(kani, test))]
7#[cfg_attr(not(kani), allow(dead_code, unused_imports))]
8mod kani;
9
10#[cfg(test)]
11mod tests;
12
13pub mod scope {
14 #[cfg(not(kani))]
15 pub use super::default::*;
16 #[cfg(kani)]
17 pub use super::kani::*;
18}
19
20pub use scope::{assume, fill_bytes, Error};
21
22pub trait Any: ValueGenerator {
23 fn any(&self) -> Self::Output;
24}
25
26impl<G: 'static + ValueGenerator> Any for G {
27 #[track_caller]
28 fn any(&self) -> Self::Output {
29 scope::any(self)
30 }
31}
32
33#[inline]
34pub fn any<T: TypeGenerator>() -> T {
35 produce().any()
36}
37
38pub trait AnySliceExt<T> {
39 fn pick(&self) -> &T;
40}
41
42impl<T> AnySliceExt<T> for [T] {
43 #[inline]
44 fn pick(&self) -> &T {
45 let index = (0..self.len()).any();
46 &self[index]
47 }
48}
49
50pub trait AnySliceMutExt<T> {
51 fn shuffle(&mut self);
52 fn fill_any(&mut self)
53 where
54 T: TypeGenerator;
55}
56
57impl<T> AnySliceMutExt<T> for [T] {
58 #[inline]
59 fn shuffle(&mut self) {
60 let max_dst = self.len().saturating_sub(1);
61 for src in 0..max_dst {
62 let dst = (src..=max_dst).any();
63 self.swap(src, dst);
64 }
65 }
66
67 #[inline]
68 fn fill_any(&mut self)
69 where
70 T: TypeGenerator,
71 {
72 for value in self {
73 *value = any();
74 }
75 }
76}
77
78#[cfg(feature = "alloc")]
79impl<T> AnySliceMutExt<T> for alloc::collections::VecDeque<T> {
80 #[inline]
81 fn shuffle(&mut self) {
82 let max_dst = self.len().saturating_sub(1);
83 for src in 0..max_dst {
84 let dst = (src..=max_dst).any();
85 self.swap(src, dst);
86 }
87 }
88
89 #[inline]
90 fn fill_any(&mut self)
91 where
92 T: TypeGenerator,
93 {
94 for value in self {
95 *value = any();
96 }
97 }
98}
99
100#[inline]
101pub fn fill<T>(values: &mut [T])
102where
103 T: TypeGenerator,
104{
105 values.fill_any()
106}
107
108#[inline]
109pub fn shuffle<T>(items: &mut [T]) {
110 items.shuffle()
111}
112
113#[inline]
114pub fn pick<T>(items: &[T]) -> &T {
115 items.pick()
116}