Skip to content

Commit

Permalink
Merge pull request #166 from Apokleos/add-mut-getters
Browse files Browse the repository at this point in the history
Add get_mut for fields of spec/runtime
  • Loading branch information
saschagrunert authored May 28, 2024
2 parents 4cf1c3d + e90458f commit d338cf8
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 77 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
25 changes: 18 additions & 7 deletions src/runtime/hooks.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
use crate::error::OciSpecError;
use derive_builder::Builder;
use getset::{CopyGetters, Getters, Setters};
use getset::{CopyGetters, Getters, MutGetters, Setters};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

#[derive(
Builder, Clone, Debug, Default, Deserialize, Eq, Getters, Setters, PartialEq, Serialize,
Builder,
Clone,
Debug,
Default,
Deserialize,
Eq,
MutGetters,
Getters,
Setters,
PartialEq,
Serialize,
)]
#[serde(rename_all = "camelCase")]
#[builder(
Expand All @@ -14,7 +24,7 @@ use std::path::PathBuf;
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
#[getset(get = "pub", set = "pub")]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
/// Hooks specifies a command that is run in the container at a particular
/// event in the lifecycle (setup and teardown) of a container.
pub struct Hooks {
Expand Down Expand Up @@ -75,6 +85,7 @@ pub struct Hooks {
Deserialize,
Eq,
Getters,
MutGetters,
Setters,
PartialEq,
Serialize,
Expand All @@ -88,28 +99,28 @@ pub struct Hooks {
/// Hook specifies a command that is run at a particular event in the
/// lifecycle of a container.
pub struct Hook {
#[getset(get = "pub", set = "pub")]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
/// Path to the binary to be executed. Following similar semantics to
/// [IEEE Std 1003.1-2008 `execv`'s path](https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html). This
/// specification extends the IEEE standard in that path MUST be
/// absolute.
path: PathBuf,

#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get = "pub", set = "pub")]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
/// Arguments used for the binary, including the binary name itself.
/// Following the same semantics as [IEEE Std 1003.1-2008
/// `execv`'s argv](https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html).
args: Option<Vec<String>>,

#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get = "pub", set = "pub")]
#[getset(get_mut = "pub", get = "pub", set = "pub")]
/// Additional `key=value` environment variables. Following the same
/// semantics as [IEEE Std 1003.1-2008's `environ`](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_01).
env: Option<Vec<String>>,

#[serde(default, skip_serializing_if = "Option::is_none")]
#[getset(get_copy = "pub", set = "pub")]
#[getset(get_mut = "pub", get_copy = "pub", set = "pub")]
/// Timeout is the number of seconds before aborting the hook. If set,
/// timeout MUST be greater than zero.
timeout: Option<i64>,
Expand Down
Loading

0 comments on commit d338cf8

Please sign in to comment.