From 319bcdd3556b6eec58d077e0def1f2ef296968a3 Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Wed, 22 Apr 2020 01:31:22 +0200 Subject: [PATCH] Reduce cargo clippy warnings --- src/app.rs | 81 +++++++++++++++----------------- src/buffer.rs | 4 +- src/config.rs | 54 ++++++++++----------- src/configfmt.rs | 2 +- src/desktop.rs | 8 ++-- src/draw.rs | 15 +++--- src/main.rs | 16 ++++--- src/widget.rs | 16 ++----- src/widgets/audio/alsa_sound.rs | 8 ++-- src/widgets/audio/pulse_sound.rs | 14 ++---- src/widgets/backlight.rs | 14 ++---- src/widgets/bar_widget.rs | 14 +++--- src/widgets/battery.rs | 8 ++-- src/widgets/calendar.rs | 24 +++++----- src/widgets/clock.rs | 14 +++--- src/widgets/date.rs | 14 +++--- src/widgets/launcher.rs | 50 ++++++++++---------- 17 files changed, 168 insertions(+), 188 deletions(-) diff --git a/src/app.rs b/src/app.rs index fe74423..8d0f6a4 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,4 +1,5 @@ use std::collections::VecDeque; +use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::Sender; use std::sync::{Arc, Mutex}; @@ -50,9 +51,9 @@ impl AppInner { outputs: Vec::new(), shell: None, draw_tx: tx, - output_mode: output_mode, + output_mode, visible: true, - scale: scale, + scale, } } @@ -71,7 +72,7 @@ impl AppInner { .create_surface(NewProxy::implement_dummy) .unwrap(); - let this_is_stupid = Arc::new(Mutex::new(false)); + let this_is_stupid = AtomicBool::new(false); let shell_surface = shell .get_layer_surface( @@ -83,9 +84,7 @@ impl AppInner { layer.implement_closure( move |evt, layer| match evt { zwlr_layer_surface_v1::Event::Configure { serial, .. } => { - let mut x = this_is_stupid.lock().unwrap(); - if !*x { - *x = true; + if !this_is_stupid.compare_and_swap(false, true, Ordering::SeqCst) { *(configured_surfaces.lock().unwrap()) += 1; layer.ack_configure(serial); tx.send(Cmd::ForceDraw).unwrap(); @@ -132,7 +131,7 @@ impl AppInner { match self.output_mode { OutputMode::Active => { - if self.shell_surfaces.len() > 0 { + if !self.shell_surfaces.is_empty() { return; } @@ -181,7 +180,7 @@ impl AppInner { .outputs .iter() .filter(|(output_id, _)| *output_id != id) - .map(|(x, y)| (x.clone(), y.clone())) + .map(|(x, y)| (*x, y.clone())) .collect(); if output.1.as_ref().version() >= 3 { output.1.release() @@ -224,7 +223,7 @@ impl App { if !inner.visible || inner.shell_surfaces.len() != *inner.configured_surfaces.lock().unwrap() - || inner.surfaces.len() == 0 + || inner.surfaces.is_empty() { // Not ready yet return Ok(()); @@ -269,7 +268,7 @@ impl App { &mut DrawContext { buf: &mut buf, bg: &self.bg, - time: &time, + time, force, }, (0, 0), @@ -277,7 +276,7 @@ impl App { mmap.flush().unwrap(); - if !force && !report.full_damage && report.damage.len() == 0 { + if !force && !report.full_damage && report.damage.is_empty() { // Nothing to do self.pools.never_mind(); return Ok(()); @@ -352,7 +351,7 @@ impl App { } pub fn new(tx: Sender, output_mode: OutputMode, bg: Color, scale: u32) -> App { - let inner = Arc::new(Mutex::new(AppInner::new(tx.clone(), output_mode, scale))); + let inner = Arc::new(Mutex::new(AppInner::new(tx, output_mode, scale))); // // Set up modules @@ -365,8 +364,7 @@ impl App { let display_wrapper = display .as_ref() .make_wrapper(&event_queue.get_token()) - .unwrap() - .into(); + .unwrap(); // // Set up global manager @@ -409,13 +407,12 @@ impl App { // wl_shm let shm_formats = Arc::new(Mutex::new(Vec::new())); - let shm_formats2 = shm_formats.clone(); let shm = manager .instantiate_range(1, 1, |shm| { shm.implement_closure( move |evt, _| { if let wl_shm::Event::Format { format } = evt { - shm_formats2.lock().unwrap().push(format); + shm_formats.lock().unwrap().push(format); } }, (), @@ -450,21 +447,22 @@ impl App { utf8, state, .. - } => match state { - KeyState::Pressed => match keysym { - keysyms::XKB_KEY_Escape => kbd_clone.lock().unwrap().push_back(Cmd::Exit), - keysyms::XKB_KEY_c if modifiers_state.lock().unwrap().ctrl => { - kbd_clone.lock().unwrap().push_back(Cmd::Exit) + } => { + if let KeyState::Pressed = state { + match keysym { + keysyms::XKB_KEY_Escape => kbd_clone.lock().unwrap().push_back(Cmd::Exit), + keysyms::XKB_KEY_c if modifiers_state.lock().unwrap().ctrl => { + kbd_clone.lock().unwrap().push_back(Cmd::Exit) + } + v => kbd_clone.lock().unwrap().push_back(Cmd::Keyboard { + key: v, + key_state: state, + modifiers_state: *modifiers_state.lock().unwrap(), + interpreted: utf8, + }), } - v => kbd_clone.lock().unwrap().push_back(Cmd::Keyboard { - key: v, - key_state: state, - modifiers_state: modifiers_state.lock().unwrap().clone(), - interpreted: utf8, - }), - }, - _ => (), - }, + } + } KbEvent::Modifiers { modifiers } => *modifiers_state.lock().unwrap() = modifiers, _ => (), }) @@ -522,18 +520,17 @@ impl App { vert_scroll += value; } } - wl_pointer::Event::Button { button, state, .. } => match state { - wl_pointer::ButtonState::Released => { + wl_pointer::Event::Button { button, state, .. } => { + if let wl_pointer::ButtonState::Released = state { btn = button; btn_clicked = true; } - _ => {} - }, + } wl_pointer::Event::Frame => { if vert_scroll != 0.0 || horiz_scroll != 0.0 { pointer_clone.lock().unwrap().push_back(Cmd::MouseScroll { scroll: (horiz_scroll, vert_scroll), - pos: pos, + pos, }); vert_scroll = 0.0; horiz_scroll = 0.0; @@ -542,7 +539,7 @@ impl App { pointer_clone .lock() .unwrap() - .push_back(Cmd::MouseClick { btn: btn, pos: pos }); + .push_back(Cmd::MouseClick { btn, pos }); btn_clicked = false; } } @@ -556,13 +553,13 @@ impl App { display.flush().unwrap(); App { - display: display, - event_queue: event_queue, - cmd_queue: cmd_queue, - pools: pools, + display, + event_queue, + cmd_queue, + pools, widget: None, - bg: bg, - inner: inner, + bg, + inner, last_damage: None, last_dim: (0, 0), } diff --git a/src/buffer.rs b/src/buffer.rs index 0188690..e5a56e7 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -11,8 +11,8 @@ pub struct Buffer<'a> { impl<'a> Buffer<'a> { pub fn new(buf: &'a mut MmapMut, dimensions: (u32, u32)) -> Buffer { Buffer { - buf: buf, - dimensions: dimensions, + buf, + dimensions, subdimensions: None, } } diff --git a/src/config.rs b/src/config.rs index 960534e..e892be5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -19,8 +19,8 @@ pub enum Widget { height: u32, widget: Box, }, - HorizontalLayout(Vec>), - VerticalLayout(Vec>), + HorizontalLayout(Vec), + VerticalLayout(Vec), Clock { font_size: f32, }, @@ -66,11 +66,11 @@ pub enum Widget { impl Widget { pub fn construct( self, - time: &NaiveDateTime, + time: NaiveDateTime, tx: Sender, ) -> Option> { match self { - Widget::Margin { margins, widget } => match widget.construct(time, tx.clone()) { + Widget::Margin { margins, widget } => match widget.construct(time, tx) { Some(w) => Some(widget::Margin::new(margins, w)), None => None, }, @@ -78,7 +78,7 @@ impl Widget { width, height, widget, - } => match widget.construct(time, tx.clone()) { + } => match widget.construct(time, tx) { Some(w) => Some(widget::Fixed::new((width, height), w)), None => None, }, @@ -113,17 +113,17 @@ impl Widget { } => Some(widgets::launcher::Launcher::new( font_size, length, - tx.clone(), + tx, app_opener, term_opener, - if url_opener.len() == 0 { + if url_opener.is_empty() { "xdg_open ".to_string() } else { url_opener }, )), Widget::Battery { font_size, length } => { - match widgets::battery::UpowerBattery::new(font_size, length, tx.clone()) { + match widgets::battery::UpowerBattery::new(font_size, length, tx) { Ok(w) => Some(w), Err(_) => None, } @@ -145,7 +145,7 @@ impl Widget { } #[cfg(feature = "pulseaudio-widget")] Widget::PulseAudio { font_size, length } => { - match widgets::audio::PulseAudio::new(font_size, length, tx.clone()) { + match widgets::audio::PulseAudio::new(font_size, length, tx) { Ok(w) => Some(w), Err(_) => None, } @@ -189,51 +189,51 @@ impl Default for Config { widget: Widget::Margin { margins: (20, 20, 20, 20), widget: Box::new(Widget::VerticalLayout(vec![ - Box::new(Widget::HorizontalLayout(vec![ - Box::new(Widget::Margin { + Widget::HorizontalLayout(vec![ + Widget::Margin { margins: (0, 88, 0, 32), widget: Box::new(Widget::VerticalLayout(vec![ - Box::new(Widget::Date { font_size: 64.0 }), - Box::new(Widget::Clock { font_size: 256.0 }), + Widget::Date { font_size: 64.0 }, + Widget::Clock { font_size: 256.0 }, ])), - }), - Box::new(Widget::VerticalLayout(vec![ - Box::new(Widget::Margin { + }, + Widget::VerticalLayout(vec![ + Widget::Margin { margins: (0, 0, 0, 8), widget: Box::new(Widget::Battery { font_size: 24.0, length: 600, }), - }), - Box::new(Widget::Margin { + }, + Widget::Margin { margins: (0, 0, 0, 8), widget: Box::new(Widget::Backlight { device: "intel_backlight".to_string(), font_size: 24.0, length: 600, }), - }), + }, #[cfg(feature = "pulseaudio-widget")] - Box::new(Widget::Margin { + Widget::Margin { margins: (0, 0, 0, 8), widget: Box::new(Widget::PulseAudio { font_size: 24.0, length: 600, }), - }), - ])), - ])), - Box::new(Widget::Calendar { + }, + ]), + ]), + Widget::Calendar { font_size: 16.0, sections: 3, - }), - Box::new(Widget::Launcher { + }, + Widget::Launcher { font_size: 32.0, length: 1200, app_opener: "".to_string(), term_opener: "".to_string(), url_opener: "".to_string(), - }), + }, ])), }, output_mode: Default::default(), diff --git a/src/configfmt.rs b/src/configfmt.rs index 4aa02ea..8c3d280 100644 --- a/src/configfmt.rs +++ b/src/configfmt.rs @@ -8,7 +8,7 @@ pub enum ConfigFmt { Json, } -pub const CONFIG_NAMES: &'static [&'static str] = &[ +pub const CONFIG_NAMES: &[&str] = &[ #[cfg(feature = "yaml-cfg")] "config.yaml", #[cfg(feature = "json-cfg")] diff --git a/src/desktop.rs b/src/desktop.rs index f89de29..2f8e822 100644 --- a/src/desktop.rs +++ b/src/desktop.rs @@ -40,12 +40,12 @@ impl Desktop { keywords: desktop .get("Keywords") .map(|x| { - x.split(";") + x.split(';') .map(|y| y.trim().to_string()) .filter(|z| z != "") .collect() }) - .unwrap_or(vec![]), + .unwrap_or_else(|| vec![]), }), None => Err(Box::new(io_error::new( ErrorKind::NotFound, @@ -92,7 +92,7 @@ pub fn load_desktop_files() -> Vec { let xdg_data_home = match env::var_os("XDG_DATA_HOME") { Some(s) => s.into_string().unwrap(), - None => format!("{}/.local/share", home).to_string(), + None => format!("{}/.local/share", home), }; let xdg_data_dirs = match env::var_os("XDG_DATA_DIRS") { Some(s) => s.into_string().unwrap(), @@ -100,7 +100,7 @@ pub fn load_desktop_files() -> Vec { }; std::iter::once(xdg_data_home.as_str()) - .chain(xdg_data_dirs.split(":")) + .chain(xdg_data_dirs.split(':')) .map(|p| Desktop::parse_dir(&format!("{}/applications", p))) .filter_map(Result::ok) .flatten() diff --git a/src/draw.rs b/src/draw.rs index 75e7571..466ce51 100644 --- a/src/draw.rs +++ b/src/draw.rs @@ -6,9 +6,8 @@ use std::collections::HashMap; use lazy_static::lazy_static; use rusttype::{point, Font as RustFont, Scale}; -pub static DEJAVUSANS_MONO_FONT_DATA: &'static [u8] = - include_bytes!("../fonts/dejavu/DejaVuSansMono.ttf"); -pub static ROBOTO_REGULAR_FONT_DATA: &'static [u8] = include_bytes!("../fonts/Roboto-Regular.ttf"); +pub static DEJAVUSANS_MONO_FONT_DATA: &[u8] = include_bytes!("../fonts/dejavu/DejaVuSansMono.ttf"); +pub static ROBOTO_REGULAR_FONT_DATA: &[u8] = include_bytes!("../fonts/Roboto-Regular.ttf"); lazy_static! { pub static ref DEJAVUSANS_MONO: RustFont<'static> = @@ -47,9 +46,9 @@ impl CachedGlyph { render[pos as usize] = o; }); CachedGlyph { - origin: origin, - dimensions: dimensions, - render: render, + origin, + dimensions, + render, } } else { CachedGlyph { @@ -92,8 +91,8 @@ impl Font { pub fn new(font: &'static RustFont, size: f32) -> Font { Font { glyphs: HashMap::new(), - font: font, - size: size, + font, + size, } } diff --git a/src/main.rs b/src/main.rs index 051195b..01c98e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,7 +45,7 @@ fn main() { Ok(dir) => dir + "/wldash", Err(_) => match env::var("HOME") { Ok(home) => home + "/.config/wldash", - Err(_) => panic!("unable to find user folder"), + _ => panic!("unable to find user folder"), }, }; @@ -105,7 +105,7 @@ fn main() { }, None => Mode::Start, }; - if let Some(_) = args.next() { + if args.next().is_some() { // total = args.count + 2 (the two we skipped + the rest) eprintln!("expected 0 or 1 arguments, got {}", args.count() + 2); std::process::exit(1); @@ -115,7 +115,7 @@ fn main() { match mode { Mode::ToggleVisible => { - if let Ok(mut socket) = UnixStream::connect(socket_path.clone()) { + if let Ok(mut socket) = UnixStream::connect(socket_path) { socket.write_all(b"toggle_visible\n").unwrap(); return; }; @@ -129,13 +129,13 @@ fn main() { }; } Mode::Start => { - if let Ok(_) = UnixStream::connect(socket_path.clone()) { + if UnixStream::connect(socket_path.clone()).is_ok() { eprintln!("wldash is already running"); std::process::exit(1); }; } Mode::Daemonize => { - if let Ok(_) = UnixStream::connect(socket_path.clone()) { + if UnixStream::connect(socket_path.clone()).is_ok() { eprintln!("wldash is already running"); std::process::exit(1); }; @@ -161,9 +161,11 @@ fn main() { let tx_draw_mod = tx_draw.clone(); let (mod_tx, mod_rx) = channel(); std::thread::spawn(move || { - let n = Local::now().naive_local(); // Print, write to a file, or send to an HTTP server. - match config.widget.construct(&n, tx_draw_mod) { + match config + .widget + .construct(Local::now().naive_local(), tx_draw_mod) + { Some(w) => mod_tx.send(w).unwrap(), None => panic!("no widget configured"), } diff --git a/src/widget.rs b/src/widget.rs index 43c6d3b..4ce8776 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -7,7 +7,7 @@ pub use smithay_client_toolkit::keyboard::{KeyState, ModifiersState}; pub struct DrawContext<'a> { pub buf: &'a mut Buffer<'a>, pub bg: &'a Color, - pub time: &'a NaiveDateTime, + pub time: NaiveDateTime, pub force: bool, } @@ -76,7 +76,7 @@ pub struct VerticalLayout { #[allow(dead_code)] impl VerticalLayout { pub fn new(children: Vec>) -> Box { - Box::new(VerticalLayout { children: children }) + Box::new(VerticalLayout { children }) } } @@ -187,7 +187,7 @@ pub struct HorizontalLayout { #[allow(dead_code)] impl HorizontalLayout { pub fn new(children: Vec>) -> Box { - Box::new(HorizontalLayout { children: children }) + Box::new(HorizontalLayout { children }) } } @@ -301,10 +301,7 @@ pub struct Margin { #[allow(dead_code)] impl Margin { pub fn new(margins: (u32, u32, u32, u32), child: Box) -> Box { - Box::new(Margin { - child: child, - margins: margins, - }) + Box::new(Margin { child, margins }) } } @@ -378,10 +375,7 @@ pub struct Fixed { #[allow(dead_code)] impl Fixed { pub fn new(size: (u32, u32), child: Box) -> Box { - Box::new(Fixed { - child: child, - size: size, - }) + Box::new(Fixed { child, size }) } } diff --git a/src/widgets/audio/alsa_sound.rs b/src/widgets/audio/alsa_sound.rs index fb1ec09..dbd94d9 100644 --- a/src/widgets/audio/alsa_sound.rs +++ b/src/widgets/audio/alsa_sound.rs @@ -4,8 +4,8 @@ use crate::widgets::bar_widget::{BarWidget, BarWidgetImpl}; use alsa::mixer::{Mixer, Selem, SelemChannelId, SelemId}; -const CARD_NAME: &'static str = "default"; -const SELEM_NAME: &'static str = "Master"; +const CARD_NAME: &str = "default"; +const SELEM_NAME: &str = "Master"; const SELEM_ID: u32 = 0u32; #[inline] @@ -15,8 +15,8 @@ fn alsa_error_to_io_error(fail: &'static str, err: &alsa::Error) -> ::std::io::E let errno = err .errno() .map(|errno| format!("; errno = {}", errno)) - .unwrap_or(String::new()); - return ::std::io::Error::new(kind, format!("{}: {}{}", fail, func, errno)); + .unwrap_or_default(); + ::std::io::Error::new(kind, format!("{}: {}{}", fail, func, errno)) } #[inline] diff --git a/src/widgets/audio/pulse_sound.rs b/src/widgets/audio/pulse_sound.rs index 9d95a49..60bac74 100644 --- a/src/widgets/audio/pulse_sound.rs +++ b/src/widgets/audio/pulse_sound.rs @@ -158,7 +158,7 @@ impl PulseAudioClient { match req { PulseAudioClientRequest::GetDefaultDevice(s) => { introspector.get_server_info(move |info| { - let _res = PulseAudioClient::server_info_callback( + PulseAudioClient::server_info_callback( cl.clone(), l.clone(), info, @@ -260,13 +260,12 @@ impl PulseAudioClient { } fn send(&self, request: PulseAudioClientRequest) -> Result<(), ::std::io::Error> { - let res = self.sender.send(request).map_err(|_e| { + self.sender.send(request).map_err(|_e| { ::std::io::Error::new( ::std::io::ErrorKind::Other, "unable to send pulseaudio request", ) - }); - res + }) } fn server_info_callback(s: Arc>, listener: F, server_info: &ServerInfo) @@ -371,10 +370,7 @@ impl PulseAudioSoundDevice { cl.default_sink.to_string() }; (*inner.lock().unwrap()).name = Some(name.clone()); - let device = PulseAudioSoundDevice { - client: cl, - inner: inner, - }; + let device = PulseAudioSoundDevice { client: cl, inner }; let (tx, rx) = channel(); { let cl = client.lock().unwrap(); @@ -497,7 +493,7 @@ impl PulseAudio { *dirty.lock().unwrap() = true; sender.send(Cmd::Draw).unwrap(); })?; - Ok(Box::new(PulseAudio { device: device })) + Ok(Box::new(PulseAudio { device })) }) } } diff --git a/src/widgets/backlight.rs b/src/widgets/backlight.rs index 73497d5..9a51849 100644 --- a/src/widgets/backlight.rs +++ b/src/widgets/backlight.rs @@ -2,9 +2,10 @@ use crate::color::Color; use crate::widget::WaitContext; use crate::widgets::bar_widget::{BarWidget, BarWidgetImpl}; +use std::fs; use std::fs::OpenOptions; +use std::io::Write; use std::io::{Error, ErrorKind}; -use std::io::{Read, Write}; use std::path::{Path, PathBuf}; pub struct Backlight { @@ -14,11 +15,9 @@ pub struct Backlight { } fn read_file_as_u64(path: &Path) -> Result { - let mut file = OpenOptions::new().read(true).open(path)?; - let mut str = String::new(); - file.read_to_string(&mut str)?; - str.pop(); - str.parse::() + let mut s = fs::read_to_string(path)?; + s.pop(); + s.parse::() .map_err(|_e| Error::new(ErrorKind::Other, "unable to parse value")) } @@ -101,7 +100,6 @@ impl BarWidgetImpl for Backlight { Ok(val) => val, Err(err) => { eprintln!("Error while trying to change brightness: {}", err); - () } } } @@ -111,7 +109,6 @@ impl BarWidgetImpl for Backlight { Ok(val) => val, Err(err) => { eprintln!("Error while trying to change brightness: {}", err); - () } } } @@ -125,7 +122,6 @@ impl BarWidgetImpl for Backlight { Ok(val) => val, Err(err) => { eprintln!("Error while trying to change brightness: {}", err); - () } } } diff --git a/src/widgets/bar_widget.rs b/src/widgets/bar_widget.rs index e0a1a94..7087092 100644 --- a/src/widgets/bar_widget.rs +++ b/src/widgets/bar_widget.rs @@ -34,9 +34,9 @@ impl BarWidget { Box::new(BarWidget { bar_impl: w, dirty: Arc::new(Mutex::new(true)), - font: font, + font, font_size: font_size as u32, - length: length, + length, }) } @@ -53,10 +53,10 @@ impl BarWidget { Ok(Box::new(BarWidget { bar_impl: im, - dirty: dirty, - font: font, + dirty, + font, font_size: font_size as u32, - length: length, + length, })) } } @@ -119,8 +119,8 @@ impl Widget for BarWidget { (self.length - bar_off, self.font_size), )?; Ok(DrawReport { - width: width, - height: height, + width, + height, damage: vec![buf.get_signed_bounds()], full_damage: false, }) diff --git a/src/widgets/battery.rs b/src/widgets/battery.rs index 5efd792..1b0f75e 100644 --- a/src/widgets/battery.rs +++ b/src/widgets/battery.rs @@ -8,8 +8,6 @@ use std::sync::{Arc, Mutex}; use nix::poll::{PollFd, PollFlags}; -use dbus; - fn get_upower_property( con: &dbus::Connection, device_path: &str, @@ -44,7 +42,7 @@ struct DbusConnection(dbus::Connection); impl std::convert::AsRef for DbusConnection { fn as_ref(&self) -> &dbus::Connection { - return &self.0; + &self.0 } } @@ -156,8 +154,8 @@ impl UpowerBattery { con: DbusConnection(con), dirty, sender, - capacity: capacity, - state: state, + capacity, + state, watch: fds[0], }) } diff --git a/src/widgets/calendar.rs b/src/widgets/calendar.rs index 43d407c..c94f476 100644 --- a/src/widgets/calendar.rs +++ b/src/widgets/calendar.rs @@ -22,10 +22,10 @@ impl Calendar { &self, buf: &mut Buffer, background_color: &Color, - orig: &NaiveDate, - time: &NaiveDate, + orig: NaiveDate, + time: NaiveDate, ) -> Result<(i32, i32, i32, i32), ::std::io::Error> { - let mut time = time.clone(); + let mut time = time; let mut y_off = 1; let mut done = false; @@ -153,7 +153,7 @@ impl Calendar { } impl Calendar { - pub fn new(time: &NaiveDateTime, font_size: f32, sections: u32) -> Box { + pub fn new(time: NaiveDateTime, font_size: f32, sections: u32) -> Box { let mut calendar_cache = Font::new(&DEJAVUSANS_MONO, font_size * 2.0); calendar_cache.add_str_to_cache("0123456789"); let mut month_cache = Font::new(&ROBOTO_REGULAR, font_size * 4.0); @@ -166,12 +166,12 @@ impl Calendar { cur_date: time.date(), dirty: true, offset: 0.0, - sections: sections, + sections, font_size: font_size as u32, - calendar_cache: calendar_cache, - month_cache: month_cache, - year_cache: year_cache, - day_cache: day_cache, + calendar_cache, + month_cache, + year_cache, + day_cache, }) } } @@ -233,7 +233,7 @@ impl Widget for Calendar { } let cal_pad = 7 * self.font_size * 3 + self.font_size * 5; for idx in 0..self.sections { - self.draw_month(&mut buf.offset((cal_pad * idx, 0))?, ctx.bg, &time, &t)?; + self.draw_month(&mut buf.offset((cal_pad * idx, 0))?, ctx.bg, time, t)?; t = if t.month() == 12 { t.with_year(t.year() + 1).unwrap().with_month(1).unwrap() @@ -242,8 +242,8 @@ impl Widget for Calendar { }; } Ok(DrawReport { - width: width, - height: height, + width, + height, damage: vec![buf.get_signed_bounds()], full_damage: false, }) diff --git a/src/widgets/clock.rs b/src/widgets/clock.rs index d6e6d50..30fcbd7 100644 --- a/src/widgets/clock.rs +++ b/src/widgets/clock.rs @@ -11,14 +11,14 @@ pub struct Clock { } impl Clock { - pub fn new(time: &NaiveDateTime, size: f32) -> Box { + pub fn new(time: NaiveDateTime, size: f32) -> Box { let mut clock_cache = Font::new(&ROBOTO_REGULAR, size); clock_cache.add_str_to_cache("0123456789:"); Box::new(Clock { - cur_time: time.clone(), - clock_cache: clock_cache, - size: size, + cur_time: time, + clock_cache, + size, }) } } @@ -54,7 +54,7 @@ impl Widget for Clock { return Ok(DrawReport::empty(width, height)); } - self.cur_time = ctx.time.clone(); + self.cur_time = ctx.time; let buf = &mut ctx.buf.subdimensions((pos.0, pos.1, width, height))?; buf.memset(ctx.bg); @@ -70,8 +70,8 @@ impl Widget for Clock { )?; Ok(DrawReport { - width: width, - height: height, + width, + height, damage: vec![(pos.0 as i32, pos.1 as i32, width as i32, height as i32)], full_damage: false, }) diff --git a/src/widgets/date.rs b/src/widgets/date.rs index 5cf273a..b6b7558 100644 --- a/src/widgets/date.rs +++ b/src/widgets/date.rs @@ -11,15 +11,15 @@ pub struct Date { } impl Date { - pub fn new(time: &NaiveDateTime, size: f32) -> Box { + pub fn new(time: NaiveDateTime, size: f32) -> Box { let mut date_cache = Font::new(&ROBOTO_REGULAR, size); date_cache .add_str_to_cache("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,/ "); Box::new(Date { - cur_time: time.clone(), - date_cache: date_cache, - size: size, + cur_time: time, + date_cache, + size, }) } } @@ -43,7 +43,7 @@ impl Widget for Date { return Ok(DrawReport::empty(width, height)); } - self.cur_time = ctx.time.clone(); + self.cur_time = ctx.time; let buf = &mut ctx.buf.subdimensions((pos.0, pos.1, width, height))?; buf.memset(ctx.bg); @@ -61,8 +61,8 @@ impl Widget for Date { )?; Ok(DrawReport { - width: width, - height: height, + width, + height, damage: vec![(pos.0 as i32, pos.1 as i32, width as i32, height as i32)], full_damage: false, }) diff --git a/src/widgets/launcher.rs b/src/widgets/launcher.rs index bfb84e1..e9a08ac 100644 --- a/src/widgets/launcher.rs +++ b/src/widgets/launcher.rs @@ -50,15 +50,15 @@ impl Launcher { offset: 0, font: RefCell::new(Font::new(&ROBOTO_REGULAR, font_size)), font_size: font_size as u32, - length: length, + length, dirty: true, tx: listener, }) } fn draw_launcher(&self, buf: &mut Buffer, bg: &Color) -> Result<(), ::std::io::Error> { - let mut x_off = if self.input.len() > 0 { - let c = if self.matches.len() == 0 { + let mut x_off = if !self.input.is_empty() { + let c = if self.matches.is_empty() { Color::new(1.0, 0.5, 0.5, 1.0) } else { Color::new(1.0, 1.0, 1.0, 1.0) @@ -79,7 +79,7 @@ impl Launcher { Ok(b) => b, Err(_) => break, }; - let size = if idx == self.offset && self.input.len() > 0 { + let size = if idx == self.offset && !self.input.is_empty() { let (_, indices) = fuzzy_indices(&m.name.to_lowercase(), &self.input.to_lowercase()) .unwrap_or((0, vec![])); @@ -122,7 +122,7 @@ impl Launcher { .0 + self.font_size / 4; - let x_off = if self.input.len() > 0 { + let x_off = if !self.input.is_empty() { let dim = self.font.borrow_mut().auto_draw_text( &mut buf.offset((x_off, 0))?, bg, @@ -160,7 +160,7 @@ impl Launcher { .0 + self.font_size / 4; - if self.input.len() > 0 { + if !self.input.is_empty() { self.font.borrow_mut().auto_draw_text( &mut buf.offset((x_off, 0))?, bg, @@ -175,8 +175,8 @@ impl Launcher { fn calc(s: &str) -> Result { rcalc_lib::parse::eval(s, &mut rcalc_lib::parse::CalcState::new()) - .map(|x| format!("{}", x).to_string()) - .map_err(|x| format!("{}", x).to_string()) + .map(|x| format!("{}", x)) + .map_err(|x| format!("{}", x)) } fn wlcopy(s: &str) -> Result<(), String> { @@ -223,7 +223,7 @@ impl Matcher { let mut m = self .matches .iter() - .map(|(key, ma)| (ma.clone(), key.clone())) + .map(|(key, ma)| (*ma, key.clone())) .collect::>(); m.sort_by(|(ma1, d1), (ma2, d2)| { @@ -313,8 +313,8 @@ impl Widget for Launcher { }?; Ok(DrawReport { - width: width, - height: height, + width, + height, damage: vec![buf.get_signed_bounds()], full_damage: false, }) @@ -330,7 +330,7 @@ impl Widget for Launcher { match key { keysyms::XKB_KEY_u if modifiers.ctrl => self.leave(), keysyms::XKB_KEY_BackSpace => { - if self.input.len() > 0 { + if !self.input.is_empty() { self.input = self.input[..self.input.len() - 1].to_string(); self.offset = 0; self.result = None; @@ -339,12 +339,11 @@ impl Widget for Launcher { } keysyms::XKB_KEY_Return => { match self.input.chars().next() { - Some('=') => match self.result { - Some(ref v) => { + Some('=') => { + if let Some(ref v) = self.result { let _ = wlcopy(&v); } - None => (), - }, + } Some('!') => { let _ = Command::new("sh") .arg("-c") @@ -367,24 +366,24 @@ impl Widget for Launcher { &self.app_opener }; - let lexed = if prefix.len() > 0 { + let lexed = if !prefix.is_empty() { let mut prefix = shlex::split(prefix).unwrap(); prefix.push(exec); prefix } else { shlex::split(&exec).unwrap() }; - if lexed.len() > 0 { + if !lexed.is_empty() { let _ = Command::new(lexed[0].clone()).args(&lexed[1..]).spawn(); self.tx.send(Cmd::Exit).unwrap(); } } if let Some(url) = &d.url { - if self.url_opener.len() > 0 { + if !self.url_opener.is_empty() { let mut lexed = shlex::split(&self.url_opener).unwrap(); lexed.push(url.to_string()); - if lexed.len() > 0 { + if !lexed.is_empty() { let _ = Command::new(lexed[0].clone()) .args(&lexed[1..]) .spawn(); @@ -397,26 +396,25 @@ impl Widget for Launcher { }; } keysyms::XKB_KEY_Right => { - if self.matches.len() > 0 && self.offset < self.matches.len() - 1 { + if !self.matches.is_empty() && self.offset < self.matches.len() - 1 { self.offset += 1; self.dirty = true; } } keysyms::XKB_KEY_Left => { - if self.matches.len() > 0 && self.offset > 0 { + if !self.matches.is_empty() && self.offset > 0 { self.offset -= 1; self.dirty = true; } } - _ => match interpreted { - Some(v) => { + _ => { + if let Some(v) = interpreted { self.input += &v; self.offset = 0; self.result = None; self.dirty = true; } - None => {} - }, + } } } fn mouse_click(&mut self, _: u32, _: (u32, u32)) {}