2016-08-19 06:40:32 -07:00
|
|
|
//! Script numeric.
|
|
|
|
|
|
|
|
/// Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
|
|
|
|
/// The semantics are subtle, though: operands must be in the range [-2^31 +1...2^31 -1],
|
|
|
|
/// but results may overflow (and are valid as long as they are not used in a subsequent
|
|
|
|
/// numeric operation). CScriptNum enforces those semantics by storing results as
|
|
|
|
/// an int64 and allowing out-of-range values to be returned as a vector of bytes but
|
|
|
|
/// throwing an exception if arithmetic is done or the result is interpreted as an integer.
|
|
|
|
#[derive(Debug, PartialEq, Clone, Copy)]
|
|
|
|
pub struct Num {
|
|
|
|
value: i64,
|
|
|
|
}
|
|
|
|
|
2016-08-23 06:07:14 -07:00
|
|
|
impl From<u8> for Num {
|
|
|
|
fn from(i: u8) -> Self {
|
|
|
|
Num {
|
|
|
|
value: i as i64
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-19 06:40:32 -07:00
|
|
|
impl From<i64> for Num {
|
|
|
|
fn from(i: i64) -> Self {
|
|
|
|
Num {
|
|
|
|
value: i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|