33
44//! This module introduces the Arbitrary trait as well as implementation for primitive types and
55//! other std containers.
6+
67use std:: {
78 marker:: { PhantomData , PhantomPinned } ,
89 num:: * ,
910} ;
1011
1112/// This trait should be used to generate symbolic variables that represent any valid value of
1213/// its type.
13- pub trait Arbitrary {
14+ pub trait Arbitrary
15+ where
16+ Self : Sized ,
17+ {
1418 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+ }
1527}
1628
1729/// The given type can be represented by an unconstrained symbolic value of size_of::<T>.
@@ -20,8 +32,21 @@ macro_rules! trivial_arbitrary {
2032 impl Arbitrary for $type {
2133 #[ inline( always) ]
2234 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+ }
2550 }
2651 }
2752 } ;
@@ -99,9 +124,10 @@ nonzero_arbitrary!(NonZeroIsize, isize);
99124impl < T , const N : usize > Arbitrary for [ T ; N ]
100125where
101126 T : Arbitrary ,
127+ [ ( ) ; std:: mem:: size_of :: < [ T ; N ] > ( ) ] : ,
102128{
103129 fn any ( ) -> Self {
104- [ ( ) ; N ] . map ( |_| T :: any ( ) )
130+ T :: any_array ( )
105131 }
106132}
107133
0 commit comments