Merge rust-bitcoin/rust-bitcoin#4336: units: Make minor improvements to `MathOp`
0f62c9a582
units: Make minor improvements to MathOp (Tobin C. Harding) Pull request description: Follow up from #4312, improve the `MathOp` type by: - Do not provide public constructor - Add cast protection ACKs for top commit: apoelstra: ACK 0f62c9a5822a813be5c6f6b19d160458243f174a; successfully ran local tests Kixunil: ACK0f62c9a582
Tree-SHA512: dbb06c5afd8df5364a2aec12b7c8632620a1e8f3955b83e91a9b4f5c2e0daaa1ecdb050d8e395e95bf018d718847cddbf3338bd89f70cbb0382bf5e080d5cf21
This commit is contained in:
commit
8ca3a430c7
|
@ -3,6 +3,7 @@
|
|||
//! Provides a monodic type returned by mathematical operations (`core::ops`).
|
||||
|
||||
use core::fmt;
|
||||
use core::convert::Infallible;
|
||||
|
||||
use NumOpResult as R;
|
||||
|
||||
|
@ -202,7 +203,7 @@ pub struct NumOpError(MathOp);
|
|||
|
||||
impl NumOpError {
|
||||
/// Creates a [`NumOpError`] caused by `op`.
|
||||
pub fn while_doing(op: MathOp) -> Self { NumOpError(op) }
|
||||
pub(crate) fn while_doing(op: MathOp) -> Self { NumOpError(op) }
|
||||
|
||||
/// Returns `true` if this operation error'ed due to overflow.
|
||||
pub fn is_overflow(self) -> bool { self.0.is_overflow() }
|
||||
|
@ -239,6 +240,10 @@ pub enum MathOp {
|
|||
Rem,
|
||||
/// Negation failed ([`core::ops::Neg`] resulted in an invalid value).
|
||||
Neg,
|
||||
/// Stops users from casting this enum to an integer.
|
||||
// May get removed if one day Rust supports disabling casts natively.
|
||||
#[doc(hidden)]
|
||||
_DoNotUse(Infallible),
|
||||
}
|
||||
|
||||
impl MathOp {
|
||||
|
@ -253,13 +258,14 @@ impl MathOp {
|
|||
|
||||
impl fmt::Display for MathOp {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
match *self {
|
||||
MathOp::Add => write!(f, "add"),
|
||||
MathOp::Sub => write!(f, "sub"),
|
||||
MathOp::Mul => write!(f, "mul"),
|
||||
MathOp::Div => write!(f, "div"),
|
||||
MathOp::Rem => write!(f, "rem"),
|
||||
MathOp::Neg => write!(f, "neg"),
|
||||
MathOp::_DoNotUse(infallible) => match infallible {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue