3
3
4
4
//! This module introduces the Arbitrary trait as well as implementation for primitive types and
5
5
//! other std containers.
6
+
6
7
use std:: {
7
8
marker:: { PhantomData , PhantomPinned } ,
8
9
num:: * ,
9
10
} ;
10
11
11
12
/// This trait should be used to generate symbolic variables that represent any valid value of
12
13
/// its type.
13
- pub trait Arbitrary {
14
+ pub trait Arbitrary
15
+ where
16
+ Self : Sized ,
17
+ {
14
18
fn any ( ) -> Self ;
19
+ fn any_array < const MAX_ARRAY_LENGTH : usize > ( ) -> [ Self ; MAX_ARRAY_LENGTH ]
20
+ // the requirement defined in the where clause must appear on the `impl`'s method `any_array`
21
+ // but also on the corresponding trait's method
22
+ where
23
+ [ ( ) ; std:: mem:: size_of :: < [ Self ; MAX_ARRAY_LENGTH ] > ( ) ] : ,
24
+ {
25
+ [ ( ) ; MAX_ARRAY_LENGTH ] . map ( |_| Self :: any ( ) )
26
+ }
15
27
}
16
28
17
29
/// The given type can be represented by an unconstrained symbolic value of size_of::<T>.
@@ -20,8 +32,21 @@ macro_rules! trivial_arbitrary {
20
32
impl Arbitrary for $type {
21
33
#[ inline( always) ]
22
34
fn any( ) -> Self {
23
- // This size_of call does not use generic_const_exprs feature. It's inside a macro, and $type isn't generic.
24
- unsafe { crate :: any_raw_internal:: <$type, { std:: mem:: size_of:: <$type>( ) } >( ) }
35
+ // This size_of call does not use generic_const_exprs feature. It's inside a macro, and Self isn't generic.
36
+ unsafe { crate :: any_raw_internal:: <Self , { std:: mem:: size_of:: <Self >( ) } >( ) }
37
+ }
38
+ fn any_array<const MAX_ARRAY_LENGTH : usize >( ) -> [ Self ; MAX_ARRAY_LENGTH ]
39
+ where
40
+ // `generic_const_exprs` requires all potential errors to be reflected in the signature/header.
41
+ // We must repeat the expression in the header, to make sure that if the body can fail the header will also fail.
42
+ [ ( ) ; { std:: mem:: size_of:: <[ $type; MAX_ARRAY_LENGTH ] >( ) } ] : ,
43
+ {
44
+ unsafe {
45
+ crate :: any_raw_internal:: <
46
+ [ Self ; MAX_ARRAY_LENGTH ] ,
47
+ { std:: mem:: size_of:: <[ Self ; MAX_ARRAY_LENGTH ] >( ) } ,
48
+ >( )
49
+ }
25
50
}
26
51
}
27
52
} ;
@@ -99,9 +124,10 @@ nonzero_arbitrary!(NonZeroIsize, isize);
99
124
impl < T , const N : usize > Arbitrary for [ T ; N ]
100
125
where
101
126
T : Arbitrary ,
127
+ [ ( ) ; std:: mem:: size_of :: < [ T ; N ] > ( ) ] : ,
102
128
{
103
129
fn any ( ) -> Self {
104
- [ ( ) ; N ] . map ( |_| T :: any ( ) )
130
+ T :: any_array ( )
105
131
}
106
132
}
107
133
0 commit comments