xref: /DragonOS/kernel/src/driver/block/cache/cache_block.rs (revision c635d8a9cfe25bc11779f323ef0c7d7a0f597d4a)
1 use alloc::{boxed::Box, vec::Vec};
2 
3 use crate::driver::base::block::block_device::BlockId;
4 
5 use super::{BlockCacheError, BLOCK_SIZE};
6 
7 /// # 枚举功能
8 /// 该枚举设计来是用于实现回写法的,但是目前并未使用
9 #[allow(dead_code)]
10 pub enum CacheBlockFlag {
11     Unused,
12     Unwrited,
13     Writed,
14 }
15 
16 pub type CacheBlockAddr = usize;
17 
18 /// # 结构功能
19 /// 存储数据的最小单位
20 pub struct CacheBlock {
21     data: Box<[u8]>,
22     _flag: CacheBlockFlag,
23     lba_id: BlockId,
24 }
25 
26 impl CacheBlock {
27     pub fn new(data: Box<[u8]>, flag: CacheBlockFlag, lba_id: BlockId) -> Self {
28         CacheBlock {
29             data,
30             _flag: flag,
31             lba_id,
32         }
33     }
34 
35     pub fn from_data(lba_id: BlockId, data: Vec<u8>) -> Self {
36         let space_box = data.into_boxed_slice();
37         CacheBlock::new(space_box, CacheBlockFlag::Unwrited, lba_id)
38     }
39 
40     pub fn _set_flag(&mut self, _flag: CacheBlockFlag) -> Option<()> {
41         todo!()
42     }
43     pub fn data(&self, buf: &mut [u8]) -> Result<usize, BlockCacheError> {
44         if buf.len() != BLOCK_SIZE {
45             return Err(BlockCacheError::BlockSizeError);
46         }
47         buf.copy_from_slice(&self.data);
48         return Ok(BLOCK_SIZE);
49     }
50 
51     pub fn lba_id(&self) -> BlockId {
52         self.lba_id
53     }
54 }
55