add expected! macro to checked math expression suite
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
2709995f84
commit
3d4b0f10a5
3 changed files with 29 additions and 11 deletions
|
@ -7,32 +7,50 @@ use crate::{debug::type_name, err, Err, Error, Result};
|
||||||
/// Checked arithmetic expression. Returns a Result<R, Error::Arithmetic>
|
/// Checked arithmetic expression. Returns a Result<R, Error::Arithmetic>
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! checked {
|
macro_rules! checked {
|
||||||
($($input:tt)*) => {
|
($($input:tt)+) => {
|
||||||
$crate::utils::math::checked_ops!($($input)*)
|
$crate::utils::math::checked_ops!($($input)+)
|
||||||
.ok_or_else(|| $crate::err!(Arithmetic("operation overflowed or result invalid")))
|
.ok_or_else(|| $crate::err!(Arithmetic("operation overflowed or result invalid")))
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// in release-mode. Use for performance when the expression is obviously safe.
|
/// Checked arithmetic expression which panics on failure. This is for
|
||||||
/// The check remains in debug-mode for regression analysis.
|
/// expressions which do not meet the threshold for validated! but the caller
|
||||||
|
/// has no realistic expectation for error and no interest in cluttering the
|
||||||
|
/// callsite with result handling from checked!.
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! expected {
|
||||||
|
($msg:literal, $($input:tt)+) => {
|
||||||
|
$crate::checked!($($input)+).expect($msg)
|
||||||
|
};
|
||||||
|
|
||||||
|
($($input:tt)+) => {
|
||||||
|
$crate::expected!("arithmetic expression expectation failure", $($input)+)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Unchecked arithmetic expression in release-mode. Use for performance when
|
||||||
|
/// the expression is obviously safe. The check remains in debug-mode for
|
||||||
|
/// regression analysis.
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(not(debug_assertions))]
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! validated {
|
macro_rules! validated {
|
||||||
($($input:tt)*) => {
|
($($input:tt)+) => {
|
||||||
//#[allow(clippy::arithmetic_side_effects)] {
|
//#[allow(clippy::arithmetic_side_effects)] {
|
||||||
//Some($($input)*)
|
//Some($($input)*)
|
||||||
// .ok_or_else(|| $crate::err!(Arithmetic("this error should never been seen")))
|
// .ok_or_else(|| $crate::err!(Arithmetic("this error should never been seen")))
|
||||||
//}
|
//}
|
||||||
|
|
||||||
//NOTE: remove me when stmt_expr_attributes is stable
|
//NOTE: remove me when stmt_expr_attributes is stable
|
||||||
$crate::checked!($($input)*)
|
$crate::expected!("validated arithmetic expression failed", $($input)+)
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checked arithmetic expression in debug-mode. Use for performance when
|
||||||
|
/// the expression is obviously safe. The check is elided in release-mode.
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! validated {
|
macro_rules! validated {
|
||||||
($($input:tt)*) => { $crate::checked!($($input)*) }
|
($($input:tt)+) => { $crate::expected!($($input)+) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns false if the exponential backoff has expired based on the inputs
|
/// Returns false if the exponential backoff has expired based on the inputs
|
||||||
|
|
|
@ -66,7 +66,7 @@ impl Service {
|
||||||
.enumerate()
|
.enumerate()
|
||||||
{
|
{
|
||||||
let bucket: usize = short.try_into()?;
|
let bucket: usize = short.try_into()?;
|
||||||
let bucket: usize = validated!(bucket % NUM_BUCKETS)?;
|
let bucket: usize = validated!(bucket % NUM_BUCKETS);
|
||||||
buckets[bucket].insert((short, starting_events[i]));
|
buckets[bucket].insert((short, starting_events[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1205,7 +1205,7 @@ impl Service {
|
||||||
let count = self.services.globals.next_count()?;
|
let count = self.services.globals.next_count()?;
|
||||||
let mut pdu_id = shortroomid.to_be_bytes().to_vec();
|
let mut pdu_id = shortroomid.to_be_bytes().to_vec();
|
||||||
pdu_id.extend_from_slice(&0_u64.to_be_bytes());
|
pdu_id.extend_from_slice(&0_u64.to_be_bytes());
|
||||||
pdu_id.extend_from_slice(&(validated!(max - count)?).to_be_bytes());
|
pdu_id.extend_from_slice(&(validated!(max - count)).to_be_bytes());
|
||||||
|
|
||||||
// Insert pdu
|
// Insert pdu
|
||||||
self.db.prepend_backfill_pdu(&pdu_id, &event_id, &value)?;
|
self.db.prepend_backfill_pdu(&pdu_id, &event_id, &value)?;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue