1
0
Fork 0
mirror of https://github.com/alacritty/alacritty.git synced 2024-11-11 13:51:01 -05:00

Refactor limit function

Was reading through the code and realized this function could be cleaned
up significantly.
This commit is contained in:
Joe Wilm 2016-11-25 12:03:11 -08:00
parent adf02b5049
commit 941818d88e
2 changed files with 15 additions and 8 deletions

View file

@ -20,6 +20,7 @@
#![feature(unicode)] #![feature(unicode)]
#![feature(step_trait)] #![feature(step_trait)]
#![feature(core_intrinsics)] #![feature(core_intrinsics)]
#![feature(test)]
#![allow(stable_features)] // lying about question_mark because 1.14.0 isn't released! #![allow(stable_features)] // lying about question_mark because 1.14.0 isn't released!
#![feature(proc_macro)] #![feature(proc_macro)]

View file

@ -16,6 +16,7 @@
use std::mem; use std::mem;
use std::ops::{Deref, Range}; use std::ops::{Deref, Range};
use std::ptr; use std::ptr;
use std::cmp;
use ansi::{self, Attr, Handler}; use ansi::{self, Attr, Handler};
use grid::{Grid, ClearRegion}; use grid::{Grid, ClearRegion};
@ -65,14 +66,9 @@ impl<'a> Deref for RenderGrid<'a> {
} }
/// coerce val to be between min and max /// coerce val to be between min and max
fn limit<T: PartialOrd>(val: T, min: T, max: T) -> T { #[inline]
if val < min { fn limit<T: PartialOrd + Ord>(val: T, min: T, max: T) -> T {
min cmp::min(cmp::max(min, val), max)
} else if val > max {
max
} else {
val
}
} }
pub mod cell { pub mod cell {
@ -878,6 +874,9 @@ impl ansi::Handler for Term {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
extern crate serde_json; extern crate serde_json;
extern crate test;
use super::limit;
use ansi::Color; use ansi::Color;
use grid::Grid; use grid::Grid;
@ -903,4 +902,11 @@ mod tests {
assert_eq!(deserialized, grid); 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);
}
} }