optimize and refactor: read_to_string and read_i[u]64_from

There's so many Duplicated read_string_from method in different
subsystem's implementation, so as read_u64_from/read_i64_from methods.
(1) Move the read_string_from method into `lib.rs`,
called by each subsystem implementation as needed.
(2) Refactor read_u[i]64_from method with the help Rust Generic f
unction `read_from` and wrapped by read_u64_from or read_i64_from.

fix: #20

Signed-off-by: LiYa'nan <oliverliyn@gmail.com>
This commit is contained in:
LiYa'nan
2020-12-23 13:55:08 +08:00
parent ac4e6eda66
commit 033fa4b857
11 changed files with 43 additions and 163 deletions
+2 -21
View File
@@ -8,13 +8,13 @@
//! //!
//! See the Kernel's documentation for more information about this subsystem, found at: //! See the Kernel's documentation for more information about this subsystem, found at:
//! [Documentation/cgroup-v1/blkio-controller.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/blkio-controller.txt) //! [Documentation/cgroup-v1/blkio-controller.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/blkio-controller.txt)
use std::fs::File; use std::io::Write;
use std::io::{Read, Write};
use std::path::PathBuf; use std::path::PathBuf;
use crate::error::ErrorKind::*; use crate::error::ErrorKind::*;
use crate::error::*; use crate::error::*;
use crate::{read_string_from, read_u64_from};
use crate::{ use crate::{
BlkIoResources, ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem, BlkIoResources, ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem,
}; };
@@ -401,25 +401,6 @@ impl<'a> From<&'a Subsystem> for &'a BlkIoController {
} }
} }
fn read_string_from(mut file: File) -> Result<String> {
let mut string = String::new();
match file.read_to_string(&mut string) {
Ok(_) => Ok(string.trim().to_string()),
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}
fn read_u64_from(mut file: File) -> Result<u64> {
let mut string = String::new();
match file.read_to_string(&mut string) {
Ok(_) => string
.trim()
.parse()
.map_err(|e| Error::with_cause(ParseError, e)),
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}
impl BlkIoController { impl BlkIoController {
/// Constructs a new `BlkIoController` with `root` serving as the root of the control group. /// Constructs a new `BlkIoController` with `root` serving as the root of the control group.
pub fn new(root: PathBuf, v2: bool) -> Self { pub fn new(root: PathBuf, v2: bool) -> Self {
+1 -12
View File
@@ -15,7 +15,7 @@ use std::path::PathBuf;
use crate::error::ErrorKind::*; use crate::error::ErrorKind::*;
use crate::error::*; use crate::error::*;
use crate::{parse_max_value, read_i64_from}; use crate::{parse_max_value, read_i64_from, read_u64_from};
use crate::{ use crate::{
ControllIdentifier, ControllerInternal, Controllers, CpuResources, CustomizedAttribute, ControllIdentifier, ControllerInternal, Controllers, CpuResources, CustomizedAttribute,
@@ -110,17 +110,6 @@ impl<'a> From<&'a Subsystem> for &'a CpuController {
} }
} }
fn read_u64_from(mut file: File) -> Result<u64> {
let mut string = String::new();
match file.read_to_string(&mut string) {
Ok(_) => string
.trim()
.parse()
.map_err(|e| Error::with_cause(ParseError, e)),
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}
impl CpuController { impl CpuController {
/// Contructs a new `CpuController` with `root` serving as the root of the control group. /// Contructs a new `CpuController` with `root` serving as the root of the control group.
pub fn new(root: PathBuf, v2: bool) -> Self { pub fn new(root: PathBuf, v2: bool) -> Self {
+2 -22
View File
@@ -7,13 +7,13 @@
//! //!
//! See the Kernel's documentation for more information about this subsystem, found at: //! See the Kernel's documentation for more information about this subsystem, found at:
//! [Documentation/cgroup-v1/cpuacct.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt) //! [Documentation/cgroup-v1/cpuacct.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/cpuacct.txt)
use std::fs::File; use std::io::Write;
use std::io::{Read, Write};
use std::path::PathBuf; use std::path::PathBuf;
use crate::error::ErrorKind::*; use crate::error::ErrorKind::*;
use crate::error::*; use crate::error::*;
use crate::{read_string_from, read_u64_from};
use crate::{ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem}; use crate::{ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem};
/// A controller that allows controlling the `cpuacct` subsystem of a Cgroup. /// A controller that allows controlling the `cpuacct` subsystem of a Cgroup.
@@ -97,26 +97,6 @@ impl<'a> From<&'a Subsystem> for &'a CpuAcctController {
} }
} }
fn read_u64_from(mut file: File) -> Result<u64> {
let mut string = String::new();
let res = file.read_to_string(&mut string);
match res {
Ok(_) => match string.trim().parse() {
Ok(e) => Ok(e),
Err(e) => Err(Error::with_cause(ParseError, e)),
},
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}
fn read_string_from(mut file: File) -> Result<String> {
let mut string = String::new();
match file.read_to_string(&mut string) {
Ok(_) => Ok(string.trim().to_string()),
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}
impl CpuAcctController { impl CpuAcctController {
/// Contructs a new `CpuAcctController` with `root` serving as the root of the control group. /// Contructs a new `CpuAcctController` with `root` serving as the root of the control group.
pub fn new(root: PathBuf) -> Self { pub fn new(root: PathBuf) -> Self {
+2 -21
View File
@@ -10,13 +10,13 @@
//! [Documentation/cgroup-v1/cpusets.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/cpusets.txt) //! [Documentation/cgroup-v1/cpusets.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/cpusets.txt)
use log::*; use log::*;
use std::fs::File; use std::io::Write;
use std::io::{Read, Write};
use std::path::PathBuf; use std::path::PathBuf;
use crate::error::ErrorKind::*; use crate::error::ErrorKind::*;
use crate::error::*; use crate::error::*;
use crate::{read_string_from, read_u64_from};
use crate::{ use crate::{
ControllIdentifier, ControllerInternal, Controllers, CpuResources, Resources, Subsystem, ControllIdentifier, ControllerInternal, Controllers, CpuResources, Resources, Subsystem,
}; };
@@ -204,25 +204,6 @@ impl<'a> From<&'a Subsystem> for &'a CpuSetController {
} }
} }
fn read_string_from(mut file: File) -> Result<String> {
let mut string = String::new();
match file.read_to_string(&mut string) {
Ok(_) => Ok(string.trim().to_string()),
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}
fn read_u64_from(mut file: File) -> Result<u64> {
let mut string = String::new();
match file.read_to_string(&mut string) {
Ok(_) => string
.trim()
.parse()
.map_err(|e| Error::with_cause(ParseError, e)),
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}
/// Parse a string like "1,2,4-5,8" into a list of (start, end) tuples. /// Parse a string like "1,2,4-5,8" into a list of (start, end) tuples.
fn parse_range(s: String) -> Result<Vec<(u64, u64)>> { fn parse_range(s: String) -> Result<Vec<(u64, u64)>> {
let mut fin = Vec::new(); let mut fin = Vec::new();
+2 -14
View File
@@ -8,13 +8,12 @@
//! //!
//! See the Kernel's documentation for more information about this subsystem, found at: //! See the Kernel's documentation for more information about this subsystem, found at:
//! [Documentation/cgroup-v1/hugetlb.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/hugetlb.txt) //! [Documentation/cgroup-v1/hugetlb.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/hugetlb.txt)
use std::fs::File; use std::io::Write;
use std::io::{Read, Write};
use std::path::PathBuf; use std::path::PathBuf;
use crate::error::ErrorKind::*; use crate::error::ErrorKind::*;
use crate::error::*; use crate::error::*;
use crate::flat_keyed_to_vec; use crate::{flat_keyed_to_vec, read_u64_from};
use crate::{ use crate::{
ControllIdentifier, ControllerInternal, Controllers, HugePageResources, Resources, Subsystem, ControllIdentifier, ControllerInternal, Controllers, HugePageResources, Resources, Subsystem,
@@ -86,17 +85,6 @@ impl<'a> From<&'a Subsystem> for &'a HugeTlbController {
} }
} }
fn read_u64_from(mut file: File) -> Result<u64> {
let mut string = String::new();
match file.read_to_string(&mut string) {
Ok(_) => string
.trim()
.parse()
.map_err(|e| Error::with_cause(ParseError, e)),
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}
impl HugeTlbController { impl HugeTlbController {
/// Constructs a new `HugeTlbController` with `root` serving as the root of the control group. /// Constructs a new `HugeTlbController` with `root` serving as the root of the control group.
pub fn new(root: PathBuf, v2: bool) -> Self { pub fn new(root: PathBuf, v2: bool) -> Self {
+25 -3
View File
@@ -10,6 +10,7 @@ use std::collections::HashMap;
use std::fs::{self, File}; use std::fs::{self, File};
use std::io::{BufRead, BufReader, Read, Write}; use std::io::{BufRead, BufReader, Read, Write};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::str::FromStr;
macro_rules! update_and_test { macro_rules! update_and_test {
($self: ident, $set_func:ident, $value:expr, $get_func:ident) => { ($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
Ok(h) Ok(h)
} }
/// read and parse an i64 data fn read_from<T>(mut file: File) -> Result<T>
fn read_i64_from(mut file: File) -> Result<i64> { where
T: FromStr,
<T as FromStr>::Err: 'static + Send + Sync + std::error::Error,
{
let mut string = String::new(); let mut string = String::new();
match file.read_to_string(&mut string) { match file.read_to_string(&mut string) {
Ok(_) => string Ok(_) => string
.trim() .trim()
.parse() .parse::<T>()
.map_err(|e| Error::with_cause(ParseError, e)), .map_err(|e| Error::with_cause(ParseError, e)),
Err(e) => Err(Error::with_cause(ReadFailed, e)), Err(e) => Err(Error::with_cause(ReadFailed, e)),
} }
} }
fn read_string_from(mut file: File) -> Result<String> {
let mut string = String::new();
match file.read_to_string(&mut string) {
Ok(_) => Ok(string.trim().to_string()),
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}
/// read and parse an u64 data
fn read_u64_from(file: File) -> Result<u64> {
read_from::<u64>(file)
}
/// read and parse an i64 data
fn read_i64_from(file: File) -> Result<i64> {
read_from::<i64>(file)
}
+2 -22
View File
@@ -9,15 +9,14 @@
//! See the Kernel's documentation for more information about this subsystem, found at: //! See the Kernel's documentation for more information about this subsystem, found at:
//! [Documentation/cgroup-v1/memory.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt) //! [Documentation/cgroup-v1/memory.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt)
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::File; use std::io::Write;
use std::io::{Read, Write};
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::mpsc::Receiver; use std::sync::mpsc::Receiver;
use crate::error::ErrorKind::*; use crate::error::ErrorKind::*;
use crate::error::*; use crate::error::*;
use crate::events; use crate::events;
use crate::read_i64_from; use crate::{read_i64_from, read_string_from, read_u64_from};
use crate::flat_keyed_to_hashmap; use crate::flat_keyed_to_hashmap;
@@ -870,25 +869,6 @@ impl<'a> From<&'a Subsystem> for &'a MemController {
} }
} }
fn read_u64_from(mut file: File) -> Result<u64> {
let mut string = String::new();
match file.read_to_string(&mut string) {
Ok(_) => string
.trim()
.parse()
.map_err(|e| Error::with_cause(ParseError, e)),
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}
fn read_string_from(mut file: File) -> Result<String> {
let mut string = String::new();
match file.read_to_string(&mut string) {
Ok(_) => Ok(string.trim().to_string()),
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::memory::{ use crate::memory::{
+2 -13
View File
@@ -7,13 +7,13 @@
//! //!
//! See the Kernel's documentation for more information about this subsystem, found at: //! See the Kernel's documentation for more information about this subsystem, found at:
//! [Documentation/cgroup-v1/net_cls.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/net_cls.txt) //! [Documentation/cgroup-v1/net_cls.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/net_cls.txt)
use std::fs::File; use std::io::Write;
use std::io::{Read, Write};
use std::path::PathBuf; use std::path::PathBuf;
use crate::error::ErrorKind::*; use crate::error::ErrorKind::*;
use crate::error::*; use crate::error::*;
use crate::read_u64_from;
use crate::{ use crate::{
ControllIdentifier, ControllerInternal, Controllers, NetworkResources, Resources, Subsystem, ControllIdentifier, ControllerInternal, Controllers, NetworkResources, Resources, Subsystem,
}; };
@@ -74,17 +74,6 @@ impl<'a> From<&'a Subsystem> for &'a NetClsController {
} }
} }
fn read_u64_from(mut file: File) -> Result<u64> {
let mut string = String::new();
match file.read_to_string(&mut string) {
Ok(_) => string
.trim()
.parse()
.map_err(|e| Error::with_cause(ParseError, e)),
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}
impl NetClsController { impl NetClsController {
/// Constructs a new `NetClsController` with `root` serving as the root of the control group. /// Constructs a new `NetClsController` with `root` serving as the root of the control group.
pub fn new(root: PathBuf) -> Self { pub fn new(root: PathBuf) -> Self {
+2 -13
View File
@@ -8,13 +8,13 @@
//! See the Kernel's documentation for more information about this subsystem, found at: //! See the Kernel's documentation for more information about this subsystem, found at:
//! [Documentation/cgroup-v1/net_prio.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/net_prio.txt) //! [Documentation/cgroup-v1/net_prio.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/net_prio.txt)
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::File; use std::io::{BufRead, BufReader, Write};
use std::io::{BufRead, BufReader, Read, Write};
use std::path::PathBuf; use std::path::PathBuf;
use crate::error::ErrorKind::*; use crate::error::ErrorKind::*;
use crate::error::*; use crate::error::*;
use crate::read_u64_from;
use crate::{ use crate::{
ControllIdentifier, ControllerInternal, Controllers, NetworkResources, Resources, Subsystem, ControllIdentifier, ControllerInternal, Controllers, NetworkResources, Resources, Subsystem,
}; };
@@ -77,17 +77,6 @@ impl<'a> From<&'a Subsystem> for &'a NetPrioController {
} }
} }
fn read_u64_from(mut file: File) -> Result<u64> {
let mut string = String::new();
match file.read_to_string(&mut string) {
Ok(_) => string
.trim()
.parse()
.map_err(|e| Error::with_cause(ParseError, e)),
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}
impl NetPrioController { impl NetPrioController {
/// Constructs a new `NetPrioController` with `root` serving as the root of the control group. /// Constructs a new `NetPrioController` with `root` serving as the root of the control group.
pub fn new(root: PathBuf) -> Self { pub fn new(root: PathBuf) -> Self {
+1 -12
View File
@@ -8,13 +8,13 @@
//! //!
//! See the Kernel's documentation for more information about this subsystem, found at: //! See the Kernel's documentation for more information about this subsystem, found at:
//! [Documentation/cgroups-v1/pids.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/pids.txt) //! [Documentation/cgroups-v1/pids.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/pids.txt)
use std::fs::File;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::path::PathBuf; use std::path::PathBuf;
use crate::error::ErrorKind::*; use crate::error::ErrorKind::*;
use crate::error::*; use crate::error::*;
use crate::read_u64_from;
use crate::{ use crate::{
parse_max_value, ControllIdentifier, ControllerInternal, Controllers, MaxValue, PidResources, parse_max_value, ControllIdentifier, ControllerInternal, Controllers, MaxValue, PidResources,
Resources, Subsystem, Resources, Subsystem,
@@ -89,17 +89,6 @@ impl<'a> From<&'a Subsystem> for &'a PidController {
} }
} }
fn read_u64_from(mut file: File) -> Result<u64> {
let mut string = String::new();
match file.read_to_string(&mut string) {
Ok(_) => string
.trim()
.parse()
.map_err(|e| Error::with_cause(ParseError, e)),
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}
impl PidController { impl PidController {
/// Constructors a new `PidController` instance, with `root` serving as the controller's root /// Constructors a new `PidController` instance, with `root` serving as the controller's root
/// directory. /// directory.
+2 -10
View File
@@ -7,13 +7,13 @@
//! //!
//! See the Kernel's documentation for more information about this subsystem, found at: //! See the Kernel's documentation for more information about this subsystem, found at:
//! [Documentation/cgroup-v1/rdma.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/rdma.txt) //! [Documentation/cgroup-v1/rdma.txt](https://www.kernel.org/doc/Documentation/cgroup-v1/rdma.txt)
use std::fs::File; use std::io::Write;
use std::io::{Read, Write};
use std::path::PathBuf; use std::path::PathBuf;
use crate::error::ErrorKind::*; use crate::error::ErrorKind::*;
use crate::error::*; use crate::error::*;
use crate::read_string_from;
use crate::{ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem}; use crate::{ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem};
/// A controller that allows controlling the `rdma` subsystem of a Cgroup. /// A controller that allows controlling the `rdma` subsystem of a Cgroup.
@@ -66,14 +66,6 @@ impl<'a> From<&'a Subsystem> for &'a RdmaController {
} }
} }
fn read_string_from(mut file: File) -> Result<String> {
let mut string = String::new();
match file.read_to_string(&mut string) {
Ok(_) => Ok(string.trim().to_string()),
Err(e) => Err(Error::with_cause(ReadFailed, e)),
}
}
impl RdmaController { impl RdmaController {
/// Constructs a new `RdmaController` with `root` serving as the root of the control group. /// Constructs a new `RdmaController` with `root` serving as the root of the control group.
pub fn new(root: PathBuf) -> Self { pub fn new(root: PathBuf) -> Self {