Skip to content

Commit

Permalink
Merge pull request #439 from YJDoc2/remove_derivative
Browse files Browse the repository at this point in the history
replace derivative crate with derive_more crate
  • Loading branch information
cyqsimon authored Oct 17, 2024
2 parents 6314f1c + de4bdc5 commit 01e5fed
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 44 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Changed

* Bump msrv to 1.75.0 #439 - @YJDoc2
* Replace `derivative` with `derive_more` #439 - @YJDoc2
* Add build optimizations for release binary #434 - @pando85
* Minor cleanup and optimisations #435 - @cyqsimon

Expand Down
38 changes: 27 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ keywords = ["networking", "utilization", "cli"]
license = "MIT"
readme = "README.md"
repository = "https://github.com/imsnif/bandwhich"
rust-version = "1.74.0"
rust-version = "1.75.0"
description = "Display current network utilization by process, connection and remote IP/hostname"

[features]
Expand All @@ -32,7 +32,6 @@ chrono = "0.4"
clap-verbosity-flag = "2.2.2"
clap = { version = "4.5.19", features = ["derive"] }
crossterm = "0.28.1"
derivative = "2.2.0"
ipnetwork = "0.20.0"
itertools = "0.13.0"
log = "0.4.22"
Expand All @@ -47,6 +46,7 @@ tokio = { version = "1.40", features = ["rt", "sync"] }
trust-dns-resolver = "0.23.2"
unicode-width = "0.2.0"
strum = { version = "0.26.3", features = ["derive"] }
derive_more = {version = "1.0.0", features = ["debug"]}

[target.'cfg(any(target_os = "android", target_os = "linux"))'.dependencies]
procfs = "0.17.0"
Expand All @@ -71,7 +71,7 @@ clap = { version = "4.5.19", features = ["derive"] }
clap-verbosity-flag = "2.2.2"
clap_complete = "4.5.32"
clap_mangen = "0.2.23"
derivative = "2.2.0"
derive_more = {version = "1.0.0", features = ["debug"]}
strum = { version = "0.26.3", features = ["derive"] }

[target.'cfg(target_os = "windows")'.build-dependencies]
Expand Down
6 changes: 2 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use std::{net::Ipv4Addr, path::PathBuf};

use clap::{Args, Parser, ValueEnum, ValueHint};
use clap_verbosity_flag::{InfoLevel, Verbosity};
use derivative::Derivative;
use derive_more::Debug;
use strum::EnumIter;

#[derive(Clone, Debug, Derivative, Parser)]
#[derivative(Default)]
#[derive(Clone, Debug, Parser, Default)]
#[command(name = "bandwhich", version)]
pub struct Opt {
#[arg(short, long)]
Expand Down Expand Up @@ -34,7 +33,6 @@ pub struct Opt {
pub log_to: Option<PathBuf>,

#[command(flatten)]
#[derivative(Default(value = "Verbosity::new(0, 0)"))]
pub verbosity: Verbosity<InfoLevel>,

#[command(flatten)]
Expand Down
22 changes: 8 additions & 14 deletions src/display/components/display_bandwidth.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use std::fmt;

use derivative::Derivative;
use derive_more::Debug;

use crate::cli::UnitFamily;

#[derive(Copy, Clone, Derivative)]
#[derivative(Debug)]
#[derive(Copy, Clone, Debug)]
pub struct DisplayBandwidth {
#[derivative(Debug(format_with = "fmt_f64"))]
// Custom format for reduced precision.
// Workaround for FP calculation discrepancy between Unix and Windows.
// See https://github.com/rust-lang/rust/issues/111405#issuecomment-2055964223.
#[debug("{bandwidth:.10e}")]
pub bandwidth: f64,
pub unit_family: BandwidthUnitFamily,
}
Expand All @@ -19,17 +21,9 @@ impl fmt::Display for DisplayBandwidth {
}
}

/// Custom formatter with reduced precision.
///
/// Workaround for FP calculation discrepancy between Unix and Windows.
/// See https://github.com/rust-lang/rust/issues/111405#issuecomment-2055964223.
fn fmt_f64(val: &f64, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{val:.10e}")
}

/// Type wrapper around [`UnitFamily`] to provide extra functionality.
#[derive(Copy, Clone, Derivative, Default, Eq, PartialEq)]
#[derivative(Debug = "transparent")]
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
#[debug("{_0:?}")]
pub struct BandwidthUnitFamily(UnitFamily);
impl From<UnitFamily> for BandwidthUnitFamily {
fn from(value: UnitFamily) -> Self {
Expand Down
16 changes: 4 additions & 12 deletions src/display/components/table.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashMap, fmt, net::IpAddr, ops::Index, rc::Rc};
use std::{collections::HashMap, net::IpAddr, ops::Index, rc::Rc};

use derivative::Derivative;
use derive_more::Debug;
use itertools::Itertools;
use ratatui::{
layout::{Constraint, Rect},
Expand Down Expand Up @@ -165,8 +165,7 @@ impl TableData {
///
/// Note that the number of columns here is independent of the number of columns
/// being actually shown. If width-constrained, we might only show some of the columns.
#[derive(Clone, Derivative)]
#[derivative(Debug)]
#[derive(Clone, Debug)]
struct NColsTableData<const C: usize> {
/// The name of each column.
column_names: [&'static str; C],
Expand All @@ -176,20 +175,13 @@ struct NColsTableData<const C: usize> {
///
/// This function should return a vector of column indices.
/// The indices should be less than `C`; otherwise this will cause a runtime panic.
#[derivative(Debug(format_with = "debug_fn::<C>"))]
#[debug("Rc</* function pointer */>")]
column_selector: Rc<ColumnSelectorFn>,
}

/// Clippy wanted me to write this. 💢
type ColumnSelectorFn = dyn Fn(&DisplayLayout) -> Vec<usize>;

fn debug_fn<const C: usize>(
_func: &Rc<ColumnSelectorFn>,
f: &mut fmt::Formatter,
) -> Result<(), fmt::Error> {
write!(f, "Rc</* function pointer */>")
}

/// A table displayed by bandwhich.
#[derive(Clone, Debug)]
pub struct Table {
Expand Down

0 comments on commit 01e5fed

Please sign in to comment.