rustjail: use Iterator to manipulate vector elements

Use Iterator can save codes, and make code more readable

Signed-off-by: bin liu <bin@hyper.sh>
This commit is contained in:
bin liu 2020-10-12 06:08:27 +00:00
parent dc1442c33a
commit 6b9f99156e

View File

@ -387,7 +387,7 @@ fn do_init_child(cwfd: RawFd) -> Result<()> {
let linux = spec.linux.as_ref().unwrap(); let linux = spec.linux.as_ref().unwrap();
// get namespace vector to join/new // get namespace vector to join/new
let nses = get_namespaces(&linux)?; let nses = get_namespaces(&linux);
let mut userns = false; let mut userns = false;
let mut to_new = CloneFlags::empty(); let mut to_new = CloneFlags::empty();
@ -1140,24 +1140,21 @@ fn get_pid_namespace(logger: &Logger, linux: &Linux) -> Result<Option<RawFd>> {
} }
fn is_userns_enabled(linux: &Linux) -> bool { fn is_userns_enabled(linux: &Linux) -> bool {
for ns in &linux.namespaces { linux
if ns.r#type == "user" && ns.path == "" { .namespaces
return true; .iter()
} .any(|ns| ns.r#type == "user" && ns.path == "")
}
false
} }
fn get_namespaces(linux: &Linux) -> Result<Vec<LinuxNamespace>> { fn get_namespaces(linux: &Linux) -> Vec<LinuxNamespace> {
let mut ns: Vec<LinuxNamespace> = Vec::new(); linux
for i in &linux.namespaces { .namespaces
ns.push(LinuxNamespace { .iter()
r#type: i.r#type.clone(), .map(|ns| LinuxNamespace {
path: i.path.clone(), r#type: ns.r#type.clone(),
}); path: ns.path.clone(),
} })
Ok(ns) .collect()
} }
fn join_namespaces( fn join_namespaces(
@ -1270,15 +1267,12 @@ fn join_namespaces(
} }
fn write_mappings(logger: &Logger, path: &str, maps: &[LinuxIDMapping]) -> Result<()> { fn write_mappings(logger: &Logger, path: &str, maps: &[LinuxIDMapping]) -> Result<()> {
let mut data = String::new(); let data = maps
for m in maps { .iter()
if m.size == 0 { .filter(|m| m.size != 0)
continue; .map(|m| format!("{} {} {}\n", m.container_id, m.host_id, m.size))
} .collect::<Vec<_>>()
.join("");
let val = format!("{} {} {}\n", m.container_id, m.host_id, m.size);
data = data + &val;
}
info!(logger, "mapping: {}", data); info!(logger, "mapping: {}", data);
if !data.is_empty() { if !data.is_empty() {