diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2020-01-19 23:34:09 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2020-01-19 23:34:09 +0200 |
commit | c6c98371c471cba796560164a25a5c4bd29862fa (patch) | |
tree | 172b71873b84819b8583fa3584c36c9206379a1f /src | |
parent | fa3b0d8cb573a2a0700fa565d6cf12abf66dc70f (diff) |
Replace custom debug with derivative
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 26 |
1 files changed, 8 insertions, 18 deletions
@@ -1,3 +1,4 @@ +use derivative::Derivative; use num::bigint::BigInt; use num::traits::identities::Zero; use rpds::RedBlackTreeMap; @@ -10,15 +11,21 @@ use std::iter::Iterator; pub type Intcode = BigInt; -#[derive(Clone)] +#[derive(Clone, Derivative)] +#[derivative(Debug)] pub struct IntcodeProgram { + #[derivative(Debug(format_with = "std::fmt::Display::fmt"))] instruction_pointer: Intcode, + #[derivative(Debug(format_with = "std::fmt::Display::fmt"))] relative_base: Intcode, pub error: Option<IntcodeProgramError>, pub halted: bool, pub awaiting_input: bool, + #[derivative(Debug(format_with = "std::fmt::Display::fmt"))] memory: RedBlackTreeMap<Intcode, Intcode>, + #[derivative(Debug(format_with = "std::fmt::Display::fmt"))] pub input: List<Intcode>, + #[derivative(Debug(format_with = "std::fmt::Display::fmt"))] pub output: Vector<Intcode>, } @@ -41,23 +48,6 @@ impl FromIterator<Intcode> for IntcodeProgram { } } -impl fmt::Debug for IntcodeProgram { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - writeln!( - f, - "IntcodeProgram {{ \nins: {}, base: {}, error: {:?}, halted: {}, awaiting_input: {} \nmemory: {}, \ninput: {}, \n output: {} \n}}", - self.instruction_pointer, - self.relative_base, - self.error, - self.halted, - self.awaiting_input, - format!("{:?}", self.memory.iter().map(|(k, v)| format!("{} -> {}", k, v)).collect::<Vec<_>>()), - format!("{:?}", self.input.iter().map(|n| format!("{}", n)).collect::<Vec<_>>()), - format!("{:?}", self.output.iter().map(|n| format!("{}", n)).collect::<Vec<_>>()), - ) - } -} - pub fn intcode_to_bool(i: &Intcode) -> bool { *i != Intcode::from(0) } |