refactor: replace iter() to into_iter(), and remove useless .iters()'s

This commit is contained in:
Jack Wills
2022-10-07 02:43:04 +00:00
parent 5660b34d51
commit a77f690a49
4 changed files with 30 additions and 24 deletions
+15 -9
View File
@@ -259,10 +259,10 @@ impl AppData {
/// Get the title for log panel for selected container
/// will be "logs x/x"
pub fn get_log_title(&self) -> String {
self.get_selected_log_index().map_or(
"".to_owned(),
|index| self.containers.items[index].logs.get_state_title(),
)
self.get_selected_log_index()
.map_or("".to_owned(), |index| {
self.containers.items[index].logs.get_state_title()
})
}
/// select next selected log line
@@ -301,6 +301,8 @@ impl AppData {
}
}
/// Check if the initial parsing has been completed, by making sure that all ids given (which are running) have a non empty cpu_stats vecdec
pub fn initialised(&mut self, all_ids: &[(bool, ContainerId)]) -> bool {
let count_is_running = all_ids.iter().filter(|i| i.0).count();
let number_with_cpu_status = self
@@ -382,7 +384,7 @@ impl AppData {
.collect::<Vec<_>>()
}
/// find container given id
/// return a mutable container by given id
fn get_container_by_id(&mut self, id: &ContainerId) -> Option<&mut ContainerItem> {
self.containers.items.iter_mut().find(|i| &i.id == id)
}
@@ -443,8 +445,10 @@ impl AppData {
}
}
}
// Trim a &String and return String
let trim_owned = |x: &String| x.trim().to_owned();
for i in all_containers.iter_mut() {
for i in all_containers {
if let Some(id) = i.id.as_ref() {
let name = i.names.as_mut().map_or("".to_owned(), |names| {
names.first_mut().map_or("".to_owned(), |f| {
@@ -458,12 +462,12 @@ impl AppData {
let state = State::from(
i.state
.as_ref()
.map_or("dead".to_owned(), |f| f.trim().to_owned()),
.map_or("dead".to_owned(), trim_owned),
);
let status = i
.status
.as_ref()
.map_or("".to_owned(), |f| f.trim().to_owned());
.map_or("".to_owned(), trim_owned);
let image = i
.image
@@ -512,7 +516,7 @@ impl AppData {
container.last_updated = tz;
let current_len = container.logs.items.len();
for i in output.iter() {
for i in output {
let lines = if color {
log_sanitizer::colorize_logs(i)
} else if raw {
@@ -523,6 +527,8 @@ impl AppData {
container.logs.items.push(ListItem::new(lines));
}
// Set the logs selected row for each container
// Either when no long currently selected, or currently selected (before updated) is already at end
if container.logs.state.selected().is_none()
|| container.logs.state.selected().map_or(1, |f| f + 1) == current_len
{
+7 -7
View File
@@ -151,7 +151,7 @@ impl DockerData {
/// Update all stats, spawn each container into own tokio::spawn thread
fn update_all_container_stats(&mut self, all_ids: &[(bool, ContainerId)]) {
for (is_running, id) in all_ids.iter() {
for (is_running, id) in all_ids {
let docker = Arc::clone(&self.docker);
let app_data = Arc::clone(&self.app_data);
let spawns = Arc::clone(&self.spawns);
@@ -185,13 +185,13 @@ impl DockerData {
.unwrap_or_default();
let mut output = containers
.iter()
.into_iter()
.filter_map(|f| match f.id {
Some(_) => {
if f.command.as_ref().map_or(false, |c| c.contains("oxker")) {
None
} else {
Some(f.clone())
Some(f)
}
}
None => None,
@@ -205,13 +205,13 @@ impl DockerData {
// Just get the containers that are currently running, or being restarted, no point updating info on paused or dead containers
output
.iter()
.into_iter()
.filter_map(|i| {
i.id.as_ref().map(|id| {
i.id.map(|id| {
(
i.state == Some("running".to_owned())
|| i.state == Some("restarting".to_owned()),
ContainerId::from(id.as_str()),
ContainerId::from(id),
)
})
})
@@ -253,7 +253,7 @@ impl DockerData {
/// Update all logs, spawn each container into own tokio::spawn thread
fn init_all_logs(&mut self, all_ids: &[(bool, ContainerId)]) {
for (_, id) in all_ids.iter() {
for (_, id) in all_ids {
let docker = Arc::clone(&self.docker);
let app_data = Arc::clone(&self.app_data);
let spawns = Arc::clone(&self.spawns);