Last active
July 29, 2022 11:02
-
-
Save kriogenia/be66a8ccb94ac2630a128b55c78ed875 to your computer and use it in GitHub Desktop.
&Counter implementing Add<i32> and returning Result<i32, String>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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