mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Use bespoke macro instead of ?
inside const fns
This commit is contained in:
parent
c981d67b50
commit
2afa99379d
@ -10,6 +10,16 @@ use crate::intrinsics;
|
|||||||
use crate::mem;
|
use crate::mem;
|
||||||
use crate::str::FromStr;
|
use crate::str::FromStr;
|
||||||
|
|
||||||
|
// Used because the `?` operator is not allowed in a const context.
|
||||||
|
macro_rules! try_opt {
|
||||||
|
($e:expr) => {
|
||||||
|
match $e {
|
||||||
|
Some(x) => x,
|
||||||
|
None => return None,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
macro_rules! impl_nonzero_fmt {
|
macro_rules! impl_nonzero_fmt {
|
||||||
( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
|
( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
|
||||||
$(
|
$(
|
||||||
@ -1000,17 +1010,17 @@ $EndFeature, "
|
|||||||
|
|
||||||
while exp > 1 {
|
while exp > 1 {
|
||||||
if (exp & 1) == 1 {
|
if (exp & 1) == 1 {
|
||||||
acc = acc.checked_mul(base)?;
|
acc = try_opt!(acc.checked_mul(base));
|
||||||
}
|
}
|
||||||
exp /= 2;
|
exp /= 2;
|
||||||
base = base.checked_mul(base)?;
|
base = try_opt!(base.checked_mul(base));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deal with the final bit of the exponent separately, since
|
// Deal with the final bit of the exponent separately, since
|
||||||
// squaring the base afterwards is not necessary and may cause a
|
// squaring the base afterwards is not necessary and may cause a
|
||||||
// needless overflow.
|
// needless overflow.
|
||||||
if exp == 1 {
|
if exp == 1 {
|
||||||
acc = acc.checked_mul(base)?;
|
acc = try_opt!(acc.checked_mul(base));
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(acc)
|
Some(acc)
|
||||||
@ -3126,17 +3136,17 @@ assert_eq!(", stringify!($SelfT), "::max_value().checked_pow(2), None);", $EndFe
|
|||||||
|
|
||||||
while exp > 1 {
|
while exp > 1 {
|
||||||
if (exp & 1) == 1 {
|
if (exp & 1) == 1 {
|
||||||
acc = acc.checked_mul(base)?;
|
acc = try_opt!(acc.checked_mul(base));
|
||||||
}
|
}
|
||||||
exp /= 2;
|
exp /= 2;
|
||||||
base = base.checked_mul(base)?;
|
base = try_opt!(base.checked_mul(base));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deal with the final bit of the exponent separately, since
|
// Deal with the final bit of the exponent separately, since
|
||||||
// squaring the base afterwards is not necessary and may cause a
|
// squaring the base afterwards is not necessary and may cause a
|
||||||
// needless overflow.
|
// needless overflow.
|
||||||
if exp == 1 {
|
if exp == 1 {
|
||||||
acc = acc.checked_mul(base)?;
|
acc = try_opt!(acc.checked_mul(base));
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(acc)
|
Some(acc)
|
||||||
|
Loading…
Reference in New Issue
Block a user