Skip to content

Commit

Permalink
Fix build failure on Windows (#2)
Browse files Browse the repository at this point in the history
Signed-off-by: kennytm <[email protected]>
  • Loading branch information
kennytm authored Feb 1, 2020
1 parent aa98faf commit df280e3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
22 changes: 13 additions & 9 deletions src/io.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;

/// IOLoad represents current system block devices IO statistics
#[derive(Debug)]
Expand Down Expand Up @@ -45,24 +43,30 @@ impl IOLoad {
///
/// # Notes
///
/// Current don't support non-unix operating system
/// Currently not supported outside Unix. On those operating systems, this
/// method always returns an empty map.
#[cfg(not(unix))]
pub fn snapshot() -> HashMap<String, NICLoad> {
pub fn snapshot() -> HashMap<String, Self> {
HashMap::new()
}

/// Returns the current IO statistics
///
/// # Notes
///
/// Currently not supported outside Unix. On those operating systems, this
/// method always returns an empty map.
#[cfg(unix)]
pub fn snapshot() -> HashMap<String, IOLoad> {
pub fn snapshot() -> HashMap<String, Self> {
let mut result = HashMap::new();
// https://www.kernel.org/doc/Documentation/block/stat.txt
if let Ok(dir) = std::fs::read_dir("/sys/block/") {
for entry in dir {
if let Ok(entry) = entry {
let stat = entry.path().join("stat");
let mut s = String::new();
if let Err(_) = File::open(stat).and_then(|mut f| f.read_to_string(&mut s)) {
continue;
let s = match std::fs::read_to_string(stat) {
Ok(s) => s,
Err(_) => continue,
};
let parts = s
.split_whitespace()
Expand All @@ -71,7 +75,7 @@ impl IOLoad {
if parts.len() != 11 {
continue;
}
let load = IOLoad {
let load = Self {
read_io: parts[0],
read_merges: parts[1],
read_sectors: parts[2],
Expand Down
14 changes: 10 additions & 4 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,21 @@ impl NICLoad {
///
/// # Notes
///
/// Current don't support non-unix operating system
/// Currently not supported outside Unix. On those operating systems, this
/// method always returns an empty map.
#[cfg(not(unix))]
pub fn snapshot() -> HashMap<String, NICLoad> {
pub fn snapshot() -> HashMap<String, Self> {
HashMap::new()
}

/// Returns the current network interfaces card statistics
///
/// # Notes
///
/// Currently not supported outside Unix. On those operating systems, this
/// method always returns an empty map.
#[cfg(unix)]
pub fn snapshot() -> HashMap<String, NICLoad> {
pub fn snapshot() -> HashMap<String, Self> {
let mut result = HashMap::new();
if let Ok(dir) = std::fs::read_dir("/sys/class/net/") {
for entry in dir {
Expand All @@ -53,7 +59,7 @@ impl NICLoad {
.parse()
.unwrap_or_default()
};
let load = NICLoad {
let load = Self {
rx_bytes: read("rx_bytes"),
tx_bytes: read("tx_bytes"),
rx_packets: read("rx_packets"),
Expand Down
1 change: 1 addition & 0 deletions src/windows/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ pub fn get_key_used(p: &mut Processor) -> &mut Option<KeyHandler> {
&mut p.key_used
}

/// get_cpu_frequency returns the CPU frequency in MHz
pub fn get_cpu_frequency() -> u64 {
// TODO: support windows
0
Expand Down

0 comments on commit df280e3

Please sign in to comment.