Created
February 8, 2019 02:54
-
-
Save CoolOppo/89f4bb2dc60caa15f3f51dd218135105 to your computer and use it in GitHub Desktop.
Rust clamp function for most types
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
trait Clamp<T> { | |
fn clamp(self, min: T, max: T) -> T; | |
} | |
impl<T> Clamp<T> for T | |
where | |
T: PartialOrd + Copy, | |
{ | |
fn clamp(self, min: T, max: T) -> T { | |
if self > max { | |
max | |
} else if self < min { | |
min | |
} else { | |
self | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment