diff --git a/src/common.rs b/src/common.rs index 7750d9ea5..3ec02ad9b 100644 --- a/src/common.rs +++ b/src/common.rs @@ -35,7 +35,23 @@ macro_rules! xx { } } +#[macro_export] +macro_rules! flush { + ($($arg:tt)*) => ($crate::flush(format!("{}", format_args!($($arg)*)))); +} + +#[macro_export] +macro_rules! flushln { + ($fmt:expr) => (flush!(concat!($fmt, "\n"))); + ($fmt:expr, $($arg:tt)*) => (flush!(concat!($fmt, "\n"), $($arg)*)); +} + pub fn flush(s: String) { ::std::io::stdout().write(s.as_bytes()).unwrap(); ::std::io::stdout().flush().unwrap(); } + +#[test] +fn test_flush() { + flushln!("hello_world {:?}", 1); +} diff --git a/src/network/host.rs b/src/network/host.rs index 8b1038c8a..99e8b5e7c 100644 --- a/src/network/host.rs +++ b/src/network/host.rs @@ -600,14 +600,12 @@ impl IoHandler> for Host where Messa FIRST_CONNECTION ... LAST_CONNECTION => self.connection_timeout(token, io), NODETABLE_DISCOVERY => {}, NODETABLE_MAINTAIN => {}, - _ => { - let protocol = *self.timers.get_mut(&token).expect("Unknown user timer token"); - match self.handlers.get_mut(protocol) { - None => { warn!(target: "net", "No handler found for protocol: {:?}", protocol) }, - Some(h) => { - h.timeout(&mut NetworkContext::new(io, protocol, Some(token), &mut self.connections, &mut self.timers), token); - } - } + _ => match self.timers.get_mut(&token).map(|p| *p) { + Some(protocol) => match self.handlers.get_mut(protocol) { + None => { warn!(target: "net", "No handler found for protocol: {:?}", protocol) }, + Some(h) => { h.timeout(&mut NetworkContext::new(io, protocol, Some(token), &mut self.connections, &mut self.timers), token); } + }, + None => {} // time not registerd through us } } } diff --git a/src/rlp/rlpin.rs b/src/rlp/rlpin.rs index 2179643d1..4cc83b261 100644 --- a/src/rlp/rlpin.rs +++ b/src/rlp/rlpin.rs @@ -1,3 +1,4 @@ +use std::fmt; use rlp::{View, Decodable, DecoderError, UntrustedRlp, PayloadInfo, Prototype}; impl<'a> From> for Rlp<'a> { @@ -15,6 +16,12 @@ pub struct Rlp<'a> { rlp: UntrustedRlp<'a> } +impl<'a> fmt::Display for Rlp<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + write!(f, "{}", self.rlp) + } +} + impl<'a, 'view> View<'a, 'view> for Rlp<'a> where 'a: 'view { type Prototype = Prototype; type PayloadInfo = PayloadInfo; diff --git a/src/rlp/untrusted_rlp.rs b/src/rlp/untrusted_rlp.rs index c0e7c4cf7..2bf33ba68 100644 --- a/src/rlp/untrusted_rlp.rs +++ b/src/rlp/untrusted_rlp.rs @@ -1,4 +1,6 @@ use std::cell::Cell; +use std::fmt; +use rustc_serialize::hex::ToHex; use bytes::{FromBytes}; use rlp::{View, Decoder, Decodable, DecoderError}; @@ -63,6 +65,24 @@ impl<'a> Clone for UntrustedRlp<'a> { } } +impl<'a> fmt::Display for UntrustedRlp<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match self.prototype() { + Ok(Prototype::Null) => write!(f, "null"), + Ok(Prototype::Data(_)) => write!(f, "\"0x{}\"", self.data().unwrap().to_hex()), + Ok(Prototype::List(len)) => { + try!(write!(f, "[")); + for i in 0..len-1 { + try!(write!(f, "{}, ", self.at(i).unwrap())); + } + try!(write!(f, "{}", self.at(len - 1).unwrap())); + write!(f, "]") + }, + Err(err) => write!(f, "{:?}", err) + } + } +} + impl<'a, 'view> View<'a, 'view> for UntrustedRlp<'a> where 'a: 'view { type Prototype = Result; type PayloadInfo = Result; @@ -410,3 +430,12 @@ impl_array_decodable_recursive!( 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 40, 48, 56, 64, 72, 96, 128, 160, 192, 224, ); + +#[test] +fn test_rlp_display() { + use rustc_serialize::hex::FromHex; + let data = "f84d0589010efbef67941f79b2a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470".from_hex().unwrap(); + let rlp = UntrustedRlp::new(&data); + assert_eq!(format!("{}", rlp), "[\"0x05\", \"0x010efbef67941f79b2\", \"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421\", \"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470\"]"); +} + diff --git a/src/uint.rs b/src/uint.rs index ccc66b447..546965ca4 100644 --- a/src/uint.rs +++ b/src/uint.rs @@ -695,16 +695,30 @@ macro_rules! construct_uint { impl fmt::Debug for $name { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let &$name(ref data) = self; - try!(write!(f, "0x")); - for ch in data.iter().rev() { - try!(write!(f, "{:02x}", ch)); - } - Ok(()) + fmt::Display::fmt(self, f) } } impl fmt::Display for $name { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if *self == $name::zero() { + return write!(f, "0"); + } + + let mut s = String::new(); + let mut current = *self; + let ten = $name::from(10); + + while current != $name::zero() { + s = format!("{}{}", (current % ten).low_u32(), s); + current = current / ten; + } + + write!(f, "{}", s) + } + } + + impl fmt::LowerHex for $name { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let &$name(ref data) = self; try!(write!(f, "0x")); @@ -1222,5 +1236,16 @@ mod tests { assert_eq!(U256::from_dec_str("10").unwrap(), U256::from(10u64)); assert_eq!(U256::from_dec_str("1024").unwrap(), U256::from(1024u64)); } + + #[test] + fn display_uint() { + let s = "12345678987654321023456789"; + assert_eq!(format!("{}", U256::from_dec_str(s).unwrap()), s); + } + + #[test] + fn display_uint_zero() { + assert_eq!(format!("{}", U256::from(0)), "0"); + } }