Skip to content

Commit

Permalink
Merge pull request #191 from ludo-c/wip-alternate-root-for-loadaverage
Browse files Browse the repository at this point in the history
Add 'from_reader' for LoadAverage struct
  • Loading branch information
eminence authored Jul 29, 2022
2 parents f54530e + 07bd9ca commit 2344fd4
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,16 @@ pub struct LoadAverage {
impl LoadAverage {
/// Reads load average info from `/proc/loadavg`
pub fn new() -> ProcResult<LoadAverage> {
let mut f = FileWrapper::open("/proc/loadavg")?;
let mut s = String::new();
f.read_to_string(&mut s)?;
let mut s = s.split_whitespace();
LoadAverage::from_reader(FileWrapper::open("/proc/loadavg")?)
}

/// Get LoadAverage from a custom Read instead of the default `/proc/loadavg`.
pub fn from_reader<R: io::Read>(r: R) -> ProcResult<LoadAverage> {
let mut reader = BufReader::new(r);
let mut line = String::new();

reader.read_to_string(&mut line)?;
let mut s = line.split_whitespace();

let one = expect!(f32::from_str(expect!(s.next())));
let five = expect!(f32::from_str(expect!(s.next())));
Expand Down

0 comments on commit 2344fd4

Please sign in to comment.