diff --git a/src/backend/curses/mod.rs b/src/backend/curses/mod.rs index cdaebe5..f5b75ae 100644 --- a/src/backend/curses/mod.rs +++ b/src/backend/curses/mod.rs @@ -75,15 +75,15 @@ where } } -fn find_closest_pair(pair: &ColorPair, max_colors: i16) -> (i16, i16) { +fn find_closest_pair(pair: ColorPair, max_colors: i16) -> (i16, i16) { ( - find_closest(&pair.front, max_colors), - find_closest(&pair.back, max_colors), + find_closest(pair.front, max_colors), + find_closest(pair.back, max_colors), ) } -fn find_closest(color: &Color, max_colors: i16) -> i16 { - match *color { +fn find_closest(color: Color, max_colors: i16) -> i16 { + match color { Color::TerminalDefault => -1, Color::Dark(BaseColor::Black) => 0, Color::Dark(BaseColor::Red) => 1, diff --git a/src/backend/curses/n.rs b/src/backend/curses/n.rs index 2f8332e..6194d5a 100644 --- a/src/backend/curses/n.rs +++ b/src/backend/curses/n.rs @@ -153,7 +153,7 @@ impl InputParser { } } -fn find_closest_pair(pair: &ColorPair) -> (i16, i16) { +fn find_closest_pair(pair: ColorPair) -> (i16, i16) { super::find_closest_pair(pair, ncurses::COLORS() as i16) } @@ -243,7 +243,7 @@ impl Backend { let mut pairs = self.pairs.borrow_mut(); // Find if we have this color in stock - let (front, back) = find_closest_pair(&pair); + let (front, back) = find_closest_pair(pair); if pairs.contains_key(&(front, back)) { // We got it! pairs[&(front, back)] diff --git a/src/backend/curses/pan.rs b/src/backend/curses/pan.rs index 4ce9c0e..7565d6f 100644 --- a/src/backend/curses/pan.rs +++ b/src/backend/curses/pan.rs @@ -272,7 +272,7 @@ impl InputParser { } } -fn find_closest_pair(pair: &ColorPair) -> (i16, i16) { +fn find_closest_pair(pair: ColorPair) -> (i16, i16) { super::find_closest_pair(pair, pancurses::COLORS() as i16) } @@ -334,7 +334,7 @@ impl Backend { /// Checks the pair in the cache, or re-define a color if needed. fn get_or_create(&self, pair: ColorPair) -> i32 { let mut pairs = self.pairs.borrow_mut(); - let pair = find_closest_pair(&pair); + let pair = find_closest_pair(pair); // Find if we have this color in stock if pairs.contains_key(&pair) { diff --git a/src/direction.rs b/src/direction.rs index 5525ae3..1193aaf 100644 --- a/src/direction.rs +++ b/src/direction.rs @@ -37,13 +37,13 @@ impl Orientation { /// /// (`Horizontal` will return the x value, /// and `Vertical` will return the y value.) - pub fn get(&self, v: &XY) -> T { - v.get(*self).clone() + pub fn get(self, v: &XY) -> T { + v.get(self).clone() } /// Returns the other orientation. - pub fn swap(&self) -> Self { - match *self { + pub fn swap(self) -> Self { + match self { Orientation::Horizontal => Orientation::Vertical, Orientation::Vertical => Orientation::Horizontal, } @@ -51,8 +51,8 @@ impl Orientation { /// Returns a mutable reference to the component of the given vector /// corresponding to this orientation. - pub fn get_ref<'a, 'b, T>(&'a self, v: &'b mut XY) -> &'b mut T { - match *self { + pub fn get_ref(self, v: &mut XY) -> &mut T { + match self { Orientation::Horizontal => &mut v.x, Orientation::Vertical => &mut v.y, } @@ -63,8 +63,8 @@ impl Orientation { /// /// For an horizontal view, returns (Sum(x), Max(y)). /// For a vertical view, returns (Max(x),Sum(y)). - pub fn stack<'a, T: Iterator>(&self, iter: T) -> Vec2 { - match *self { + pub fn stack<'a, T: Iterator>(self, iter: T) -> Vec2 { + match self { Orientation::Horizontal => { iter.fold(Vec2::zero(), |a, b| a.stack_horizontal(b)) } @@ -75,7 +75,7 @@ impl Orientation { } /// Creates a new `Vec2` with `value` in `self`'s axis. - pub fn make_vec(&self, main_axis: usize, second_axis: usize) -> Vec2 { + pub fn make_vec(self, main_axis: usize, second_axis: usize) -> Vec2 { let mut result = Vec2::zero(); *self.get_ref(&mut result) = main_axis; *self.swap().get_ref(&mut result) = second_axis; diff --git a/src/event.rs b/src/event.rs index 90b326c..25ae4e7 100644 --- a/src/event.rs +++ b/src/event.rs @@ -260,8 +260,8 @@ impl MouseEvent { /// Returns the button used by this event, if any. /// /// Returns `None` if `self` is `WheelUp` or `WheelDown`. - pub fn button(&self) -> Option { - match *self { + pub fn button(self) -> Option { + match self { MouseEvent::Press(btn) | MouseEvent::Release(btn) | MouseEvent::Hold(btn) => Some(btn), diff --git a/src/printer.rs b/src/printer.rs index 7d05698..1c64b28 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -308,7 +308,7 @@ impl<'a, 'b> Printer<'a, 'b> { offset: self.offset, size: self.size, focused: self.focused, - theme: theme, + theme, backend: self.backend, output_size: self.output_size, content_offset: self.content_offset, diff --git a/src/theme/color_pair.rs b/src/theme/color_pair.rs index ea1ccf7..e531f83 100644 --- a/src/theme/color_pair.rs +++ b/src/theme/color_pair.rs @@ -14,7 +14,7 @@ impl ColorPair { /// Return an inverted color pair. /// /// With swapped front abd back color. - pub fn invert(&self) -> Self { + pub fn invert(self) -> Self { ColorPair { front: self.back, back: self.front, diff --git a/src/theme/palette.rs b/src/theme/palette.rs index 8657f9d..2192552 100644 --- a/src/theme/palette.rs +++ b/src/theme/palette.rs @@ -67,7 +67,7 @@ impl Palette { /// Returns `None` if the given key was not found. pub fn custom<'a>(&'a self, key: &str) -> Option<&'a Color> { self.custom.get(key).and_then(|node| { - if let &PaletteNode::Color(ref color) = node { + if let PaletteNode::Color(ref color) = *node { Some(color) } else { None diff --git a/src/views/dialog.rs b/src/views/dialog.rs index 4b6820c..d3ad79d 100644 --- a/src/views/dialog.rs +++ b/src/views/dialog.rs @@ -270,9 +270,7 @@ impl Dialog { } /// Returns an iterator on this buttons for this dialog. - pub fn buttons_mut<'a>( - &'a mut self, - ) -> impl Iterator { + pub fn buttons_mut(&mut self) -> impl Iterator { self.buttons.iter_mut().map(|b| &mut b.button.view) } diff --git a/src/views/edit_view.rs b/src/views/edit_view.rs index 3a46668..b53c28d 100644 --- a/src/views/edit_view.rs +++ b/src/views/edit_view.rs @@ -585,10 +585,12 @@ impl View for EditView { let selected = self.content[self.cursor..] .graphemes(true) .next() - .expect(&format!( - "Found no char at cursor {} in {}", - self.cursor, &self.content - )); + .unwrap_or_else(|| { + panic!( + "Found no char at cursor {} in {}", + self.cursor, &self.content + ) + }); if self.secret { make_small_stars(selected.width()) } else { diff --git a/src/views/scroll_view.rs b/src/views/scroll_view.rs index ef56564..f7ad73a 100644 --- a/src/views/scroll_view.rs +++ b/src/views/scroll_view.rs @@ -245,11 +245,12 @@ where let offsets = self.scrollbar_thumb_offsets(lengths); // Iterate on axises, and keep the one we grabbed. - for (orientation, pos, length, offset) in + if let Some((orientation, pos, length, offset)) = XY::zip4(Orientation::pair(), position, lengths, offsets) .keep(grabbed.and(self.enabled)) .into_iter() .filter_map(|x| x) + .next() { if pos >= offset && pos < offset + length { // We grabbed the thumb! Now scroll from that position. diff --git a/src/xy.rs b/src/xy.rs index f0e0989..ba70a53 100644 --- a/src/xy.rs +++ b/src/xy.rs @@ -10,6 +10,15 @@ pub struct XY { pub y: T, } +impl IntoIterator for XY { + type Item = T; + type IntoIter = iter::Chain, iter::Once>; + + fn into_iter(self) -> Self::IntoIter { + iter::once(self.x).chain(iter::once(self.y)) + } +} + impl XY { /// Creates a new `XY` from the given values. pub fn new(x: T, y: T) -> Self { @@ -88,11 +97,6 @@ impl XY { iter::once(&self.x).chain(iter::once(&self.y)) } - /// Creates an iterator that returns `x`, then `y`. - pub fn into_iter(self) -> iter::Chain, iter::Once> { - iter::once(self.x).chain(iter::once(self.y)) - } - /// Returns a reference to the value on the given axis. pub fn get(&self, o: Orientation) -> &T { match o { @@ -177,34 +181,34 @@ impl XY> { impl XY { /// Returns `true` if any of `x` or `y` is `true`. - pub fn any(&self) -> bool { + pub fn any(self) -> bool { use std::ops::BitOr; self.fold(BitOr::bitor) } /// Returns `true` if both `x` and `y` are `true`. - pub fn both(&self) -> bool { + pub fn both(self) -> bool { use std::ops::BitAnd; self.fold(BitAnd::bitand) } /// For each axis, keeps elements from `other` if `self` is `true`. - pub fn select(&self, other: XY) -> XY> { + pub fn select(self, other: XY) -> XY> { self.zip_map(other, |keep, o| if keep { Some(o) } else { None }) } /// For each axis, selects `if_true` if `self` is true, else `if_false`. - pub fn select_or(&self, if_true: XY, if_false: XY) -> XY { + pub fn select_or(self, if_true: XY, if_false: XY) -> XY { self.select(if_true).unwrap_or(if_false) } /// Returns a term-by-term AND operation. - pub fn and(&self, other: Self) -> Self { + pub fn and(self, other: Self) -> Self { self.zip_map(other, |s, o| s && o) } /// Returns a term-by-term OR operation. - pub fn or(&self, other: Self) -> Self { + pub fn or(self, other: Self) -> Self { self.zip_map(other, |s, o| s || o) } }