Skip to content

Replace (usize, Option<usize>) with custom type SizeHint and eliminate try_size_hint(). #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 24 additions & 34 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn expand_derive_arbitrary(input: syn::DeriveInput) -> Result<TokenStream> {
}

#[automatically_derived]
impl #impl_generics arbitrary::Arbitrary<#lifetime_without_bounds> for #name #ty_generics #where_clause {
impl #impl_generics ::arbitrary::Arbitrary<#lifetime_without_bounds> for #name #ty_generics #where_clause {
#arbitrary_method
#size_hint_method
}
Expand Down Expand Up @@ -141,7 +141,7 @@ fn add_trait_bounds(mut generics: Generics, lifetime: LifetimeParam) -> Generics
if let GenericParam::Type(type_param) = param {
type_param
.bounds
.push(parse_quote!(arbitrary::Arbitrary<#lifetime>));
.push(parse_quote!(::arbitrary::Arbitrary<#lifetime>));
}
}
generics
Expand All @@ -156,7 +156,7 @@ fn with_recursive_count_guard(
if guard_against_recursion {
#recursive_count.with(|count| {
if count.get() > 0 {
return Err(arbitrary::Error::NotEnoughData);
return Err(::arbitrary::Error::NotEnoughData);
}
count.set(count.get() + 1);
Ok(())
Expand Down Expand Up @@ -194,11 +194,11 @@ fn gen_arbitrary_method(
with_recursive_count_guard(recursive_count, quote! { Ok(#ident #arbitrary_take_rest) });

Ok(quote! {
fn arbitrary(u: &mut arbitrary::Unstructured<#lifetime>) -> arbitrary::Result<Self> {
fn arbitrary(u: &mut ::arbitrary::Unstructured<#lifetime>) -> ::arbitrary::Result<Self> {
#body
}

fn arbitrary_take_rest(mut u: arbitrary::Unstructured<#lifetime>) -> arbitrary::Result<Self> {
fn arbitrary_take_rest(mut u: ::arbitrary::Unstructured<#lifetime>) -> ::arbitrary::Result<Self> {
#take_rest_body
}
})
Expand All @@ -225,7 +225,7 @@ fn gen_arbitrary_method(
// Use a multiply + shift to generate a ranged random number
// with slight bias. For details, see:
// https://lemire.me/blog/2016/06/30/fast-random-shuffling
Ok(match (u64::from(<u32 as arbitrary::Arbitrary>::arbitrary(#unstructured)?) * #count) >> 32 {
Ok(match (u64::from(<u32 as ::arbitrary::Arbitrary>::arbitrary(#unstructured)?) * #count) >> 32 {
#(#variants,)*
_ => unreachable!()
})
Expand Down Expand Up @@ -279,11 +279,11 @@ fn gen_arbitrary_method(
let arbitrary_take_rest = arbitrary_enum_method(recursive_count, quote! { &mut u }, &variants_take_rest);

quote! {
fn arbitrary(u: &mut arbitrary::Unstructured<#lifetime>) -> arbitrary::Result<Self> {
fn arbitrary(u: &mut ::arbitrary::Unstructured<#lifetime>) -> ::arbitrary::Result<Self> {
#arbitrary
}

fn arbitrary_take_rest(mut u: arbitrary::Unstructured<#lifetime>) -> arbitrary::Result<Self> {
fn arbitrary_take_rest(mut u: ::arbitrary::Unstructured<#lifetime>) -> ::arbitrary::Result<Self> {
#arbitrary_take_rest
}
}
Expand Down Expand Up @@ -344,9 +344,9 @@ fn construct_take_rest(fields: &Fields) -> Result<TokenStream> {
FieldConstructor::Default => quote!(::core::default::Default::default()),
FieldConstructor::Arbitrary => {
if idx + 1 == fields.len() {
quote! { arbitrary::Arbitrary::arbitrary_take_rest(u)? }
quote! { ::arbitrary::Arbitrary::arbitrary_take_rest(u)? }
} else {
quote! { arbitrary::Arbitrary::arbitrary(&mut u)? }
quote! { ::arbitrary::Arbitrary::arbitrary(&mut u)? }
}
}
FieldConstructor::With(function_or_closure) => quote!((#function_or_closure)(&mut u)?),
Expand All @@ -364,41 +364,36 @@ fn gen_size_hint_method(input: &DeriveInput) -> Result<TokenStream> {
determine_field_constructor(f).map(|field_constructor| {
match field_constructor {
FieldConstructor::Default | FieldConstructor::Value(_) => {
quote!(Ok((0, Some(0))))
quote!(::arbitrary::SizeHint::exactly(0))
}
FieldConstructor::Arbitrary => {
quote! { <#ty as arbitrary::Arbitrary>::try_size_hint(depth) }
quote! { context.get::<#ty>() }
}

// Note that in this case it's hard to determine what size_hint must be, so size_of::<T>() is
// just an educated guess, although it's gonna be inaccurate for dynamically
// allocated types (Vec, HashMap, etc.).
FieldConstructor::With(_) => {
quote! { Ok((::core::mem::size_of::<#ty>(), None)) }
quote! { ::arbitrary::SizeHint::at_least(::core::mem::size_of::<#ty>()) }
}
}
})
})
.collect::<Result<Vec<TokenStream>>>()
.map(|hints| {
quote! {
Ok(arbitrary::size_hint::and_all(&[
#( #hints? ),*
]))
::arbitrary::SizeHint::and_all(&[
#( #hints ),*
])
}
})
};
let size_hint_structlike = |fields: &Fields| {
size_hint_fields(fields).map(|hint| {
quote! {
#[inline]
fn size_hint(depth: usize) -> (usize, ::core::option::Option<usize>) {
Self::try_size_hint(depth).unwrap_or_default()
}

#[inline]
fn try_size_hint(depth: usize) -> ::core::result::Result<(usize, ::core::option::Option<usize>), arbitrary::MaxRecursionReached> {
arbitrary::size_hint::try_recursion_guard(depth, |depth| #hint)
fn size_hint(context: &::arbitrary::size_hint::Context) -> ::arbitrary::size_hint::SizeHint {
#hint
}
}
})
Expand All @@ -418,17 +413,12 @@ fn gen_size_hint_method(input: &DeriveInput) -> Result<TokenStream> {
.collect::<Result<Vec<TokenStream>>>()
.map(|variants| {
quote! {
fn size_hint(depth: usize) -> (usize, ::core::option::Option<usize>) {
Self::try_size_hint(depth).unwrap_or_default()
}
#[inline]
fn try_size_hint(depth: usize) -> ::core::result::Result<(usize, ::core::option::Option<usize>), arbitrary::MaxRecursionReached> {
Ok(arbitrary::size_hint::and(
<u32 as arbitrary::Arbitrary>::try_size_hint(depth)?,
arbitrary::size_hint::try_recursion_guard(depth, |depth| {
Ok(arbitrary::size_hint::or_all(&[ #( #variants? ),* ]))
})?,
))
fn size_hint(context: &::arbitrary::size_hint::Context) -> ::arbitrary::size_hint::SizeHint {
::arbitrary::SizeHint::and(
<u32 as ::arbitrary::Arbitrary>::size_hint(context),
::arbitrary::SizeHint::or_all(&[ #( #variants ),* ])
)
}
}
}),
Expand All @@ -438,7 +428,7 @@ fn gen_size_hint_method(input: &DeriveInput) -> Result<TokenStream> {
fn gen_constructor_for_field(field: &Field) -> Result<TokenStream> {
let ctor = match determine_field_constructor(field)? {
FieldConstructor::Default => quote!(::core::default::Default::default()),
FieldConstructor::Arbitrary => quote!(arbitrary::Arbitrary::arbitrary(u)?),
FieldConstructor::Arbitrary => quote!(::arbitrary::Arbitrary::arbitrary(u)?),
FieldConstructor::With(function_or_closure) => quote!((#function_or_closure)(u)?),
FieldConstructor::Value(value) => quote!(#value),
};
Expand Down
11 changes: 2 additions & 9 deletions src/foreign/alloc/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ where
}

#[inline]
fn size_hint(depth: usize) -> (usize, Option<usize>) {
Self::try_size_hint(depth).unwrap_or_default()
}

#[inline]
fn try_size_hint(depth: usize) -> Result<(usize, Option<usize>), crate::MaxRecursionReached> {
size_hint::try_recursion_guard(depth, |depth| {
<<A as ToOwned>::Owned as Arbitrary>::try_size_hint(depth)
})
fn size_hint(context: &size_hint::Context) -> size_hint::SizeHint {
<<A as ToOwned>::Owned as Arbitrary>::size_hint(context)
}
}
18 changes: 7 additions & 11 deletions src/foreign/alloc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ where
}

#[inline]
fn size_hint(depth: usize) -> (usize, Option<usize>) {
Self::try_size_hint(depth).unwrap_or_default()
}

#[inline]
fn try_size_hint(depth: usize) -> Result<(usize, Option<usize>), crate::MaxRecursionReached> {
size_hint::try_recursion_guard(depth, <A as Arbitrary>::try_size_hint)
fn size_hint(context: &size_hint::Context) -> size_hint::SizeHint {
context.get::<A>()
}
}

Expand All @@ -35,8 +30,8 @@ where
}

#[inline]
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(0, None)
fn size_hint(_context: &size_hint::Context) -> size_hint::SizeHint {
size_hint::SizeHint::at_least(0)
}
}

Expand All @@ -46,7 +41,8 @@ impl<'a> Arbitrary<'a> for Box<str> {
}

#[inline]
fn size_hint(depth: usize) -> (usize, Option<usize>) {
<String as Arbitrary>::size_hint(depth)
fn size_hint(context: &size_hint::Context) -> size_hint::SizeHint {
// known non-recursive
<String as Arbitrary>::size_hint(context)
}
}
6 changes: 3 additions & 3 deletions src/foreign/alloc/collections/binary_heap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
crate::{Arbitrary, Result, Unstructured},
crate::{size_hint, Arbitrary, Result, Unstructured},
std::collections::binary_heap::BinaryHeap,
};

Expand All @@ -16,7 +16,7 @@ where
}

#[inline]
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(0, None)
fn size_hint(_context: &size_hint::Context) -> size_hint::SizeHint {
size_hint::SizeHint::at_least(0)
}
}
6 changes: 3 additions & 3 deletions src/foreign/alloc/collections/btree_map.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
crate::{Arbitrary, Result, Unstructured},
crate::{size_hint, Arbitrary, Result, Unstructured},
std::collections::btree_map::BTreeMap,
};

Expand All @@ -17,7 +17,7 @@ where
}

#[inline]
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(0, None)
fn size_hint(_context: &size_hint::Context) -> size_hint::SizeHint {
size_hint::SizeHint::at_least(0)
}
}
6 changes: 3 additions & 3 deletions src/foreign/alloc/collections/btree_set.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
crate::{Arbitrary, Result, Unstructured},
crate::{size_hint, Arbitrary, Result, Unstructured},
std::collections::btree_set::BTreeSet,
};

Expand All @@ -16,7 +16,7 @@ where
}

#[inline]
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(0, None)
fn size_hint(_context: &size_hint::Context) -> size_hint::SizeHint {
size_hint::SizeHint::at_least(0)
}
}
6 changes: 3 additions & 3 deletions src/foreign/alloc/collections/linked_list.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
crate::{Arbitrary, Result, Unstructured},
crate::{size_hint, Arbitrary, Result, Unstructured},
std::collections::linked_list::LinkedList,
};

Expand All @@ -16,7 +16,7 @@ where
}

#[inline]
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(0, None)
fn size_hint(_context: &size_hint::Context) -> size_hint::SizeHint {
size_hint::SizeHint::at_least(0)
}
}
6 changes: 3 additions & 3 deletions src/foreign/alloc/collections/vec_deque.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
crate::{Arbitrary, Result, Unstructured},
crate::{size_hint, Arbitrary, Result, Unstructured},
std::collections::vec_deque::VecDeque,
};

Expand All @@ -16,7 +16,7 @@ where
}

#[inline]
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(0, None)
fn size_hint(_context: &size_hint::Context) -> size_hint::SizeHint {
size_hint::SizeHint::at_least(0)
}
}
7 changes: 4 additions & 3 deletions src/foreign/alloc/ffi/c_str.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
crate::{Arbitrary, Result, Unstructured},
crate::{size_hint, Arbitrary, Result, Unstructured},
std::ffi::CString,
};

Expand All @@ -13,7 +13,8 @@ impl<'a> Arbitrary<'a> for CString {
}

#[inline]
fn size_hint(depth: usize) -> (usize, Option<usize>) {
<Vec<u8> as Arbitrary>::size_hint(depth)
fn size_hint(context: &size_hint::Context) -> size_hint::SizeHint {
// known non-recursive
<Vec<u8> as Arbitrary>::size_hint(context)
}
}
17 changes: 6 additions & 11 deletions src/foreign/alloc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ where
}

#[inline]
fn size_hint(depth: usize) -> (usize, Option<usize>) {
Self::try_size_hint(depth).unwrap_or_default()
}

#[inline]
fn try_size_hint(depth: usize) -> Result<(usize, Option<usize>), crate::MaxRecursionReached> {
size_hint::try_recursion_guard(depth, <A as Arbitrary>::try_size_hint)
fn size_hint(context: &size_hint::Context) -> size_hint::SizeHint {
context.get::<A>()
}
}

Expand All @@ -35,8 +30,8 @@ where
}

#[inline]
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(0, None)
fn size_hint(_context: &size_hint::Context) -> size_hint::SizeHint {
size_hint::SizeHint::at_least(0)
}
}

Expand All @@ -46,7 +41,7 @@ impl<'a> Arbitrary<'a> for Rc<str> {
}

#[inline]
fn size_hint(depth: usize) -> (usize, Option<usize>) {
<&str as Arbitrary>::size_hint(depth)
fn size_hint(context: &size_hint::Context) -> size_hint::SizeHint {
<&str as Arbitrary>::size_hint(context)
}
}
6 changes: 3 additions & 3 deletions src/foreign/alloc/string.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
crate::{Arbitrary, Result, Unstructured},
crate::{size_hint, Arbitrary, Result, Unstructured},
std::string::String,
};

Expand All @@ -13,7 +13,7 @@ impl<'a> Arbitrary<'a> for String {
}

#[inline]
fn size_hint(depth: usize) -> (usize, Option<usize>) {
<&str as Arbitrary>::size_hint(depth)
fn size_hint(context: &size_hint::Context) -> size_hint::SizeHint {
<&str as Arbitrary>::size_hint(context)
}
}
Loading