Add from_raw constructors to NoteValue and ValueSum

These might be replaced later with APIs that can provide more useful
bounds checks, but we do need some way to construct these types.
This commit is contained in:
Jack Grigg 2021-04-27 12:27:23 +12:00
parent 52d87e257c
commit a60051c8a2
1 changed files with 18 additions and 0 deletions

View File

@ -52,6 +52,14 @@ impl NoteValue {
Default::default()
}
/// Creates a note value from its raw numeric value.
///
/// This only enforces that the value is an unsigned 64-bit integer. Callers should
/// enforce any additional constraints on the value's valid range themselves.
pub fn from_raw(value: u64) -> Self {
NoteValue(value)
}
pub(crate) fn to_le_bits(self) -> BitArray<Lsb0, [u8; 8]> {
BitArray::<Lsb0, _>::new(self.0.to_le_bytes())
}
@ -71,6 +79,16 @@ impl Sub for NoteValue {
#[derive(Clone, Copy, Debug, Default)]
pub struct ValueSum(i64);
impl ValueSum {
/// Creates a value sum from its raw numeric value.
///
/// This only enforces that the value is a signed 63-bit integer. Callers should
/// enforce any additional constraints on the value's valid range themselves.
pub fn from_raw(value: i64) -> Self {
ValueSum(value)
}
}
impl Add for ValueSum {
type Output = Result<ValueSum, OverflowError>;