Skip to content

Commit

Permalink
refactor(rarity): use display trait to format state output
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianMoesl committed Mar 1, 2021
1 parent aa39116 commit 19bc224
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/rarity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,30 @@ pub enum SyscallId {
pub struct State {
pc: u64,
regs: [Value; 32],
#[allow(dead_code)]
memory: Vec<Value>,
}

impl State {
fn write<P>(&self, path: P) -> Result<()>
fn write_to_file<P>(&self, path: P) -> Result<(), std::io::Error>
where
P: AsRef<Path>,
{
let mut file = File::create(path)?;

writeln!(&mut file, "PC: 0x{:x}", self.pc)?;
writeln!(&mut file, "Register:")?;
for (idx, val) in self.regs.iter().enumerate() {
writeln!(&mut file, "{:x}: {:#}", idx, val)?;
}
write!(file, "{}", self)
}
}

writeln!(&mut file, "Memory:")?;
for (addr, val) in self.memory.iter().enumerate() {
if let Value::Concrete(concrete) = val {
writeln!(&mut file, "0x{:016x}: 0x{:x}", addr * 8, concrete)?;
}
impl fmt::Display for State {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "PC: 0x{:x}", self.pc)?;
writeln!(f, "Register:")?;
for (idx, val) in self.regs.iter().enumerate() {
writeln!(f, "{:x}: {:#}", idx, val)?;
}

Ok(())
writeln!(f, "Memory:")
}
}

Expand Down Expand Up @@ -102,7 +103,7 @@ where
for (idx, state) in states.iter().enumerate() {
let state_file = output_dir.as_ref().join(format!("dump_{}.out", idx));

state.write(&state_file)?;
state.write_to_file(&state_file)?;
}
info!(" done! State dumps written into {:?}", output_dir.as_ref());

Expand Down

0 comments on commit 19bc224

Please sign in to comment.