Skip to content

Commit fba5677

Browse files
authored
Merge pull request #39 from zvonkok/rename-with-auto-refresh
Update version.rs segfault and WithAutoRefresh rename
2 parents 5760006 + d325c31 commit fba5677

File tree

4 files changed

+25
-31
lines changed

4 files changed

+25
-31
lines changed

Diff for: src/cache.rs

+17-18
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,20 @@ impl fmt::Display for ConflictError {
4848

4949
impl Error for ConflictError {}
5050

51-
// CacheOption is an option to change some aspect of default CDI behavior.
52-
pub trait CacheOption {
53-
fn apply(&self, cache: &mut Cache);
54-
}
51+
// CdiOption is an option to change some aspect of default CDI behavior.
52+
// We define the CdiOption type using a type alias, which is a Box<dyn FnOnce(&mut Cache)>.
53+
// This means that CdiOption is a trait object that represents a one-time closure that takes a &mut Cache parameter.
54+
pub type CdiOption = Box<dyn FnOnce(&mut Cache)>;
5555

56-
// WithAutoRefresh returns an option to control automatic Cache refresh.
56+
// with_auto_refresh returns an option to control automatic Cache refresh.
5757
// By default auto-refresh is enabled, the list of Spec directories are
5858
// monitored and the Cache is automatically refreshed whenever a change
5959
// is detected. This option can be used to disable this behavior when a
6060
// manually refreshed mode is preferable.
61-
pub struct WithAutoRefresh(pub bool);
62-
63-
impl CacheOption for WithAutoRefresh {
64-
fn apply(&self, cache: &mut Cache) {
65-
cache.auto_refresh = self.0;
66-
}
61+
pub fn with_auto_refresh(auto_refresh: bool) -> CdiOption {
62+
Box::new(move |c: &mut Cache| {
63+
c.auto_refresh = auto_refresh;
64+
})
6765
}
6866

6967
#[allow(dead_code)]
@@ -79,7 +77,7 @@ pub struct Cache {
7977
//watch: Watch,
8078
}
8179

82-
pub fn new_cache(options: Vec<Arc<dyn CacheOption>>) -> Arc<Mutex<Cache>> {
80+
pub fn new_cache(options: Vec<CdiOption>) -> Arc<Mutex<Cache>> {
8381
let cache = Arc::new(Mutex::new(Cache::default()));
8482

8583
{
@@ -110,9 +108,9 @@ impl Cache {
110108
}
111109
}
112110

113-
pub fn configure(&mut self, options: Vec<Arc<dyn CacheOption>>) {
111+
pub fn configure(&mut self, options: Vec<CdiOption>) {
114112
for option in options {
115-
option.apply(self);
113+
option(self);
116114
}
117115
}
118116

@@ -268,10 +266,11 @@ impl Cache {
268266
if let Some(dev) = self.devices.get(&device) {
269267
let mut spec = dev.get_spec();
270268
if specs.insert(spec.clone()) {
271-
match spec.edits() {
272-
Some(ce) => edits.append(ce)?,
273-
None => continue,
274-
};
269+
// spec.edits may be none when we only have dev.edits
270+
// allow dev.edits to be added even if spec.edits is None
271+
if let Some(ce) = spec.edits() {
272+
edits.append(ce)?
273+
}
275274
}
276275
edits.append(dev.edits())?;
277276
} else {

Diff for: src/default_cache.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ use std::sync::{Arc, Mutex};
66
use oci_spec::runtime::Spec;
77
use once_cell::sync::OnceCell;
88

9-
use crate::cache::{new_cache, Cache, CacheOption, WithAutoRefresh};
9+
use crate::cache::{new_cache, with_auto_refresh, Cache, CdiOption};
1010

11-
fn get_or_create_default_cache(_options: Vec<Arc<dyn CacheOption>>) -> Arc<Mutex<Cache>> {
11+
fn get_or_create_default_cache(_options: &[CdiOption]) -> Arc<Mutex<Cache>> {
1212
let mut cache: OnceCell<Arc<Mutex<Cache>>> = OnceCell::new();
1313
cache.get_or_init(|| {
14-
let options: Vec<Arc<dyn CacheOption>> = vec![Arc::new(WithAutoRefresh(true))];
14+
let options: Vec<CdiOption> = vec![with_auto_refresh(true)];
1515
new_cache(options)
1616
});
1717
cache.take().unwrap()
1818
}
1919

2020
pub fn get_default_cache() -> Arc<Mutex<Cache>> {
21-
get_or_create_default_cache(vec![])
21+
get_or_create_default_cache(vec![].as_ref())
2222
}
2323

24-
pub fn configure(options: Vec<Arc<dyn CacheOption>>) -> Result<()> {
25-
let cache = get_or_create_default_cache(options.clone());
24+
pub fn configure(options: Vec<CdiOption>) -> Result<()> {
25+
let cache = get_or_create_default_cache(&options);
2626
let mut cache = cache.lock().unwrap();
2727
if options.is_empty() {
2828
return Ok(());

Diff for: src/spec_dirs.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use lazy_static::lazy_static;
99
use path_clean::clean;
1010

1111
use crate::{
12-
cache::Cache,
12+
cache::{Cache, CdiOption},
1313
spec::{read_spec, Spec},
1414
utils::is_cdi_spec,
1515
};
@@ -32,11 +32,6 @@ lazy_static! {
3232
];
3333
}
3434

35-
// CdiOption is an option to change some aspect of default CDI behavior.
36-
// We define the CdiOption type using a type alias, which is a Box<dyn FnOnce(&mut Cache)>.
37-
// This means that CdiOption is a trait object that represents a one-time closure that takes a &mut Cache parameter.
38-
type CdiOption = Box<dyn FnOnce(&mut Cache)>;
39-
4035
#[derive(Debug)]
4136
pub struct SpecError {
4237
message: String,

Diff for: src/version.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ fn requires_v040(spec: &CDISpec) -> bool {
165165
spec.devices
166166
.iter()
167167
.map(|d| &d.container_edits)
168-
.chain(std::iter::once(spec.container_edits.as_ref().unwrap()))
168+
.chain(spec.container_edits.as_ref())
169169
.flat_map(|edits| edits.mounts.iter().flat_map(|mounts| mounts.iter()))
170170
.any(|mount| mount.r#type.as_ref().map_or(false, |typ| !typ.is_empty()))
171171
}

0 commit comments

Comments
 (0)