-
Notifications
You must be signed in to change notification settings - Fork 23
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
Consider making ::new
to be const fn
#35
Comments
@greyblake is there a way to build nutype/nutype_macros/src/common/gen/new_unchecked.rs Lines 14 to 19 in e0daf53
|
@schneiderfelipe hi!
One of the challenges with It's practically impossible to say upfront if a generated implementation will be const-compliant or not. In order to avoid situation when nutype generates code with |
I understand. +1 for having users control whether What about |
I am not sure why you're asking about
It has nothing to do with So if we take a very simplified code similar to what struct Email(String);
impl Email {
const fn new(email: &str) -> Email {
Email(email.to_string())
}
} This won't compile:
@schneiderfelipe I am not sure what you're doing, but I guess you can just use either So going forward with the Email example, the following code works as expected. use nutype::nutype;
use once_cell::sync::Lazy;
#[nutype(
sanitize(trim, lowercase),
validate(not_empty, len_char_max = 50),
derive(Debug, PartialEq, Clone, Display),
)]
struct Email(String);
static DEFAULT_EMAIL: Lazy<Email> =
Lazy::new(|| Email::new("[email protected]").unwrap());
fn main() {
println!("Default email is {}", *DEFAULT_EMAIL);
}
|
Sorry, I may have mixed up things and I should have mentioned my use case from the start. I would like to fill up an associated impl MyType {
const my_value: MyType = MyType::new_([1, 2, 3]);
// ...
} I'm 100% sure those initializations won't break invariants. Since |
For your example to compile |
I have the same use case as @schneiderfelipe and I think Having associated constants can be very useful. I would love to migrate my newtypes (with existing constants) to nutype but not being able to migrate the constants turns out to be a showstopper. |
@helgoboss It's not that simple as it may seems.
|
But I see a demand for that. A solution could be at least partial support of |
Thanks for considering it. BTW, at this occasion, I really love the idea of nutype and how it's implemented. It makes writing robust and coherent newtypes much easier.
I'm aware that I appreciate that you want to make it hard to construct invalid types. On the other hand ... having some well-defined popular constants (with the author of the crate "promising" that they are correct) can actually contribute to safety for the consumers of this crate because constants can be used basically everywhere and are very convenient to use.
Declaring the function as I think adding a helgoboss@e9f2538#diff-a50c55e38c78545fe73f1b3fb129600fd97bb29ce5e2afd4b95f5940132bcd12R17 |
A potential alternative would be to add a dedicated syntax for adding associated constants and checking the validity of the wrapped values in the same way as the |
I had the following situation, different, but similar. I need to implement the
parse() is also not const fn. LazyLock doesn't help because you cannot assign consts from statics.
If I didn't miss something there seems to be a real need to have const fn for getting specific values that is missing. Perhaps const fn const_default() which is provided whenever default = ... is used? Or more generally the ability to define associated consts in #[nutype], perhaps like this:
|
@nalply Thanks for your comment. |
No description provided.
The text was updated successfully, but these errors were encountered: