Skip to content

Commit 50d3c39

Browse files
authored
Merge pull request #22 from Apokleos/master
optimize and refactor: read_to_string and read_i[u]64_from
2 parents ac4e6ed + 033fa4b commit 50d3c39

File tree

11 files changed

+43
-163
lines changed

11 files changed

+43
-163
lines changed

src/blkio.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
//!
99
//! See the Kernel's documentation for more information about this subsystem, found at:
1010
//! [Documentation/cgroup-v1/blkio-controller.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/blkio-controller.txt)
11-
use std::fs::File;
12-
use std::io::{Read, Write};
11+
use std::io::Write;
1312
use std::path::PathBuf;
1413

1514
use crate::error::ErrorKind::*;
1615
use crate::error::*;
1716

17+
use crate::{read_string_from, read_u64_from};
1818
use crate::{
1919
BlkIoResources, ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem,
2020
};
@@ -401,25 +401,6 @@ impl<'a> From<&'a Subsystem> for &'a BlkIoController {
401401
}
402402
}
403403

404-
fn read_string_from(mut file: File) -> Result<String> {
405-
let mut string = String::new();
406-
match file.read_to_string(&mut string) {
407-
Ok(_) => Ok(string.trim().to_string()),
408-
Err(e) => Err(Error::with_cause(ReadFailed, e)),
409-
}
410-
}
411-
412-
fn read_u64_from(mut file: File) -> Result<u64> {
413-
let mut string = String::new();
414-
match file.read_to_string(&mut string) {
415-
Ok(_) => string
416-
.trim()
417-
.parse()
418-
.map_err(|e| Error::with_cause(ParseError, e)),
419-
Err(e) => Err(Error::with_cause(ReadFailed, e)),
420-
}
421-
}
422-
423404
impl BlkIoController {
424405
/// Constructs a new `BlkIoController` with `root` serving as the root of the control group.
425406
pub fn new(root: PathBuf, v2: bool) -> Self {

src/cpu.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::path::PathBuf;
1515

1616
use crate::error::ErrorKind::*;
1717
use crate::error::*;
18-
use crate::{parse_max_value, read_i64_from};
18+
use crate::{parse_max_value, read_i64_from, read_u64_from};
1919

2020
use crate::{
2121
ControllIdentifier, ControllerInternal, Controllers, CpuResources, CustomizedAttribute,
@@ -110,17 +110,6 @@ impl<'a> From<&'a Subsystem> for &'a CpuController {
110110
}
111111
}
112112

113-
fn read_u64_from(mut file: File) -> Result<u64> {
114-
let mut string = String::new();
115-
match file.read_to_string(&mut string) {
116-
Ok(_) => string
117-
.trim()
118-
.parse()
119-
.map_err(|e| Error::with_cause(ParseError, e)),
120-
Err(e) => Err(Error::with_cause(ReadFailed, e)),
121-
}
122-
}
123-
124113
impl CpuController {
125114
/// Contructs a new `CpuController` with `root` serving as the root of the control group.
126115
pub fn new(root: PathBuf, v2: bool) -> Self {

src/cpuacct.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
//!
88
//! See the Kernel's documentation for more information about this subsystem, found at:
99
//! [Documentation/cgroup-v1/cpuacct.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt)
10-
use std::fs::File;
11-
use std::io::{Read, Write};
10+
use std::io::Write;
1211
use std::path::PathBuf;
1312

1413
use crate::error::ErrorKind::*;
1514
use crate::error::*;
1615

16+
use crate::{read_string_from, read_u64_from};
1717
use crate::{ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem};
1818

1919
/// A controller that allows controlling the `cpuacct` subsystem of a Cgroup.
@@ -97,26 +97,6 @@ impl<'a> From<&'a Subsystem> for &'a CpuAcctController {
9797
}
9898
}
9999

100-
fn read_u64_from(mut file: File) -> Result<u64> {
101-
let mut string = String::new();
102-
let res = file.read_to_string(&mut string);
103-
match res {
104-
Ok(_) => match string.trim().parse() {
105-
Ok(e) => Ok(e),
106-
Err(e) => Err(Error::with_cause(ParseError, e)),
107-
},
108-
Err(e) => Err(Error::with_cause(ReadFailed, e)),
109-
}
110-
}
111-
112-
fn read_string_from(mut file: File) -> Result<String> {
113-
let mut string = String::new();
114-
match file.read_to_string(&mut string) {
115-
Ok(_) => Ok(string.trim().to_string()),
116-
Err(e) => Err(Error::with_cause(ReadFailed, e)),
117-
}
118-
}
119-
120100
impl CpuAcctController {
121101
/// Contructs a new `CpuAcctController` with `root` serving as the root of the control group.
122102
pub fn new(root: PathBuf) -> Self {

src/cpuset.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
//! [Documentation/cgroup-v1/cpusets.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/cpusets.txt)
1111
1212
use log::*;
13-
use std::fs::File;
14-
use std::io::{Read, Write};
13+
use std::io::Write;
1514
use std::path::PathBuf;
1615

1716
use crate::error::ErrorKind::*;
1817
use crate::error::*;
1918

19+
use crate::{read_string_from, read_u64_from};
2020
use crate::{
2121
ControllIdentifier, ControllerInternal, Controllers, CpuResources, Resources, Subsystem,
2222
};
@@ -204,25 +204,6 @@ impl<'a> From<&'a Subsystem> for &'a CpuSetController {
204204
}
205205
}
206206

207-
fn read_string_from(mut file: File) -> Result<String> {
208-
let mut string = String::new();
209-
match file.read_to_string(&mut string) {
210-
Ok(_) => Ok(string.trim().to_string()),
211-
Err(e) => Err(Error::with_cause(ReadFailed, e)),
212-
}
213-
}
214-
215-
fn read_u64_from(mut file: File) -> Result<u64> {
216-
let mut string = String::new();
217-
match file.read_to_string(&mut string) {
218-
Ok(_) => string
219-
.trim()
220-
.parse()
221-
.map_err(|e| Error::with_cause(ParseError, e)),
222-
Err(e) => Err(Error::with_cause(ReadFailed, e)),
223-
}
224-
}
225-
226207
/// Parse a string like "1,2,4-5,8" into a list of (start, end) tuples.
227208
fn parse_range(s: String) -> Result<Vec<(u64, u64)>> {
228209
let mut fin = Vec::new();

src/hugetlb.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
//!
99
//! See the Kernel's documentation for more information about this subsystem, found at:
1010
//! [Documentation/cgroup-v1/hugetlb.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/hugetlb.txt)
11-
use std::fs::File;
12-
use std::io::{Read, Write};
11+
use std::io::Write;
1312
use std::path::PathBuf;
1413

1514
use crate::error::ErrorKind::*;
1615
use crate::error::*;
17-
use crate::flat_keyed_to_vec;
16+
use crate::{flat_keyed_to_vec, read_u64_from};
1817

1918
use crate::{
2019
ControllIdentifier, ControllerInternal, Controllers, HugePageResources, Resources, Subsystem,
@@ -86,17 +85,6 @@ impl<'a> From<&'a Subsystem> for &'a HugeTlbController {
8685
}
8786
}
8887

89-
fn read_u64_from(mut file: File) -> Result<u64> {
90-
let mut string = String::new();
91-
match file.read_to_string(&mut string) {
92-
Ok(_) => string
93-
.trim()
94-
.parse()
95-
.map_err(|e| Error::with_cause(ParseError, e)),
96-
Err(e) => Err(Error::with_cause(ReadFailed, e)),
97-
}
98-
}
99-
10088
impl HugeTlbController {
10189
/// Constructs a new `HugeTlbController` with `root` serving as the root of the control group.
10290
pub fn new(root: PathBuf, v2: bool) -> Self {

src/lib.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::collections::HashMap;
1010
use std::fs::{self, File};
1111
use std::io::{BufRead, BufReader, Read, Write};
1212
use std::path::{Path, PathBuf};
13+
use std::str::FromStr;
1314

1415
macro_rules! update_and_test {
1516
($self: ident, $set_func:ident, $value:expr, $get_func:ident) => {
@@ -832,14 +833,35 @@ pub fn nested_keyed_to_hashmap(mut file: File) -> Result<HashMap<String, HashMap
832833
Ok(h)
833834
}
834835

835-
/// read and parse an i64 data
836-
fn read_i64_from(mut file: File) -> Result<i64> {
836+
fn read_from<T>(mut file: File) -> Result<T>
837+
where
838+
T: FromStr,
839+
<T as FromStr>::Err: 'static + Send + Sync + std::error::Error,
840+
{
837841
let mut string = String::new();
838842
match file.read_to_string(&mut string) {
839843
Ok(_) => string
840844
.trim()
841-
.parse()
845+
.parse::<T>()
842846
.map_err(|e| Error::with_cause(ParseError, e)),
843847
Err(e) => Err(Error::with_cause(ReadFailed, e)),
844848
}
845849
}
850+
851+
fn read_string_from(mut file: File) -> Result<String> {
852+
let mut string = String::new();
853+
match file.read_to_string(&mut string) {
854+
Ok(_) => Ok(string.trim().to_string()),
855+
Err(e) => Err(Error::with_cause(ReadFailed, e)),
856+
}
857+
}
858+
859+
/// read and parse an u64 data
860+
fn read_u64_from(file: File) -> Result<u64> {
861+
read_from::<u64>(file)
862+
}
863+
864+
/// read and parse an i64 data
865+
fn read_i64_from(file: File) -> Result<i64> {
866+
read_from::<i64>(file)
867+
}

src/memory.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99
//! See the Kernel's documentation for more information about this subsystem, found at:
1010
//! [Documentation/cgroup-v1/memory.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt)
1111
use std::collections::HashMap;
12-
use std::fs::File;
13-
use std::io::{Read, Write};
12+
use std::io::Write;
1413
use std::path::PathBuf;
1514
use std::sync::mpsc::Receiver;
1615

1716
use crate::error::ErrorKind::*;
1817
use crate::error::*;
1918
use crate::events;
20-
use crate::read_i64_from;
19+
use crate::{read_i64_from, read_string_from, read_u64_from};
2120

2221
use crate::flat_keyed_to_hashmap;
2322

@@ -870,25 +869,6 @@ impl<'a> From<&'a Subsystem> for &'a MemController {
870869
}
871870
}
872871

873-
fn read_u64_from(mut file: File) -> Result<u64> {
874-
let mut string = String::new();
875-
match file.read_to_string(&mut string) {
876-
Ok(_) => string
877-
.trim()
878-
.parse()
879-
.map_err(|e| Error::with_cause(ParseError, e)),
880-
Err(e) => Err(Error::with_cause(ReadFailed, e)),
881-
}
882-
}
883-
884-
fn read_string_from(mut file: File) -> Result<String> {
885-
let mut string = String::new();
886-
match file.read_to_string(&mut string) {
887-
Ok(_) => Ok(string.trim().to_string()),
888-
Err(e) => Err(Error::with_cause(ReadFailed, e)),
889-
}
890-
}
891-
892872
#[cfg(test)]
893873
mod tests {
894874
use crate::memory::{

src/net_cls.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
//!
88
//! See the Kernel's documentation for more information about this subsystem, found at:
99
//! [Documentation/cgroup-v1/net_cls.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/net_cls.txt)
10-
use std::fs::File;
11-
use std::io::{Read, Write};
10+
use std::io::Write;
1211
use std::path::PathBuf;
1312

1413
use crate::error::ErrorKind::*;
1514
use crate::error::*;
1615

16+
use crate::read_u64_from;
1717
use crate::{
1818
ControllIdentifier, ControllerInternal, Controllers, NetworkResources, Resources, Subsystem,
1919
};
@@ -74,17 +74,6 @@ impl<'a> From<&'a Subsystem> for &'a NetClsController {
7474
}
7575
}
7676

77-
fn read_u64_from(mut file: File) -> Result<u64> {
78-
let mut string = String::new();
79-
match file.read_to_string(&mut string) {
80-
Ok(_) => string
81-
.trim()
82-
.parse()
83-
.map_err(|e| Error::with_cause(ParseError, e)),
84-
Err(e) => Err(Error::with_cause(ReadFailed, e)),
85-
}
86-
}
87-
8877
impl NetClsController {
8978
/// Constructs a new `NetClsController` with `root` serving as the root of the control group.
9079
pub fn new(root: PathBuf) -> Self {

src/net_prio.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
//! See the Kernel's documentation for more information about this subsystem, found at:
99
//! [Documentation/cgroup-v1/net_prio.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/net_prio.txt)
1010
use std::collections::HashMap;
11-
use std::fs::File;
12-
use std::io::{BufRead, BufReader, Read, Write};
11+
use std::io::{BufRead, BufReader, Write};
1312
use std::path::PathBuf;
1413

1514
use crate::error::ErrorKind::*;
1615
use crate::error::*;
1716

17+
use crate::read_u64_from;
1818
use crate::{
1919
ControllIdentifier, ControllerInternal, Controllers, NetworkResources, Resources, Subsystem,
2020
};
@@ -77,17 +77,6 @@ impl<'a> From<&'a Subsystem> for &'a NetPrioController {
7777
}
7878
}
7979

80-
fn read_u64_from(mut file: File) -> Result<u64> {
81-
let mut string = String::new();
82-
match file.read_to_string(&mut string) {
83-
Ok(_) => string
84-
.trim()
85-
.parse()
86-
.map_err(|e| Error::with_cause(ParseError, e)),
87-
Err(e) => Err(Error::with_cause(ReadFailed, e)),
88-
}
89-
}
90-
9180
impl NetPrioController {
9281
/// Constructs a new `NetPrioController` with `root` serving as the root of the control group.
9382
pub fn new(root: PathBuf) -> Self {

src/pid.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
//!
99
//! See the Kernel's documentation for more information about this subsystem, found at:
1010
//! [Documentation/cgroups-v1/pids.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/pids.txt)
11-
use std::fs::File;
1211
use std::io::{Read, Write};
1312
use std::path::PathBuf;
1413

1514
use crate::error::ErrorKind::*;
1615
use crate::error::*;
1716

17+
use crate::read_u64_from;
1818
use crate::{
1919
parse_max_value, ControllIdentifier, ControllerInternal, Controllers, MaxValue, PidResources,
2020
Resources, Subsystem,
@@ -89,17 +89,6 @@ impl<'a> From<&'a Subsystem> for &'a PidController {
8989
}
9090
}
9191

92-
fn read_u64_from(mut file: File) -> Result<u64> {
93-
let mut string = String::new();
94-
match file.read_to_string(&mut string) {
95-
Ok(_) => string
96-
.trim()
97-
.parse()
98-
.map_err(|e| Error::with_cause(ParseError, e)),
99-
Err(e) => Err(Error::with_cause(ReadFailed, e)),
100-
}
101-
}
102-
10392
impl PidController {
10493
/// Constructors a new `PidController` instance, with `root` serving as the controller's root
10594
/// directory.

0 commit comments

Comments
 (0)