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

Exhaust sequences up to a length? #50

Open
kpreid opened this issue Sep 26, 2024 · 0 comments
Open

Exhaust sequences up to a length? #50

kpreid opened this issue Sep 26, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@kpreid
Copy link
Owner

kpreid commented Sep 26, 2024

It might be useful to have a utility type which allows producing collections up to a certain length. Perhaps:

/// * N = max size
/// * C = collection type
/// * T = element type
struct UpTo<const N: usize, C, T> { ... }

impl<const N: usize, C, T> UpTo<N, C, T> {
    fn into_inner(self) -> C { ... }
}

impl<const N: usize, C, T> Exhaust for UpTo<N, C, T>
where
    T: Exhaust,
    C: FromIterator<T>,
{ ... }

This is not a very elegant interface, but the important part of deciding whether to provide it is how difficult the algorithm to actually produce the items turns out to be — how much work we save the user. (Perhaps we should expose the algorithm and not a type that uses it?) For small cases, a user could just

#[derive(Exhaust)]
enum UpTo3<T> {
    Zero,
    One([T; 1]),
    Two([T; 2]),
    Three([T; 3]),
}

but that can't be generic over length, and is sorted by length instead of lexicographically. Another derive-based approach, producing lexicographic ordering, would be

#[derive(Exhaust)]
enum Another<T, C> {
    No(C),
    Yes(T, C),
}

type UpTo3<T> = Another<T, Another<T, Another<T, ()>>>;

but again, it isn't const generic and would give the compiler a lot of work to do.

@kpreid kpreid added the enhancement New feature or request label Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant