Skip to content

Commit d338cf8

Browse files
Merge pull request #166 from Apokleos/add-mut-getters
Add get_mut for fields of spec/runtime
2 parents 4cf1c3d + e90458f commit d338cf8

File tree

9 files changed

+163
-77
lines changed

9 files changed

+163
-77
lines changed

.rustfmt.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ reorder_imports = true
1414
reorder_modules = true
1515
remove_nested_parens = true
1616
match_arm_leading_pipes = "Never"
17-
fn_args_layout = "Tall"
17+
fn_params_layout = "Tall"
1818
edition = "2018"
1919
merge_derives = true
2020
use_try_shorthand = false

src/image/config.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer}
1010
use std::collections::BTreeMap;
1111
use std::{
1212
collections::HashMap,
13+
fmt::Display,
1314
io::{Read, Write},
1415
path::Path,
1516
};
@@ -250,14 +251,19 @@ impl ImageConfiguration {
250251
}
251252
}
252253

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

src/image/index.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use getset::{CopyGetters, Getters, Setters};
88
use serde::{Deserialize, Serialize};
99
use std::{
1010
collections::HashMap,
11+
fmt::Display,
1112
io::{Read, Write},
1213
path::Path,
1314
};
@@ -210,14 +211,19 @@ impl Default for ImageIndex {
210211
}
211212
}
212213

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

src/image/manifest.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use getset::{CopyGetters, Getters, MutGetters, Setters};
88
use serde::{Deserialize, Serialize};
99
use std::{
1010
collections::HashMap,
11+
fmt::Display,
1112
io::{Read, Write},
1213
path::Path,
1314
};
@@ -221,14 +222,19 @@ impl ImageManifest {
221222
}
222223
}
223224

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

src/runtime/hooks.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
use crate::error::OciSpecError;
22
use derive_builder::Builder;
3-
use getset::{CopyGetters, Getters, Setters};
3+
use getset::{CopyGetters, Getters, MutGetters, Setters};
44
use serde::{Deserialize, Serialize};
55
use std::path::PathBuf;
66

77
#[derive(
8-
Builder, Clone, Debug, Default, Deserialize, Eq, Getters, Setters, PartialEq, Serialize,
8+
Builder,
9+
Clone,
10+
Debug,
11+
Default,
12+
Deserialize,
13+
Eq,
14+
MutGetters,
15+
Getters,
16+
Setters,
17+
PartialEq,
18+
Serialize,
919
)]
1020
#[serde(rename_all = "camelCase")]
1121
#[builder(
@@ -14,7 +24,7 @@ use std::path::PathBuf;
1424
setter(into, strip_option),
1525
build_fn(error = "OciSpecError")
1626
)]
17-
#[getset(get = "pub", set = "pub")]
27+
#[getset(get_mut = "pub", get = "pub", set = "pub")]
1828
/// Hooks specifies a command that is run in the container at a particular
1929
/// event in the lifecycle (setup and teardown) of a container.
2030
pub struct Hooks {
@@ -75,6 +85,7 @@ pub struct Hooks {
7585
Deserialize,
7686
Eq,
7787
Getters,
88+
MutGetters,
7889
Setters,
7990
PartialEq,
8091
Serialize,
@@ -88,28 +99,28 @@ pub struct Hooks {
8899
/// Hook specifies a command that is run at a particular event in the
89100
/// lifecycle of a container.
90101
pub struct Hook {
91-
#[getset(get = "pub", set = "pub")]
102+
#[getset(get_mut = "pub", get = "pub", set = "pub")]
92103
/// Path to the binary to be executed. Following similar semantics to
93104
/// [IEEE Std 1003.1-2008 `execv`'s path](https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html). This
94105
/// specification extends the IEEE standard in that path MUST be
95106
/// absolute.
96107
path: PathBuf,
97108

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

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

111122
#[serde(default, skip_serializing_if = "Option::is_none")]
112-
#[getset(get_copy = "pub", set = "pub")]
123+
#[getset(get_mut = "pub", get_copy = "pub", set = "pub")]
113124
/// Timeout is the number of seconds before aborting the hook. If set,
114125
/// timeout MUST be greater than zero.
115126
timeout: Option<i64>,

0 commit comments

Comments
 (0)