Make use of `unlikely` intrinsic
There's some bounds checks we do that panic if the condition is ever true.
This commit is contained in:
parent
b6f7b39c0d
commit
7dd176fea8
20
src/grid.rs
20
src/grid.rs
|
@ -145,17 +145,19 @@ impl<T> Grid<T> {
|
||||||
/// better error messages by doing the bounds checking ourselves.
|
/// better error messages by doing the bounds checking ourselves.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn swap_lines(&mut self, src: index::Line, dst: index::Line) {
|
pub fn swap_lines(&mut self, src: index::Line, dst: index::Line) {
|
||||||
// check that src/dst are in bounds. Since index::Line newtypes usize,
|
use std::intrinsics::unlikely;
|
||||||
// we can assume values are positive.
|
|
||||||
if src >= self.lines {
|
|
||||||
panic!("swap_lines src out of bounds; len={}, src={}", self.raw.len(), src);
|
|
||||||
}
|
|
||||||
|
|
||||||
if dst >= self.lines {
|
|
||||||
panic!("swap_lines dst out of bounds; len={}, dst={}", self.raw.len(), dst);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
|
// check that src/dst are in bounds. Since index::Line newtypes usize,
|
||||||
|
// we can assume values are positive.
|
||||||
|
if unlikely(src >= self.lines) {
|
||||||
|
panic!("swap_lines src out of bounds; len={}, src={}", self.raw.len(), src);
|
||||||
|
}
|
||||||
|
|
||||||
|
if unlikely(dst >= self.lines) {
|
||||||
|
panic!("swap_lines dst out of bounds; len={}, dst={}", self.raw.len(), dst);
|
||||||
|
}
|
||||||
|
|
||||||
let src: *mut _ = self.raw.get_unchecked_mut(src.0);
|
let src: *mut _ = self.raw.get_unchecked_mut(src.0);
|
||||||
let dst: *mut _ = self.raw.get_unchecked_mut(dst.0);
|
let dst: *mut _ = self.raw.get_unchecked_mut(dst.0);
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
#![feature(step_trait)]
|
#![feature(step_trait)]
|
||||||
#![feature(custom_derive, plugin)]
|
#![feature(custom_derive, plugin)]
|
||||||
#![plugin(serde_macros)]
|
#![plugin(serde_macros)]
|
||||||
|
#![feature(core_intrinsics)]
|
||||||
|
|
||||||
extern crate cgmath;
|
extern crate cgmath;
|
||||||
extern crate errno;
|
extern crate errno;
|
||||||
|
|
|
@ -467,8 +467,10 @@ impl ansi::Handler for Term {
|
||||||
self.cursor.col = Column(0);
|
self.cursor.col = Column(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.cursor.line == self.grid.num_lines() {
|
unsafe {
|
||||||
panic!("cursor fell off grid");
|
if ::std::intrinsics::unlikely(self.cursor.line == self.grid.num_lines()) {
|
||||||
|
panic!("cursor fell off grid");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let cell = &mut self.grid[&self.cursor];
|
let cell = &mut self.grid[&self.cursor];
|
||||||
|
|
Loading…
Reference in New Issue