From 8ae2d0c4c36cde3c00073fa8d7252df5785aec18 Mon Sep 17 00:00:00 2001 From: SKTT1Ryze Date: Fri, 16 Jul 2021 12:48:06 +0800 Subject: [PATCH] cargo fmt for async-fat32 --- async-fat32/src/block_cache.rs | 35 ++++++++++++++------------- async-fat32/src/bs_bpb.rs | 6 ++--- async-fat32/src/cache.rs | 44 +++++++++++++++++++--------------- async-fat32/src/config.rs | 2 +- async-fat32/src/lib.rs | 4 ++-- 5 files changed, 50 insertions(+), 41 deletions(-) diff --git a/async-fat32/src/block_cache.rs b/async-fat32/src/block_cache.rs index db6557c..b197b30 100644 --- a/async-fat32/src/block_cache.rs +++ b/async-fat32/src/block_cache.rs @@ -1,48 +1,51 @@ //! 块缓冲层实现 -use core::mem::MaybeUninit; -use alloc::sync::Arc; -use spin::Mutex; -use crate::AsyncBlockDevive; -use super::cache::{Cache, Node, LFUCache}; +use super::cache::{Cache, LFUCache, Node}; use super::config::*; +use crate::AsyncBlockDevive; +use alloc::sync::Arc; +use core::mem::MaybeUninit; +use spin::Mutex; // B: 一个块中的字节数 // N: 块缓冲层的块数 pub struct BlockCache, const B: usize, const N: usize> { block_device: Arc, - cache: Mutex + cache: Mutex, } impl BlockCache, BLOCK_SIZE, CACHE_SIZE> { pub fn init(device: Arc) -> Self { - let mut data: [MaybeUninit>; CACHE_SIZE] = unsafe { - MaybeUninit::uninit().assume_init() - }; + let mut data: [MaybeUninit>; CACHE_SIZE] = + unsafe { MaybeUninit::uninit().assume_init() }; for elem in &mut data[..] { *elem = MaybeUninit::new(Node::new(0, [0; BLOCK_SIZE])); } - let nodes = unsafe { core::mem::transmute::<_, [Node; CACHE_SIZE]>(data) }; + let nodes = + unsafe { core::mem::transmute::<_, [Node; CACHE_SIZE]>(data) }; let lfu_cache = LFUCache::empty(nodes); Self { block_device: device, - cache: Mutex::new(lfu_cache) + cache: Mutex::new(lfu_cache), } } pub async fn read_block(&self, block_id: usize) -> [u8; BLOCK_SIZE] { - { // 申请锁 + { + // 申请锁 let mut s = self.cache.lock(); - if let Some(block) = s.get(&block_id) { // 如果想要读取的块在缓冲层中,则读出来直接返回,不用读写块设备 + if let Some(block) = s.get(&block_id) { + // 如果想要读取的块在缓冲层中,则读出来直接返回,不用读写块设备 return block; } } // 释放锁 - // 如果要读取的块不在缓冲层中,则需要从块设备读取 + // 如果要读取的块不在缓冲层中,则需要从块设备读取 let mut data = [0; BLOCK_SIZE]; self.block_device.read(block_id, &mut data).await; // 将读取到的块写入到缓冲层 let mut s = self.cache.lock(); // 申请锁 let write_back = s.put(&block_id, data.clone()); drop(s); // 释放锁 - if let Some((id, mut block)) = write_back { // 如果有需要写回到块设备的数据,这里写回 + if let Some((id, mut block)) = write_back { + // 如果有需要写回到块设备的数据,这里写回 self.block_device.write(id, &mut block).await; } data @@ -73,4 +76,4 @@ impl BlockCache, BLOCK_SIZE, CACHE // lazy_static::lazy_static! { // pub static ref AsyncBlockCache: BlockCache, BLOCK_SIZE, CACHE_SIZE> = BlockCache::init(Arc::new(TestDevice::default())); -// } \ No newline at end of file +// } diff --git a/async-fat32/src/bs_bpb.rs b/async-fat32/src/bs_bpb.rs index cd7f4a0..ecef5b6 100644 --- a/async-fat32/src/bs_bpb.rs +++ b/async-fat32/src/bs_bpb.rs @@ -17,7 +17,7 @@ enum BootSectorOffset { /// Volume label VolLab = 71, /// One of the strings "FAT12", "FAT16", "FAT32" - FilSysType = 82 + FilSysType = 82, } /// BPB 各字段的偏移 @@ -64,5 +64,5 @@ enum BPBOffset { /// ignored BkBootSec = 50, /// Reserved - Reserved = 52 -} \ No newline at end of file + Reserved = 52, +} diff --git a/async-fat32/src/cache.rs b/async-fat32/src/cache.rs index 1ec4ee7..ee7a71a 100644 --- a/async-fat32/src/cache.rs +++ b/async-fat32/src/cache.rs @@ -19,7 +19,7 @@ pub struct Node { value: V, cnt: usize, time: usize, - dirty: bool + dirty: bool, } impl Node { @@ -29,7 +29,7 @@ impl Node { value, cnt: 0, time: 0, - dirty: false + dirty: false, } } } @@ -44,7 +44,9 @@ impl Eq for Node {} impl Ord for Node { fn cmp(&self, other: &Self) -> core::cmp::Ordering { - self.cnt.cmp(&other.cnt).then_with(|| self.time.cmp(&other.time)) + self.cnt + .cmp(&other.cnt) + .then_with(|| self.time.cmp(&other.time)) } } @@ -58,7 +60,7 @@ impl PartialOrd for Node { pub struct LFUCache { data: [Node; N], size: usize, - time: usize + time: usize, } impl LFUCache { @@ -66,7 +68,7 @@ impl LFUCache { Self { data, size: N, - time: 0 + time: 0, } } // todo: 用 `maybuninit` @@ -74,7 +76,7 @@ impl LFUCache { Self { data, size: 0, - time: 0 + time: 0, } } } @@ -85,12 +87,15 @@ impl Cache for LFUCache Option { self.time += 1; let time = self.time; - self.data[0..self.size].iter_mut().find(|i| i.key == *key).map(|node| { - // 更新结点时间和访问次数 - node.time = time; - node.cnt += 1; - node.value.clone() - }) + self.data[0..self.size] + .iter_mut() + .find(|i| i.key == *key) + .map(|node| { + // 更新结点时间和访问次数 + node.time = time; + node.cnt += 1; + node.value.clone() + }) } fn put(&mut self, key: &Self::Key, value: Self::Value) -> Option<(Self::Key, Self::Value)> { self.time += 1; @@ -99,17 +104,19 @@ impl Cache for LFUCache Cache for LFUCache Some(write_back), - false => None + false => None, } } } } } - #[test] fn lfu_cache_test() { let nodes = [Node::new(0, 0); 2]; let mut lfu_cache = LFUCache::empty(nodes); - + assert!(lfu_cache.get(&0).is_none()); assert!(lfu_cache.get(&1).is_none()); assert!(lfu_cache.get(&2).is_none()); @@ -150,4 +156,4 @@ fn lfu_cache_test() { assert_eq!(lfu_cache.get(&1), None); assert_eq!(lfu_cache.get(&3), Some(3)); assert_eq!(lfu_cache.get(&4), Some(4)); -} \ No newline at end of file +} diff --git a/async-fat32/src/config.rs b/async-fat32/src/config.rs index 0eaf06f..20aee2e 100644 --- a/async-fat32/src/config.rs +++ b/async-fat32/src/config.rs @@ -1,2 +1,2 @@ pub const BLOCK_SIZE: usize = 512; -pub const CACHE_SIZE: usize = 100; \ No newline at end of file +pub const CACHE_SIZE: usize = 100; diff --git a/async-fat32/src/lib.rs b/async-fat32/src/lib.rs index 972a15d..87de6f1 100644 --- a/async-fat32/src/lib.rs +++ b/async-fat32/src/lib.rs @@ -1,8 +1,8 @@ //! Async FAT32 File System Implemetation +mod block_cache; mod bs_bpb; mod cache; -mod block_cache; mod config; extern crate alloc; @@ -13,4 +13,4 @@ use async_trait::async_trait; pub trait AsyncBlockDevive { async fn read(&self, block_id: usize, buf: &mut [u8]); async fn write(&self, block_id: usize, buf: &[u8]); -} \ No newline at end of file +}