@@ -48,22 +48,20 @@ impl fmt::Display for ConflictError {
48
48
49
49
impl Error for ConflictError { }
50
50
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 ) > ;
55
55
56
- // WithAutoRefresh returns an option to control automatic Cache refresh.
56
+ // with_auto_refresh returns an option to control automatic Cache refresh.
57
57
// By default auto-refresh is enabled, the list of Spec directories are
58
58
// monitored and the Cache is automatically refreshed whenever a change
59
59
// is detected. This option can be used to disable this behavior when a
60
60
// 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
+ } )
67
65
}
68
66
69
67
#[ allow( dead_code) ]
@@ -79,7 +77,7 @@ pub struct Cache {
79
77
//watch: Watch,
80
78
}
81
79
82
- pub fn new_cache ( options : Vec < Arc < dyn CacheOption > > ) -> Arc < Mutex < Cache > > {
80
+ pub fn new_cache ( options : Vec < CdiOption > ) -> Arc < Mutex < Cache > > {
83
81
let cache = Arc :: new ( Mutex :: new ( Cache :: default ( ) ) ) ;
84
82
85
83
{
@@ -110,9 +108,9 @@ impl Cache {
110
108
}
111
109
}
112
110
113
- pub fn configure ( & mut self , options : Vec < Arc < dyn CacheOption > > ) {
111
+ pub fn configure ( & mut self , options : Vec < CdiOption > ) {
114
112
for option in options {
115
- option. apply ( self ) ;
113
+ option ( self ) ;
116
114
}
117
115
}
118
116
@@ -268,10 +266,11 @@ impl Cache {
268
266
if let Some ( dev) = self . devices . get ( & device) {
269
267
let mut spec = dev. get_spec ( ) ;
270
268
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
+ }
275
274
}
276
275
edits. append ( dev. edits ( ) ) ?;
277
276
} else {
0 commit comments