Skip to content

Commit

Permalink
Implement Display Trait instead of ToString Trait
Browse files Browse the repository at this point in the history
This ToString trait is automatically implemented for any type
which implements the Display trait. As such, ToString shouldn’t
be implemented directly: Display should be implemented instead,
and you get the ToString implementation for free. With such fixing,
It will make CI with lint-clippy happy.

Fixes #165

Signed-off-by: Alex Lyn <[email protected]>
  • Loading branch information
Apokleos committed May 27, 2024
1 parent 54d83eb commit e90458f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ reorder_imports = true
reorder_modules = true
remove_nested_parens = true
match_arm_leading_pipes = "Never"
fn_args_layout = "Tall"
fn_params_layout = "Tall"
edition = "2018"
merge_derives = true
use_try_shorthand = false
Expand Down
18 changes: 12 additions & 6 deletions src/image/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer}
use std::collections::BTreeMap;
use std::{
collections::HashMap,
fmt::Display,
io::{Read, Write},
path::Path,
};
Expand Down Expand Up @@ -250,14 +251,19 @@ impl ImageConfiguration {
}
}

/// Implement `ToString` directly since we cannot avoid twice memory allocation
/// when using auto-implementaion through `Display`.
impl ToString for ImageConfiguration {
fn to_string(&self) -> String {
/// This ToString trait is automatically implemented for any type which implements the Display trait.
/// As such, ToString shouldn’t be implemented directly: Display should be implemented instead,
/// and you get the ToString implementation for free.
impl Display for ImageConfiguration {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// Serde seralization never fails since this is
// a combination of String and enums.
self.to_string_pretty()
.expect("ImageConfiguration JSON convertion failed")
write!(
f,
"{}",
self.to_string_pretty()
.expect("ImageConfiguration JSON convertion failed")
)
}
}

Expand Down
18 changes: 12 additions & 6 deletions src/image/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use getset::{CopyGetters, Getters, Setters};
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
fmt::Display,
io::{Read, Write},
path::Path,
};
Expand Down Expand Up @@ -210,14 +211,19 @@ impl Default for ImageIndex {
}
}

/// Implement `ToString` directly since we cannot avoid twice memory allocation
/// when using auto-implementaion through `Display`.
impl ToString for ImageIndex {
fn to_string(&self) -> String {
/// This ToString trait is automatically implemented for any type which implements the Display trait.
/// As such, ToString shouldn’t be implemented directly: Display should be implemented instead,
/// and you get the ToString implementation for free.
impl Display for ImageIndex {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// Serde seralization never fails since this is
// a combination of String and enums.
self.to_string_pretty()
.expect("ImageIndex to JSON convertion failed")
write!(
f,
"{}",
self.to_string_pretty()
.expect("ImageIndex to JSON convertion failed")
)
}
}

Expand Down
18 changes: 12 additions & 6 deletions src/image/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use getset::{CopyGetters, Getters, MutGetters, Setters};
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
fmt::Display,
io::{Read, Write},
path::Path,
};
Expand Down Expand Up @@ -221,14 +222,19 @@ impl ImageManifest {
}
}

/// Implement `ToString` directly since we cannot avoid twice memory allocation
/// when using auto-implementaion through `Display`.
impl ToString for ImageManifest {
fn to_string(&self) -> String {
/// This ToString trait is automatically implemented for any type which implements the Display trait.
/// As such, ToString shouldn’t be implemented directly: Display should be implemented instead,
/// and you get the ToString implementation for free.
impl Display for ImageManifest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// Serde seralization never fails since this is
// a combination of String and enums.
self.to_string_pretty()
.expect("ImageManifest to JSON convertion failed")
write!(
f,
"{}",
self.to_string_pretty()
.expect("ImageManifest to JSON convertion failed")
)
}
}

Expand Down
23 changes: 16 additions & 7 deletions src/runtime/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::error::{oci_error, OciSpecError};
use derive_builder::Builder;
use getset::{CopyGetters, Getters, MutGetters, Setters};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, path::PathBuf, vec};
use std::{collections::HashMap, fmt::Display, path::PathBuf, vec};

#[derive(
Builder, Clone, Debug, Deserialize, Eq, Getters, MutGetters, Setters, PartialEq, Serialize,
Expand Down Expand Up @@ -282,8 +282,11 @@ pub struct LinuxDeviceCgroup {
access: Option<String>,
}

impl ToString for LinuxDeviceCgroup {
fn to_string(&self) -> String {
/// This ToString trait is automatically implemented for any type which implements the Display trait.
/// As such, ToString shouldn’t be implemented directly: Display should be implemented instead,
/// and you get the ToString implementation for free.
impl Display for LinuxDeviceCgroup {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let major = self
.major
.map(|mj| mj.to_string())
Expand All @@ -293,7 +296,8 @@ impl ToString for LinuxDeviceCgroup {
.map(|mi| mi.to_string())
.unwrap_or_else(|| "*".to_string());
let access = self.access.as_deref().unwrap_or("");
format!(
write!(
f,
"{} {}:{} {}",
&self.typ.unwrap_or_default().as_str(),
&major,
Expand Down Expand Up @@ -644,9 +648,14 @@ pub struct LinuxInterfacePriority {
priority: u32,
}

impl ToString for LinuxInterfacePriority {
fn to_string(&self) -> String {
format!("{} {}\n", self.name, self.priority)
/// This ToString trait is automatically implemented for any type which implements the Display trait.
/// As such, ToString shouldn’t be implemented directly: Display should be implemented instead,
/// and you get the ToString implementation for free.
impl Display for LinuxInterfacePriority {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// Serde seralization never fails since this is
// a combination of String and enums.
writeln!(f, "{} {}", self.name, self.priority)
}
}

Expand Down

0 comments on commit e90458f

Please sign in to comment.