]> git.seodisparate.com - swaybar_info/commitdiff
Refactored handling of Option wrapping primitives
authorStephen Seo <seo.disparate@gmail.com>
Tue, 18 Oct 2022 12:47:11 +0000 (21:47 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Tue, 18 Oct 2022 12:47:11 +0000 (21:47 +0900)
Option wrapping primities implement Copy, so they can be used directly
in the closure, and is more efficient to use copies of f64/u16 than with
references to them.

Changelog.md
src/main.rs

index 9ed44584662dfac3d427ed9078c803f3a3432adb..241c1b09dab058b9b2a372072fa1a49f789538d7 100644 (file)
@@ -4,6 +4,8 @@
 
 Minor refactoring of how the netgraph string is handled.
 
+Refactoring of handling of Option types wrapping primitive values.
+
 ## 0.1.5
 
 Implemented `--netdev_width=<width>` which sets the minimum width of the netdev
index 23baf738de0baaecdbe83861e4bf71914821069c..ebcaa97efb72e4958ce99a87b78062aee38c6517 100644 (file)
@@ -160,16 +160,14 @@ fn main() {
 
     let handle_net = |is_empty: bool,
                       net: &mut proc::NetInfo,
-                      array: &mut SwaybarArray,
-                      graph_opt: &Option<f64>,
-                      width: &Option<u16>|
+                      array: &mut SwaybarArray|
      -> Result<(), proc::Error> {
         net.update()?;
-        let (netinfo_string, graph_string) = net.get_netstring(*graph_opt)?;
+        let (netinfo_string, graph_string) = net.get_netstring(net_graph_max)?;
         let netinfo_parts: Vec<&str> = netinfo_string.split_whitespace().collect();
 
         if is_empty {
-            if graph_opt.is_some() {
+            if net_graph_max.is_some() {
                 let mut graph_obj =
                     SwaybarObject::from_string("net_graph".to_owned(), graph_string);
                 graph_obj.color = Some("#ffff88".into());
@@ -177,7 +175,7 @@ fn main() {
             }
 
             let mut width_string: Option<String> = None;
-            if let Some(width) = *width {
+            if let Some(width) = net_width {
                 let mut string = String::with_capacity(width.into());
                 for _ in 0..width {
                     string.push('0');
@@ -207,7 +205,7 @@ fn main() {
                 array.push_object(up_object);
             }
         } else {
-            if graph_opt.is_some() {
+            if net_graph_max.is_some() {
                 if let Some(graph_obj) = array.get_by_name_mut("net_graph") {
                     graph_obj.full_text = graph_string;
                 }
@@ -231,7 +229,7 @@ fn main() {
 
         // network traffic
         if let Some(net) = net_obj.as_mut() {
-            if let Err(e) = handle_net(is_empty, net, &mut array, &net_graph_max, &net_width) {
+            if let Err(e) = handle_net(is_empty, net, &mut array) {
                 let mut stderr_handle = io::stderr().lock();
                 stderr_handle.write_all(format!("{}\n", e).as_bytes()).ok();
                 net_obj = None;