Skip to content

Commit

Permalink
Improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Jan 31, 2024
1 parent 31665aa commit 60f205c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 9 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,17 @@ for disk in &disks {
println!("{disk:?}");
}

// Network interfaces name, data received and data transmitted:
// Network interfaces name, total data received and total data transmitted:
let networks = Networks::new_with_refreshed_list();
println!("=> networks:");
for (interface_name, data) in &networks {
println!("{interface_name}: {}/{} B", data.received(), data.transmitted());
println!(
"{interface_name}: {} B (down) / {} B (up)",
data.total_received(),
data.total_transmitted(),
);
// If you want the amount of data received/transmitted since last call
// to `Networks::refresh`, use `received`/`transmitted`.
}

// Components temperature:
Expand Down
86 changes: 79 additions & 7 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2034,7 +2034,7 @@ impl Networks {
/// Refreshes the network interfaces' content. If you didn't run [`Networks::refresh_list`]
/// before, calling this method won't do anything as no interfaces are present.
///
/// ⚠️ If a user is added or removed, this method won't take it into account. Use
/// ⚠️ If a network interface is added or removed, this method won't take it into account. Use
/// [`Networks::refresh_list`] instead.
///
/// ⚠️ If you didn't call [`Networks::refresh_list`] beforehand, this method will do nothing
Expand Down Expand Up @@ -2077,10 +2077,19 @@ pub struct NetworkData {
impl NetworkData {
/// Returns the number of received bytes since the last refresh.
///
/// If you want the total number of bytes received, take a look at the
/// [`total_received`](NetworkData::total_received) method.
///
/// ```no_run
/// use sysinfo::Networks;
/// use std::{thread, time};
///
/// let mut networks = Networks::new_with_refreshed_list();
/// // Waiting a bit to get data from network...
/// thread::sleep(time::Duration::from_millis(10));
/// // Refreshing again to generate diff.
/// networks.refresh();
///
/// let networks = Networks::new_with_refreshed_list();
/// for (interface_name, network) in &networks {
/// println!("in: {} B", network.received());
/// }
Expand All @@ -2091,6 +2100,9 @@ impl NetworkData {

/// Returns the total number of received bytes.
///
/// If you want the amount of received bytes since the last refresh, take a look at the
/// [`received`](NetworkData::received) method.
///
/// ```no_run
/// use sysinfo::Networks;
///
Expand All @@ -2105,10 +2117,19 @@ impl NetworkData {

/// Returns the number of transmitted bytes since the last refresh.
///
/// If you want the total number of bytes transmitted, take a look at the
/// [`total_transmitted`](NetworkData::total_transmitted) method.
///
/// ```no_run
/// use sysinfo::Networks;
/// use std::{thread, time};
///
/// let mut networks = Networks::new_with_refreshed_list();
/// // Waiting a bit to get data from network...
/// thread::sleep(time::Duration::from_millis(10));
/// // Refreshing again to generate diff.
/// networks.refresh();
///
/// let networks = Networks::new_with_refreshed_list();
/// for (interface_name, network) in &networks {
/// println!("out: {} B", network.transmitted());
/// }
Expand All @@ -2119,6 +2140,9 @@ impl NetworkData {

/// Returns the total number of transmitted bytes.
///
/// If you want the amount of transmitted bytes since the last refresh, take a look at the
/// [`transmitted`](NetworkData::transmitted) method.
///
/// ```no_run
/// use sysinfo::Networks;
///
Expand All @@ -2133,10 +2157,19 @@ impl NetworkData {

/// Returns the number of incoming packets since the last refresh.
///
/// If you want the total number of packets received, take a look at the
/// [`total_packets_received`](NetworkData::total_packets_received) method.
///
/// ```no_run
/// use sysinfo::Networks;
/// use std::{thread, time};
///
/// let mut networks = Networks::new_with_refreshed_list();
/// // Waiting a bit to get data from network...
/// thread::sleep(time::Duration::from_millis(10));
/// // Refreshing again to generate diff.
/// networks.refresh();
///
/// let networks = Networks::new_with_refreshed_list();
/// for (interface_name, network) in &networks {
/// println!("in: {}", network.packets_received());
/// }
Expand All @@ -2147,6 +2180,9 @@ impl NetworkData {

/// Returns the total number of incoming packets.
///
/// If you want the amount of received packets since the last refresh, take a look at the
/// [`packets_received`](NetworkData::packets_received) method.
///
/// ```no_run
/// use sysinfo::Networks;
///
Expand All @@ -2161,10 +2197,19 @@ impl NetworkData {

/// Returns the number of outcoming packets since the last refresh.
///
/// If you want the total number of packets transmitted, take a look at the
/// [`total_packets_transmitted`](NetworkData::total_packets_transmitted) method.
///
/// ```no_run
/// use sysinfo::Networks;
/// use std::{thread, time};
///
/// let mut networks = Networks::new_with_refreshed_list();
/// // Waiting a bit to get data from network...
/// thread::sleep(time::Duration::from_millis(10));
/// // Refreshing again to generate diff.
/// networks.refresh();
///
/// let networks = Networks::new_with_refreshed_list();
/// for (interface_name, network) in &networks {
/// println!("out: {}", network.packets_transmitted());
/// }
Expand All @@ -2175,6 +2220,9 @@ impl NetworkData {

/// Returns the total number of outcoming packets.
///
/// If you want the amount of transmitted packets since the last refresh, take a look at the
/// [`packets_transmitted`](NetworkData::packets_transmitted) method.
///
/// ```no_run
/// use sysinfo::Networks;
///
Expand All @@ -2189,10 +2237,19 @@ impl NetworkData {

/// Returns the number of incoming errors since the last refresh.
///
/// If you want the total number of errors on received packets, take a look at the
/// [`total_errors_on_received`](NetworkData::total_errors_on_received) method.
///
/// ```no_run
/// use sysinfo::Networks;
/// use std::{thread, time};
///
/// let mut networks = Networks::new_with_refreshed_list();
/// // Waiting a bit to get data from network...
/// thread::sleep(time::Duration::from_millis(10));
/// // Refreshing again to generate diff.
/// networks.refresh();
///
/// let networks = Networks::new_with_refreshed_list();
/// for (interface_name, network) in &networks {
/// println!("in: {}", network.errors_on_received());
/// }
Expand All @@ -2203,6 +2260,9 @@ impl NetworkData {

/// Returns the total number of incoming errors.
///
/// If you want the amount of errors on received packets since the last refresh, take a look at
/// the [`errors_on_received`](NetworkData::errors_on_received) method.
///
/// ```no_run
/// use sysinfo::Networks;
///
Expand All @@ -2217,10 +2277,19 @@ impl NetworkData {

/// Returns the number of outcoming errors since the last refresh.
///
/// If you want the total number of errors on transmitted packets, take a look at the
/// [`total_errors_on_transmitted`](NetworkData::total_errors_on_transmitted) method.
///
/// ```no_run
/// use sysinfo::Networks;
/// use std::{thread, time};
///
/// let mut networks = Networks::new_with_refreshed_list();
/// // Waiting a bit to get data from network...
/// thread::sleep(time::Duration::from_millis(10));
/// // Refreshing again to generate diff.
/// networks.refresh();
///
/// let networks = Networks::new_with_refreshed_list();
/// for (interface_name, network) in &networks {
/// println!("out: {}", network.errors_on_transmitted());
/// }
Expand All @@ -2231,6 +2300,9 @@ impl NetworkData {

/// Returns the total number of outcoming errors.
///
/// If you want the amount of errors on transmitted packets since the last refresh, take a look at
/// the [`errors_on_transmitted`](NetworkData::errors_on_transmitted) method.
///
/// ```no_run
/// use sysinfo::Networks;
///
Expand Down

0 comments on commit 60f205c

Please sign in to comment.