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

Is a generic newtype possible? #130

Open
atezet opened this issue Mar 25, 2024 · 4 comments
Open

Is a generic newtype possible? #130

atezet opened this issue Mar 25, 2024 · 4 comments

Comments

@atezet
Copy link

atezet commented Mar 25, 2024

Is it possible to create a generic newtype with nutype? When I do, let's say:

use nutype::nutype;

#[nutype(validate(predicate = |v| !v.is_empty()))]
struct NonEmptyVec<T>(Vec<T>);

I get:

 --> src/main.rs:4:27
  |
4 | struct NonEmptyVec<T>(Vec<T>);
  |                           ^ not found in this scope
  |
help: you might be missing a type parameter
  |
4 | struct NonEmptyVec<T><T>(Vec<T>);
  |                   +++
@greyblake
Copy link
Owner

Hi @atezet

It's not possible at the moment.
But I have this in mind and want to make it possible in the future.

@atezet
Copy link
Author

atezet commented Mar 27, 2024

I already like using the library as-is a lot, but definitely looking forward to that! Thanks

@greyblake
Copy link
Owner

@atezet Btw, if you need only non empty vector, I recommend taking a look at nonempty: https://github.com/cloudhead/nonempty

@atezet
Copy link
Author

atezet commented Mar 30, 2024

Thanks for the suggestion! We tried that at first, and iirc it worked for the cases where we needed a vector, but we also need IndexMap and some others. Unfortunately nonempty only supports Vec, even though its name would suggest otherwise.

I also suggested just using nutype and fixing the type of the stored values, but my colleague went ahead and implemented a newtype wrapping a generic Iterator for now.

Would love to see this to be possible with nutype or nonempty.

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

No branches or pull requests

2 participants