-
Notifications
You must be signed in to change notification settings - Fork 182
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
Decimal::try_new
is const fn
, but not usefully so in certain cases
#527
Comments
Where the use rust_decimal::Decimal;
const N: rust_decimal::Result<Decimal> = {
Decimal::try_new(i64::MAX, u32::MAX)
};
pub fn main() {
dbg!(N.unwrap_err());
} There are use rust_decimal::Decimal;
const _: () = {
// Nope. Destructor is not allowed
match Decimal::try_new(i64::MAX, u32::MAX) {
Ok(elem) => Ok(elem),
Err(err) => Err(err),
};
// Nope. No const unwrap Result
Decimal::try_new(i64::MAX, u32::MAX).unwrap_err();
// Nope. No const unwrap Option
Decimal::try_new(i64::MAX, u32::MAX).ok().unwrap();
};
pub fn main() {
} |
And yes, on current stable none of those work for larger reasons. But under #![feature(const_result_drop, const_option)]
enum ErrorWithString {
Str(String),
Num(i8)
}
enum ErrorWithoutString {
Num(i8)
}
const fn pos(num: i8) -> Result<u8, ErrorWithoutString> {
if num < 0 {
return Err(ErrorWithoutString::Num(num))
} else {
return Ok(num.abs_diff(0))
}
}
const fn pos2(num: i8) -> Result<u8, ErrorWithString> {
if num < 0 {
return Err(ErrorWithString::Num(num))
} else {
return Ok(num.abs_diff(0))
}
}
pub const A: u8 = pos(5).ok().unwrap();
pub const B: u8 = pos2(5).ok().unwrap(); My recommendation would be to keep The other potential solution would be to modify |
Oh, now I see what you meant. Beware of The general feeling within Rustc development is that more concrete constant machinery (CTFE) will take some years to flourish.
These are nice solutions and both have their own trade-offs. Not sure if Well, ultimately, it is all up to @paupino :) |
This all makes sense and is something we should be thinking towards. Honestly, ergonomic error handling was a bit of an afterthought when this library was first started in 2015 (i.e. it used to return I'll have to have a bit of think through the options here since there may be a bit of a trade off between future optimizations and backwards compatibility. I'm not a big fan of #528 namely because it exposes some of the internal library visibility however am open to start designing the error library to what it should look like - even if it is feature flagged for now. Since it is a nightly only feature right now - perhaps that's reasonable? |
I see. If this would require a semver break anyway, maybe it would be worthwhile to just restructure the Error type to completely remove the String cases, while also making sure that it's const-droppable? I'd be happy to submit a PR for that. As for #528 — I understand, and I also would very much prefer that |
Not sure if all variants can be converted but it is probably worth a try. If it is OK to @paupino , I can help you in this task. |
Sorry: a very delayed reply! Ultimately, I'd like to start development towards v2 with a focus towards how the library could become simpler/lighter/more efficient/ergonomic by default. If it means starting work towards major version changes then I'm certainly open to that. Depending on the size/complexity of the changes then either a branch or a feature flag could work... but that's really open to experimentation. A while back I started a Anyway, certainly open to any assistance one may be willing to offer! |
In regards to configurable precision and/or configurable size, https://gitlab.com/tspiteri/fixed is stating that a new release is blocked by |
Any update for this issue? I want to define a const Decimal like but this crate is https://docs.rs/decimal-rs/latest/decimal_rs/struct.Decimal.html#method.from_parts_unchecked |
If you just want to unwrap it immediately, maybe use the |
Decimal::try_new
is intended to be used in const contexts; but there's a glitch. The function declares itself as returningResult<Decimal, Error>
, whereError
is the general error type for the crate. However, some of those enums involveString
s, and any attempt to calltry_new
and match on it in a const context results in:(This precise error is due to me turning on a truckload of other const-related features, but the basic thrust of the error remains.)
This isn't actually a real issue, in that
try_new
doesn't actually construct any strings — the only error case it returns isScaleExceedsMaximumPrecision
which absolutely could be dropped in const contexts. The bugfix here would, however, require bumping semver, as at a minimum it would involve changingtry_new
's type.I'm willing to submit a PR for this, but there's at least two distinct ways to break semver here, both with good arguments for it, so I didn't want to do so without opening a discussion first.
The text was updated successfully, but these errors were encountered: