Skip to content

Commit 775c84f

Browse files
committed
chore: fix aarch64 clippy lints
Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent ffb697b commit 775c84f

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

src/vmm/src/arch/aarch64/cache_info.rs

+30-31
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ pub(crate) enum CacheInfoError {
2323
MissingOptionalAttr(String, CacheEntry),
2424
}
2525

26-
struct CacheEngine {
27-
store: Box<dyn CacheStore>,
26+
struct CacheEngine<T: CacheStore = HostCacheStore> {
27+
store: T,
2828
}
2929

3030
trait CacheStore: std::fmt::Debug {
3131
fn get_by_key(&self, index: u8, file_name: &str) -> Result<String, CacheInfoError>;
3232
}
3333

3434
#[derive(Debug)]
35-
pub(crate) struct CacheEntry {
35+
pub struct CacheEntry {
3636
// Cache Level: 1, 2, 3..
3737
pub level: u8,
3838
// Type of cache: Unified, Data, Instruction.
@@ -45,17 +45,16 @@ pub(crate) struct CacheEntry {
4545
}
4646

4747
#[derive(Debug)]
48-
struct HostCacheStore {
48+
pub struct HostCacheStore {
4949
cache_dir: PathBuf,
5050
}
5151

52-
#[cfg(not(test))]
53-
impl Default for CacheEngine {
52+
impl Default for CacheEngine<HostCacheStore> {
5453
fn default() -> Self {
5554
CacheEngine {
56-
store: Box::new(HostCacheStore {
55+
store: HostCacheStore {
5756
cache_dir: PathBuf::from("/sys/devices/system/cpu/cpu0/cache"),
58-
}),
57+
},
5958
}
6059
}
6160
}
@@ -72,7 +71,7 @@ impl CacheStore for HostCacheStore {
7271
}
7372

7473
impl CacheEntry {
75-
fn from_index(index: u8, store: &dyn CacheStore) -> Result<CacheEntry, CacheInfoError> {
74+
fn from_index(index: u8, store: &impl CacheStore) -> Result<CacheEntry, CacheInfoError> {
7675
let mut err_str = String::new();
7776
let mut cache: CacheEntry = CacheEntry::default();
7877

@@ -287,10 +286,10 @@ pub(crate) fn read_cache_config(
287286
// Also without this mechanism we would be logging the warnings for each level which pollutes
288287
// a lot the logs.
289288
let mut logged_missing_attr = false;
290-
let engine = CacheEngine::default();
289+
let engine = CacheEngine::<HostCacheStore>::default();
291290

292291
for index in 0..=MAX_CACHE_LEVEL {
293-
match CacheEntry::from_index(index, engine.store.as_ref()) {
292+
match CacheEntry::from_index(index, &engine.store) {
294293
Ok(cache) => {
295294
append_cache_level(cache_l1, cache_non_l1, cache);
296295
}
@@ -326,22 +325,22 @@ mod tests {
326325
dummy_fs: HashMap<String, String>,
327326
}
328327

329-
impl Default for CacheEngine {
328+
impl Default for CacheEngine<MockCacheStore> {
330329
fn default() -> Self {
331330
CacheEngine {
332-
store: Box::new(MockCacheStore {
331+
store: MockCacheStore {
333332
dummy_fs: create_default_store(),
334-
}),
333+
},
335334
}
336335
}
337336
}
338337

339-
impl CacheEngine {
338+
impl CacheEngine<MockCacheStore> {
340339
fn new(map: &HashMap<String, String>) -> Self {
341340
CacheEngine {
342-
store: Box::new(MockCacheStore {
341+
store: MockCacheStore {
343342
dummy_fs: map.clone(),
344-
}),
343+
},
345344
}
346345
}
347346
}
@@ -425,12 +424,12 @@ mod tests {
425424
let mut map1 = default_map.clone();
426425
map1.remove("index0/type");
427426
let engine = CacheEngine::new(&map1);
428-
let res = CacheEntry::from_index(0, engine.store.as_ref());
427+
let res = CacheEntry::from_index(0, &engine.store);
429428
// We did create the level file but we still do not have the type file.
430429
assert!(matches!(res.unwrap_err(), CacheInfoError::MissingCacheType));
431430

432431
let engine = CacheEngine::new(&default_map);
433-
let res = CacheEntry::from_index(0, engine.store.as_ref());
432+
let res = CacheEntry::from_index(0, &engine.store);
434433
assert_eq!(
435434
format!("{}", res.unwrap_err()),
436435
"shared cpu map, coherency line size, size, number of sets",
@@ -440,15 +439,15 @@ mod tests {
440439
let mut map2 = default_map.clone();
441440
map2.insert("index0/level".to_string(), "d".to_string());
442441
let engine = CacheEngine::new(&map2);
443-
let res = CacheEntry::from_index(0, engine.store.as_ref());
442+
let res = CacheEntry::from_index(0, &engine.store);
444443
assert_eq!(
445444
format!("{}", res.unwrap_err()),
446445
"Invalid cache configuration found for level: invalid digit found in string"
447446
);
448447

449448
default_map.insert("index0/type".to_string(), "Instructionn".to_string());
450449
let engine = CacheEngine::new(&default_map);
451-
let res = CacheEntry::from_index(0, engine.store.as_ref());
450+
let res = CacheEntry::from_index(0, &engine.store);
452451
assert_eq!(
453452
format!("{}", res.unwrap_err()),
454453
"Invalid cache configuration found for type: Instructionn"
@@ -464,7 +463,7 @@ mod tests {
464463
"00000000,00000001".to_string(),
465464
);
466465
let engine = CacheEngine::new(&default_map);
467-
let res = CacheEntry::from_index(0, engine.store.as_ref());
466+
let res = CacheEntry::from_index(0, &engine.store);
468467
assert_eq!(
469468
format!("{}", res.unwrap_err()),
470469
"coherency line size, size, number of sets"
@@ -475,15 +474,15 @@ mod tests {
475474
"00000000,0000000G".to_string(),
476475
);
477476
let engine = CacheEngine::new(&default_map);
478-
let res = CacheEntry::from_index(0, engine.store.as_ref());
477+
let res = CacheEntry::from_index(0, &engine.store);
479478
assert_eq!(
480479
format!("{}", res.unwrap_err()),
481480
"Invalid cache configuration found for shared_cpu_map: invalid digit found in string"
482481
);
483482

484483
default_map.insert("index0/shared_cpu_map".to_string(), "00000000".to_string());
485484
let engine = CacheEngine::new(&default_map);
486-
let res = CacheEntry::from_index(0, engine.store.as_ref());
485+
let res = CacheEntry::from_index(0, &engine.store);
487486
assert_eq!(
488487
format!("{}", res.unwrap_err()),
489488
"Invalid cache configuration found for shared_cpu_map: 00000000"
@@ -496,7 +495,7 @@ mod tests {
496495

497496
default_map.insert("index0/coherency_line_size".to_string(), "64".to_string());
498497
let engine = CacheEngine::new(&default_map);
499-
let res = CacheEntry::from_index(0, engine.store.as_ref());
498+
let res = CacheEntry::from_index(0, &engine.store);
500499
assert_eq!(
501500
"shared cpu map, size, number of sets",
502501
format!("{}", res.unwrap_err())
@@ -507,7 +506,7 @@ mod tests {
507506
"Instruction".to_string(),
508507
);
509508
let engine = CacheEngine::new(&default_map);
510-
let res = CacheEntry::from_index(0, engine.store.as_ref());
509+
let res = CacheEntry::from_index(0, &engine.store);
511510
assert_eq!(
512511
format!("{}", res.unwrap_err()),
513512
"Invalid cache configuration found for coherency_line_size: invalid digit found in \
@@ -521,23 +520,23 @@ mod tests {
521520

522521
default_map.insert("index0/size".to_string(), "64K".to_string());
523522
let engine = CacheEngine::new(&default_map);
524-
let res = CacheEntry::from_index(0, engine.store.as_ref());
523+
let res = CacheEntry::from_index(0, &engine.store);
525524
assert_eq!(
526525
format!("{}", res.unwrap_err()),
527526
"shared cpu map, coherency line size, number of sets",
528527
);
529528

530529
default_map.insert("index0/size".to_string(), "64".to_string());
531530
let engine = CacheEngine::new(&default_map);
532-
let res = CacheEntry::from_index(0, engine.store.as_ref());
531+
let res = CacheEntry::from_index(0, &engine.store);
533532
assert_eq!(
534533
format!("{}", res.unwrap_err()),
535534
"Invalid cache configuration found for size: 64"
536535
);
537536

538537
default_map.insert("index0/size".to_string(), "64Z".to_string());
539538
let engine = CacheEngine::new(&default_map);
540-
let res = CacheEntry::from_index(0, engine.store.as_ref());
539+
let res = CacheEntry::from_index(0, &engine.store);
541540
assert_eq!(
542541
format!("{}", res.unwrap_err()),
543542
"Invalid cache configuration found for size: 64Z"
@@ -550,15 +549,15 @@ mod tests {
550549

551550
default_map.insert("index0/number_of_sets".to_string(), "64".to_string());
552551
let engine = CacheEngine::new(&default_map);
553-
let res = CacheEntry::from_index(0, engine.store.as_ref());
552+
let res = CacheEntry::from_index(0, &engine.store);
554553
assert_eq!(
555554
"shared cpu map, coherency line size, size",
556555
format!("{}", res.unwrap_err())
557556
);
558557

559558
default_map.insert("index0/number_of_sets".to_string(), "64K".to_string());
560559
let engine = CacheEngine::new(&default_map);
561-
let res = CacheEntry::from_index(0, engine.store.as_ref());
560+
let res = CacheEntry::from_index(0, &engine.store);
562561
assert_eq!(
563562
format!("{}", res.unwrap_err()),
564563
"Invalid cache configuration found for number_of_sets: invalid digit found in string"

0 commit comments

Comments
 (0)