Implement custom Debug for Magic types

Related to #63
This commit is contained in:
Deirdre Connolly 2019-10-15 19:38:44 -04:00 committed by Henry de Valence
parent 65d988471a
commit 051dc2f039
2 changed files with 28 additions and 4 deletions

View File

@ -9,16 +9,19 @@ edition = "2018"
[dependencies]
bitflags = "1.2"
bytes = "0.4"
rand = "0.7"
byteorder = "1.3"
bytes = "0.4"
chrono = "0.4"
thiserror = "1"
serde = { version = "1", features = ["serde_derive"] }
pin-project = "0.4"
# indexmap has rayon support for parallel iteration,
hex = "0.4"
# indexmap has rayon support for parallel iteration,
# which we don't use, so disable it to drop the dependencies.
indexmap = { version = "1.2", default-features = false }
pin-project = "0.4"
rand = "0.7"
serde = { version = "1", features = ["serde_derive"] }
tokio = "=0.2.0-alpha.6"
futures-preview = "=0.3.0-alpha.19"

View File

@ -1,9 +1,18 @@
//! Newtype wrappers assigning semantic meaning to primitive types.
use hex;
use std::fmt;
/// A magic number identifying the network.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct Magic(pub [u8; 4]);
impl fmt::Debug for Magic {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("Magic").field(&hex::encode(&self.0)).finish()
}
}
/// A protocol version number.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Version(pub u32);
@ -34,3 +43,15 @@ impl Default for Nonce {
Self(thread_rng().gen())
}
}
#[cfg(test)]
mod tests {
use crate::constants::magics;
#[test]
fn magic_debug() {
assert_eq!(format!("{:?}", magics::MAINNET), "Magic(\"24e92764\")");
assert_eq!(format!("{:?}", magics::TESTNET), "Magic(\"fa1af9bf\")");
}
}