[cache (rust)] Implement Pack and Default for restoration

This commit is contained in:
Ming-Hung Tsai 2021-05-18 17:11:21 +08:00
parent 86e2db3a1a
commit 1907dab5ee
2 changed files with 35 additions and 0 deletions

17
src/cache/hint.rs vendored
View File

@ -1,3 +1,5 @@
use anyhow::Result;
use byteorder::WriteBytesExt;
use nom::IResult;
use std::convert::TryInto;
@ -26,4 +28,19 @@ impl Unpack for Hint {
}
}
impl Pack for Hint {
fn pack<W: WriteBytesExt>(&self, data: &mut W) -> Result<()> {
for v in &self.hint {
data.write_u8(*v)?;
}
Ok(())
}
}
impl Default for Hint {
fn default() -> Self {
Hint { hint: [0; 4] }
}
}
//------------------------------------------

18
src/cache/mapping.rs vendored
View File

@ -1,3 +1,5 @@
use anyhow::Result;
use byteorder::WriteBytesExt;
use nom::number::complete::*;
use nom::IResult;
@ -51,4 +53,20 @@ impl Unpack for Mapping {
}
}
impl Pack for Mapping {
fn pack<W: WriteBytesExt>(&self, data: &mut W) -> Result<()> {
let m: u64 = (self.oblock << 16) | self.flags as u64;
m.pack(data)
}
}
impl Default for Mapping {
fn default() -> Self {
Mapping {
oblock: 0,
flags: 0,
}
}
}
//------------------------------------------