@@ -64,7 +64,7 @@ fn expand_derive_arbitrary(input: syn::DeriveInput) -> Result<TokenStream> {
64
64
}
65
65
66
66
#[ automatically_derived]
67
- impl #impl_generics arbitrary:: Arbitrary <#lifetime_without_bounds> for #name #ty_generics #where_clause {
67
+ impl #impl_generics :: arbitrary:: Arbitrary <#lifetime_without_bounds> for #name #ty_generics #where_clause {
68
68
#arbitrary_method
69
69
#size_hint_method
70
70
}
@@ -141,7 +141,7 @@ fn add_trait_bounds(mut generics: Generics, lifetime: LifetimeParam) -> Generics
141
141
if let GenericParam :: Type ( type_param) = param {
142
142
type_param
143
143
. bounds
144
- . push ( parse_quote ! ( arbitrary:: Arbitrary <#lifetime>) ) ;
144
+ . push ( parse_quote ! ( :: arbitrary:: Arbitrary <#lifetime>) ) ;
145
145
}
146
146
}
147
147
generics
@@ -156,7 +156,7 @@ fn with_recursive_count_guard(
156
156
if guard_against_recursion {
157
157
#recursive_count. with( |count| {
158
158
if count. get( ) > 0 {
159
- return Err ( arbitrary:: Error :: NotEnoughData ) ;
159
+ return Err ( :: arbitrary:: Error :: NotEnoughData ) ;
160
160
}
161
161
count. set( count. get( ) + 1 ) ;
162
162
Ok ( ( ) )
@@ -194,11 +194,11 @@ fn gen_arbitrary_method(
194
194
with_recursive_count_guard ( recursive_count, quote ! { Ok ( #ident #arbitrary_take_rest) } ) ;
195
195
196
196
Ok ( quote ! {
197
- fn arbitrary( u: & mut arbitrary:: Unstructured <#lifetime>) -> arbitrary:: Result <Self > {
197
+ fn arbitrary( u: & mut :: arbitrary:: Unstructured <#lifetime>) -> :: arbitrary:: Result <Self > {
198
198
#body
199
199
}
200
200
201
- fn arbitrary_take_rest( mut u: arbitrary:: Unstructured <#lifetime>) -> arbitrary:: Result <Self > {
201
+ fn arbitrary_take_rest( mut u: :: arbitrary:: Unstructured <#lifetime>) -> :: arbitrary:: Result <Self > {
202
202
#take_rest_body
203
203
}
204
204
} )
@@ -225,7 +225,7 @@ fn gen_arbitrary_method(
225
225
// Use a multiply + shift to generate a ranged random number
226
226
// with slight bias. For details, see:
227
227
// https://lemire.me/blog/2016/06/30/fast-random-shuffling
228
- Ok ( match ( u64 :: from( <u32 as arbitrary:: Arbitrary >:: arbitrary( #unstructured) ?) * #count) >> 32 {
228
+ Ok ( match ( u64 :: from( <u32 as :: arbitrary:: Arbitrary >:: arbitrary( #unstructured) ?) * #count) >> 32 {
229
229
#( #variants, ) *
230
230
_ => unreachable!( )
231
231
} )
@@ -279,11 +279,11 @@ fn gen_arbitrary_method(
279
279
let arbitrary_take_rest = arbitrary_enum_method ( recursive_count, quote ! { & mut u } , & variants_take_rest) ;
280
280
281
281
quote ! {
282
- fn arbitrary( u: & mut arbitrary:: Unstructured <#lifetime>) -> arbitrary:: Result <Self > {
282
+ fn arbitrary( u: & mut :: arbitrary:: Unstructured <#lifetime>) -> :: arbitrary:: Result <Self > {
283
283
#arbitrary
284
284
}
285
285
286
- fn arbitrary_take_rest( mut u: arbitrary:: Unstructured <#lifetime>) -> arbitrary:: Result <Self > {
286
+ fn arbitrary_take_rest( mut u: :: arbitrary:: Unstructured <#lifetime>) -> :: arbitrary:: Result <Self > {
287
287
#arbitrary_take_rest
288
288
}
289
289
}
@@ -344,9 +344,9 @@ fn construct_take_rest(fields: &Fields) -> Result<TokenStream> {
344
344
FieldConstructor :: Default => quote ! ( :: core:: default :: Default :: default ( ) ) ,
345
345
FieldConstructor :: Arbitrary => {
346
346
if idx + 1 == fields. len ( ) {
347
- quote ! { arbitrary:: Arbitrary :: arbitrary_take_rest( u) ? }
347
+ quote ! { :: arbitrary:: Arbitrary :: arbitrary_take_rest( u) ? }
348
348
} else {
349
- quote ! { arbitrary:: Arbitrary :: arbitrary( & mut u) ? }
349
+ quote ! { :: arbitrary:: Arbitrary :: arbitrary( & mut u) ? }
350
350
}
351
351
}
352
352
FieldConstructor :: With ( function_or_closure) => quote ! ( ( #function_or_closure) ( & mut u) ?) ,
@@ -364,41 +364,36 @@ fn gen_size_hint_method(input: &DeriveInput) -> Result<TokenStream> {
364
364
determine_field_constructor ( f) . map ( |field_constructor| {
365
365
match field_constructor {
366
366
FieldConstructor :: Default | FieldConstructor :: Value ( _) => {
367
- quote ! ( Ok ( ( 0 , Some ( 0 ) ) ) )
367
+ quote ! ( :: arbitrary :: SizeHint :: exactly ( 0 ) )
368
368
}
369
369
FieldConstructor :: Arbitrary => {
370
- quote ! { <#ty as arbitrary :: Arbitrary > :: try_size_hint ( depth ) }
370
+ quote ! { context . get :: <#ty> ( ) }
371
371
}
372
372
373
373
// Note that in this case it's hard to determine what size_hint must be, so size_of::<T>() is
374
374
// just an educated guess, although it's gonna be inaccurate for dynamically
375
375
// allocated types (Vec, HashMap, etc.).
376
376
FieldConstructor :: With ( _) => {
377
- quote ! { Ok ( ( :: core:: mem:: size_of:: <#ty>( ) , None ) ) }
377
+ quote ! { :: arbitrary :: SizeHint :: at_least ( :: core:: mem:: size_of:: <#ty>( ) ) }
378
378
}
379
379
}
380
380
} )
381
381
} )
382
382
. collect :: < Result < Vec < TokenStream > > > ( )
383
383
. map ( |hints| {
384
384
quote ! {
385
- Ok ( arbitrary:: size_hint :: and_all( & [
386
- #( #hints? ) , *
387
- ] ) )
385
+ :: arbitrary:: SizeHint :: and_all( & [
386
+ #( #hints ) , *
387
+ ] )
388
388
}
389
389
} )
390
390
} ;
391
391
let size_hint_structlike = |fields : & Fields | {
392
392
size_hint_fields ( fields) . map ( |hint| {
393
393
quote ! {
394
394
#[ inline]
395
- fn size_hint( depth: usize ) -> ( usize , :: core:: option:: Option <usize >) {
396
- Self :: try_size_hint( depth) . unwrap_or_default( )
397
- }
398
-
399
- #[ inline]
400
- fn try_size_hint( depth: usize ) -> :: core:: result:: Result <( usize , :: core:: option:: Option <usize >) , arbitrary:: MaxRecursionReached > {
401
- arbitrary:: size_hint:: try_recursion_guard( depth, |depth| #hint)
395
+ fn size_hint( context: & :: arbitrary:: size_hint:: Context ) -> :: arbitrary:: size_hint:: SizeHint {
396
+ #hint
402
397
}
403
398
}
404
399
} )
@@ -418,17 +413,12 @@ fn gen_size_hint_method(input: &DeriveInput) -> Result<TokenStream> {
418
413
. collect :: < Result < Vec < TokenStream > > > ( )
419
414
. map ( |variants| {
420
415
quote ! {
421
- fn size_hint( depth: usize ) -> ( usize , :: core:: option:: Option <usize >) {
422
- Self :: try_size_hint( depth) . unwrap_or_default( )
423
- }
424
416
#[ inline]
425
- fn try_size_hint( depth: usize ) -> :: core:: result:: Result <( usize , :: core:: option:: Option <usize >) , arbitrary:: MaxRecursionReached > {
426
- Ok ( arbitrary:: size_hint:: and(
427
- <u32 as arbitrary:: Arbitrary >:: try_size_hint( depth) ?,
428
- arbitrary:: size_hint:: try_recursion_guard( depth, |depth| {
429
- Ok ( arbitrary:: size_hint:: or_all( & [ #( #variants? ) , * ] ) )
430
- } ) ?,
431
- ) )
417
+ fn size_hint( context: & :: arbitrary:: size_hint:: Context ) -> :: arbitrary:: size_hint:: SizeHint {
418
+ :: arbitrary:: SizeHint :: and(
419
+ <u32 as :: arbitrary:: Arbitrary >:: size_hint( context) ,
420
+ :: arbitrary:: SizeHint :: or_all( & [ #( #variants ) , * ] )
421
+ )
432
422
}
433
423
}
434
424
} ) ,
@@ -438,7 +428,7 @@ fn gen_size_hint_method(input: &DeriveInput) -> Result<TokenStream> {
438
428
fn gen_constructor_for_field ( field : & Field ) -> Result < TokenStream > {
439
429
let ctor = match determine_field_constructor ( field) ? {
440
430
FieldConstructor :: Default => quote ! ( :: core:: default :: Default :: default ( ) ) ,
441
- FieldConstructor :: Arbitrary => quote ! ( arbitrary:: Arbitrary :: arbitrary( u) ?) ,
431
+ FieldConstructor :: Arbitrary => quote ! ( :: arbitrary:: Arbitrary :: arbitrary( u) ?) ,
442
432
FieldConstructor :: With ( function_or_closure) => quote ! ( ( #function_or_closure) ( u) ?) ,
443
433
FieldConstructor :: Value ( value) => quote ! ( #value) ,
444
434
} ;
0 commit comments