Fix clippy warnings

This commit is contained in:
Alexandre Bury 2018-06-21 14:44:30 -07:00
parent bd78bca91b
commit 6ce2dba8a1
12 changed files with 47 additions and 42 deletions

View File

@ -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.front, max_colors),
find_closest(&pair.back, max_colors), find_closest(pair.back, max_colors),
) )
} }
fn find_closest(color: &Color, max_colors: i16) -> i16 { fn find_closest(color: Color, max_colors: i16) -> i16 {
match *color { match color {
Color::TerminalDefault => -1, Color::TerminalDefault => -1,
Color::Dark(BaseColor::Black) => 0, Color::Dark(BaseColor::Black) => 0,
Color::Dark(BaseColor::Red) => 1, Color::Dark(BaseColor::Red) => 1,

View File

@ -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) super::find_closest_pair(pair, ncurses::COLORS() as i16)
} }
@ -243,7 +243,7 @@ impl Backend {
let mut pairs = self.pairs.borrow_mut(); let mut pairs = self.pairs.borrow_mut();
// Find if we have this color in stock // 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)) { if pairs.contains_key(&(front, back)) {
// We got it! // We got it!
pairs[&(front, back)] pairs[&(front, back)]

View File

@ -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) 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. /// Checks the pair in the cache, or re-define a color if needed.
fn get_or_create(&self, pair: ColorPair) -> i32 { fn get_or_create(&self, pair: ColorPair) -> i32 {
let mut pairs = self.pairs.borrow_mut(); 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 // Find if we have this color in stock
if pairs.contains_key(&pair) { if pairs.contains_key(&pair) {

View File

@ -37,13 +37,13 @@ impl Orientation {
/// ///
/// (`Horizontal` will return the x value, /// (`Horizontal` will return the x value,
/// and `Vertical` will return the y value.) /// and `Vertical` will return the y value.)
pub fn get<T: Clone>(&self, v: &XY<T>) -> T { pub fn get<T: Clone>(self, v: &XY<T>) -> T {
v.get(*self).clone() v.get(self).clone()
} }
/// Returns the other orientation. /// Returns the other orientation.
pub fn swap(&self) -> Self { pub fn swap(self) -> Self {
match *self { match self {
Orientation::Horizontal => Orientation::Vertical, Orientation::Horizontal => Orientation::Vertical,
Orientation::Vertical => Orientation::Horizontal, Orientation::Vertical => Orientation::Horizontal,
} }
@ -51,8 +51,8 @@ impl Orientation {
/// Returns a mutable reference to the component of the given vector /// Returns a mutable reference to the component of the given vector
/// corresponding to this orientation. /// corresponding to this orientation.
pub fn get_ref<'a, 'b, T>(&'a self, v: &'b mut XY<T>) -> &'b mut T { pub fn get_ref<T>(self, v: &mut XY<T>) -> &mut T {
match *self { match self {
Orientation::Horizontal => &mut v.x, Orientation::Horizontal => &mut v.x,
Orientation::Vertical => &mut v.y, Orientation::Vertical => &mut v.y,
} }
@ -63,8 +63,8 @@ impl Orientation {
/// ///
/// For an horizontal view, returns (Sum(x), Max(y)). /// For an horizontal view, returns (Sum(x), Max(y)).
/// For a vertical view, returns (Max(x),Sum(y)). /// For a vertical view, returns (Max(x),Sum(y)).
pub fn stack<'a, T: Iterator<Item = &'a Vec2>>(&self, iter: T) -> Vec2 { pub fn stack<'a, T: Iterator<Item = &'a Vec2>>(self, iter: T) -> Vec2 {
match *self { match self {
Orientation::Horizontal => { Orientation::Horizontal => {
iter.fold(Vec2::zero(), |a, b| a.stack_horizontal(b)) 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. /// 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(); let mut result = Vec2::zero();
*self.get_ref(&mut result) = main_axis; *self.get_ref(&mut result) = main_axis;
*self.swap().get_ref(&mut result) = second_axis; *self.swap().get_ref(&mut result) = second_axis;

View File

@ -260,8 +260,8 @@ impl MouseEvent {
/// Returns the button used by this event, if any. /// Returns the button used by this event, if any.
/// ///
/// Returns `None` if `self` is `WheelUp` or `WheelDown`. /// Returns `None` if `self` is `WheelUp` or `WheelDown`.
pub fn button(&self) -> Option<MouseButton> { pub fn button(self) -> Option<MouseButton> {
match *self { match self {
MouseEvent::Press(btn) MouseEvent::Press(btn)
| MouseEvent::Release(btn) | MouseEvent::Release(btn)
| MouseEvent::Hold(btn) => Some(btn), | MouseEvent::Hold(btn) => Some(btn),

View File

@ -308,7 +308,7 @@ impl<'a, 'b> Printer<'a, 'b> {
offset: self.offset, offset: self.offset,
size: self.size, size: self.size,
focused: self.focused, focused: self.focused,
theme: theme, theme,
backend: self.backend, backend: self.backend,
output_size: self.output_size, output_size: self.output_size,
content_offset: self.content_offset, content_offset: self.content_offset,

View File

@ -14,7 +14,7 @@ impl ColorPair {
/// Return an inverted color pair. /// Return an inverted color pair.
/// ///
/// With swapped front abd back color. /// With swapped front abd back color.
pub fn invert(&self) -> Self { pub fn invert(self) -> Self {
ColorPair { ColorPair {
front: self.back, front: self.back,
back: self.front, back: self.front,

View File

@ -67,7 +67,7 @@ impl Palette {
/// Returns `None` if the given key was not found. /// Returns `None` if the given key was not found.
pub fn custom<'a>(&'a self, key: &str) -> Option<&'a Color> { pub fn custom<'a>(&'a self, key: &str) -> Option<&'a Color> {
self.custom.get(key).and_then(|node| { self.custom.get(key).and_then(|node| {
if let &PaletteNode::Color(ref color) = node { if let PaletteNode::Color(ref color) = *node {
Some(color) Some(color)
} else { } else {
None None

View File

@ -270,9 +270,7 @@ impl Dialog {
} }
/// Returns an iterator on this buttons for this dialog. /// Returns an iterator on this buttons for this dialog.
pub fn buttons_mut<'a>( pub fn buttons_mut(&mut self) -> impl Iterator<Item = &mut Button> {
&'a mut self,
) -> impl Iterator<Item = &'a mut Button> {
self.buttons.iter_mut().map(|b| &mut b.button.view) self.buttons.iter_mut().map(|b| &mut b.button.view)
} }

View File

@ -585,10 +585,12 @@ impl View for EditView {
let selected = self.content[self.cursor..] let selected = self.content[self.cursor..]
.graphemes(true) .graphemes(true)
.next() .next()
.expect(&format!( .unwrap_or_else(|| {
"Found no char at cursor {} in {}", panic!(
self.cursor, &self.content "Found no char at cursor {} in {}",
)); self.cursor, &self.content
)
});
if self.secret { if self.secret {
make_small_stars(selected.width()) make_small_stars(selected.width())
} else { } else {

View File

@ -245,11 +245,12 @@ where
let offsets = self.scrollbar_thumb_offsets(lengths); let offsets = self.scrollbar_thumb_offsets(lengths);
// Iterate on axises, and keep the one we grabbed. // 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) XY::zip4(Orientation::pair(), position, lengths, offsets)
.keep(grabbed.and(self.enabled)) .keep(grabbed.and(self.enabled))
.into_iter() .into_iter()
.filter_map(|x| x) .filter_map(|x| x)
.next()
{ {
if pos >= offset && pos < offset + length { if pos >= offset && pos < offset + length {
// We grabbed the thumb! Now scroll from that position. // We grabbed the thumb! Now scroll from that position.

View File

@ -10,6 +10,15 @@ pub struct XY<T> {
pub y: T, pub y: T,
} }
impl<T> IntoIterator for XY<T> {
type Item = T;
type IntoIter = iter::Chain<iter::Once<T>, iter::Once<T>>;
fn into_iter(self) -> Self::IntoIter {
iter::once(self.x).chain(iter::once(self.y))
}
}
impl<T> XY<T> { impl<T> XY<T> {
/// Creates a new `XY` from the given values. /// Creates a new `XY` from the given values.
pub fn new(x: T, y: T) -> Self { pub fn new(x: T, y: T) -> Self {
@ -88,11 +97,6 @@ impl<T> XY<T> {
iter::once(&self.x).chain(iter::once(&self.y)) 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<T>, iter::Once<T>> {
iter::once(self.x).chain(iter::once(self.y))
}
/// Returns a reference to the value on the given axis. /// Returns a reference to the value on the given axis.
pub fn get(&self, o: Orientation) -> &T { pub fn get(&self, o: Orientation) -> &T {
match o { match o {
@ -177,34 +181,34 @@ impl<T> XY<Option<T>> {
impl XY<bool> { impl XY<bool> {
/// Returns `true` if any of `x` or `y` is `true`. /// Returns `true` if any of `x` or `y` is `true`.
pub fn any(&self) -> bool { pub fn any(self) -> bool {
use std::ops::BitOr; use std::ops::BitOr;
self.fold(BitOr::bitor) self.fold(BitOr::bitor)
} }
/// Returns `true` if both `x` and `y` are `true`. /// Returns `true` if both `x` and `y` are `true`.
pub fn both(&self) -> bool { pub fn both(self) -> bool {
use std::ops::BitAnd; use std::ops::BitAnd;
self.fold(BitAnd::bitand) self.fold(BitAnd::bitand)
} }
/// For each axis, keeps elements from `other` if `self` is `true`. /// For each axis, keeps elements from `other` if `self` is `true`.
pub fn select<T>(&self, other: XY<T>) -> XY<Option<T>> { pub fn select<T>(self, other: XY<T>) -> XY<Option<T>> {
self.zip_map(other, |keep, o| if keep { Some(o) } else { None }) 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`. /// For each axis, selects `if_true` if `self` is true, else `if_false`.
pub fn select_or<T>(&self, if_true: XY<T>, if_false: XY<T>) -> XY<T> { pub fn select_or<T>(self, if_true: XY<T>, if_false: XY<T>) -> XY<T> {
self.select(if_true).unwrap_or(if_false) self.select(if_true).unwrap_or(if_false)
} }
/// Returns a term-by-term AND operation. /// 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) self.zip_map(other, |s, o| s && o)
} }
/// Returns a term-by-term OR operation. /// 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) self.zip_map(other, |s, o| s || o)
} }
} }