Skip to content
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

Replace arbitrary_array macro with const generics (after stabilization) #69

Closed
frewsxcv opened this issue Jan 23, 2021 · 2 comments
Closed

Comments

@frewsxcv
Copy link
Member

arbitrary/src/lib.rs

Lines 716 to 797 in ab4adcf

macro_rules! arbitrary_array {
{$n:expr, ($t:ident, $a:ident) $(($ts:ident, $as:ident))*} => {
arbitrary_array!{($n - 1), $(($ts, $as))*}
impl<T: Arbitrary> Arbitrary for [T; $n] {
fn arbitrary(u: &mut Unstructured<'_>) -> Result<[T; $n]> {
Ok([
Arbitrary::arbitrary(u)?,
$(<$ts as Arbitrary>::arbitrary(u)?),*
])
}
#[allow(unused_mut)]
fn arbitrary_take_rest(mut u: Unstructured<'_>) -> Result<[T; $n]> {
$(let $as = $ts::arbitrary(&mut u)?;)*
let last = Arbitrary::arbitrary_take_rest(u)?;
Ok([
$($as,)* last
])
}
#[inline]
fn size_hint(depth: usize) -> (usize, Option<usize>) {
crate::size_hint::and_all(&[
<$t as Arbitrary>::size_hint(depth),
$( <$ts as Arbitrary>::size_hint(depth) ),*
])
}
#[allow(unused_mut)] // For the `[T; 1]` case.
fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
let mut i = 0;
let mut shrinkers = [
self[i].shrink(),
$({
i += 1;
let t: &$ts = &self[i];
t.shrink()
}),*
];
Box::new(iter::from_fn(move || {
let mut i = 0;
Some([
shrinkers[i].next()?,
$({
i += 1;
let t: $ts = shrinkers[i].next()?;
t
}),*
])
}))
}
}
};
($n: expr,) => {};
}
impl<T: Arbitrary> Arbitrary for [T; 0] {
fn arbitrary(_: &mut Unstructured<'_>) -> Result<[T; 0]> {
Ok([])
}
fn arbitrary_take_rest(_: Unstructured<'_>) -> Result<[T; 0]> {
Ok([])
}
#[inline]
fn size_hint(_: usize) -> (usize, Option<usize>) {
crate::size_hint::and_all(&[])
}
fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
Box::new(iter::from_fn(|| None))
}
}
arbitrary_array! { 32, (T, a) (T, b) (T, c) (T, d) (T, e) (T, f) (T, g) (T, h)
(T, i) (T, j) (T, k) (T, l) (T, m) (T, n) (T, o) (T, p)
(T, q) (T, r) (T, s) (T, u) (T, v) (T, w) (T, x) (T, y)
(T, z) (T, aa) (T, ab) (T, ac) (T, ad) (T, ae) (T, af)
(T, ag) }

@frewsxcv
Copy link
Member Author

An attempt was made in #55

@e00E
Copy link
Contributor

e00E commented May 17, 2021

Maybe this can be closed because the PR was merged. Although no release with this feature has been made.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants