Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'from_reader' for LoadAverage struct #191

Merged
merged 1 commit into from
Jul 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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