summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoe Wilm <joe@jwilm.com>2016-11-25 12:03:11 -0800
committerJoe Wilm <joe@jwilm.com>2016-12-11 20:23:41 -0800
commit941818d88ebc1f0d90ef9b1ef7d1313174afb36b (patch)
tree5a976cf53ddb5df30171271b5289476434d11b35
parentadf02b5049f644e56033834e948481f199e6bb6a (diff)
Refactor limit function
Was reading through the code and realized this function could be cleaned up significantly.
-rw-r--r--src/lib.rs1
-rw-r--r--src/term.rs22
2 files changed, 15 insertions, 8 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 8052ce07..aa9275c2 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -20,6 +20,7 @@
#![feature(unicode)]
#![feature(step_trait)]
#![feature(core_intrinsics)]
+#![feature(test)]
#![allow(stable_features)] // lying about question_mark because 1.14.0 isn't released!
#![feature(proc_macro)]
diff --git a/src/term.rs b/src/term.rs
index d2d711b4..430a68ea 100644
--- a/src/term.rs
+++ b/src/term.rs
@@ -16,6 +16,7 @@
use std::mem;
use std::ops::{Deref, Range};
use std::ptr;
+use std::cmp;
use ansi::{self, Attr, Handler};
use grid::{Grid, ClearRegion};
@@ -65,14 +66,9 @@ impl<'a> Deref for RenderGrid<'a> {
}
/// coerce val to be between min and max
-fn limit<T: PartialOrd>(val: T, min: T, max: T) -> T {
- if val < min {
- min
- } else if val > max {
- max
- } else {
- val
- }
+#[inline]
+fn limit<T: PartialOrd + Ord>(val: T, min: T, max: T) -> T {
+ cmp::min(cmp::max(min, val), max)
}
pub mod cell {
@@ -878,6 +874,9 @@ impl ansi::Handler for Term {
#[cfg(test)]
mod tests {
extern crate serde_json;
+ extern crate test;
+
+ use super::limit;
use ansi::Color;
use grid::Grid;
@@ -903,4 +902,11 @@ mod tests {
assert_eq!(deserialized, grid);
}
+
+ #[test]
+ fn limit_works() {
+ assert_eq!(limit(5, 1, 10), 5);
+ assert_eq!(limit(5, 6, 10), 6);
+ assert_eq!(limit(5, 1, 4), 4);
+ }
}