[array (rust)] Implement Pack for restoration

This commit is contained in:
Ming-Hung Tsai 2021-05-14 21:21:18 +08:00
parent 7e2d69ede9
commit a6e1870b2b
1 changed files with 22 additions and 1 deletions

View File

@ -1,3 +1,4 @@
use byteorder::{LittleEndian, WriteBytesExt};
use nom::{multi::count, number::complete::*, IResult};
use std::fmt;
use thiserror::Error;
@ -5,7 +6,7 @@ use thiserror::Error;
use crate::checksum;
use crate::io_engine::BLOCK_SIZE;
use crate::pdata::btree;
use crate::pdata::unpack::Unpack;
use crate::pdata::unpack::{Pack, Unpack};
//------------------------------------------
@ -44,11 +45,31 @@ impl Unpack for ArrayBlockHeader {
}
}
impl Pack for ArrayBlockHeader {
fn pack<W: WriteBytesExt>(&self, w: &mut W) -> anyhow::Result<()> {
// csum needs to be calculated right for the whole metadata block.
w.write_u32::<LittleEndian>(0)?;
w.write_u32::<LittleEndian>(self.max_entries)?;
w.write_u32::<LittleEndian>(self.nr_entries)?;
w.write_u32::<LittleEndian>(self.value_size)?;
w.write_u64::<LittleEndian>(self.blocknr)?;
Ok(())
}
}
//------------------------------------------
pub struct ArrayBlock<V: Unpack> {
pub header: ArrayBlockHeader,
pub values: Vec<V>,
}
impl<V: Unpack> ArrayBlock<V> {
pub fn set_block(&mut self, b: u64) {
self.header.blocknr = b;
}
}
//------------------------------------------
#[derive(Error, Clone, Debug)]