pub struct Modifier { /* private fields */ }Expand description
A validated operator and value applied to a roll’s total.
Create one with Modifier::try_new.
Implementations§
Source§impl Modifier
impl Modifier
Sourcepub fn try_new(operator: Operator, value: u32) -> Result<Self, NotationError>
pub fn try_new(operator: Operator, value: u32) -> Result<Self, NotationError>
Creates a modifier, checking the value is usable with the operator.
§Errors
Returns NotationError::Zero if the operator is Operator::Div
and the value is zero, or NotationError::NumberTooLarge if the
value exceeds MAX_VALUE.
§Examples
use rust_roll::notation::{Modifier, Operator};
let modifier = Modifier::try_new(Operator::Add, 3).unwrap();
assert_eq!(modifier.apply(10), 13);
assert!(Modifier::try_new(Operator::Div, 0).is_err());Sourcepub fn apply(self, total: u32) -> u32
pub fn apply(self, total: u32) -> u32
Applies the modifier to a total.
The result never goes below zero or above u32::MAX: subtraction
stops at zero and addition and multiplication saturate. Division rounds
down, following the D&D 5e (2014) rules.
§Examples
use rust_roll::notation::{Modifier, Operator};
let halve = Modifier::try_new(Operator::Div, 2).unwrap();
assert_eq!(halve.apply(7), 3);
let minus_five = Modifier::try_new(Operator::Sub, 5).unwrap();
assert_eq!(minus_five.apply(3), 0);Trait Implementations§
impl Copy for Modifier
impl Eq for Modifier
impl StructuralPartialEq for Modifier
Auto Trait Implementations§
impl Freeze for Modifier
impl RefUnwindSafe for Modifier
impl Send for Modifier
impl Sync for Modifier
impl Unpin for Modifier
impl UnsafeUnpin for Modifier
impl UnwindSafe for Modifier
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more