Skip to content

Instantly share code, notes, and snippets.

@kriogenia
Last active July 29, 2022 11:02
Show Gist options
  • Save kriogenia/be66a8ccb94ac2630a128b55c78ed875 to your computer and use it in GitHub Desktop.
Save kriogenia/be66a8ccb94ac2630a128b55c78ed875 to your computer and use it in GitHub Desktop.
&Counter implementing Add<i32> and returning Result<i32, String>
impl Add<i32> for &Counter {
type Output = Result<i32, String>;
fn add(self, rhs: i32) -> Self::Output {
let sum = self.0 + rhs;
if sum > MAX_COUNTER {
Err("Counter overflow".to_string())
} else {
Ok(i32::try_from(sum).unwrap())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment