mirror of
https://github.com/kata-containers/cgroups-rs.git
synced 2026-08-05 02:13:23 +00:00
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:
@@ -9,15 +9,14 @@
|
||||
//! 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)
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::mpsc::Receiver;
|
||||
|
||||
use crate::error::ErrorKind::*;
|
||||
use crate::error::*;
|
||||
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;
|
||||
|
||||
@@ -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)]
|
||||
mod tests {
|
||||
use crate::memory::{
|
||||
|
||||
Reference in New Issue
Block a user