From 3e1add7051e49f4a5e53a50e2ba067702f9b9aa2 Mon Sep 17 00:00:00 2001 From: debris Date: Fri, 15 Jan 2016 19:51:48 +0100 Subject: [PATCH 1/5] flush macro --- src/common.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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); +} From e3f1d70353757d5b8395c81953068c3740aff4d3 Mon Sep 17 00:00:00 2001 From: debris Date: Fri, 15 Jan 2016 21:03:38 +0100 Subject: [PATCH 2/5] display trait implemented for rlp --- src/rlp/rlpin.rs | 7 +++++++ src/rlp/untrusted_rlp.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) 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..a5ee7990e 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); + println!("{}", rlp); +} + From d41ad82aa93853744d0f3b115f350b42cf6b1230 Mon Sep 17 00:00:00 2001 From: debris Date: Sat, 16 Jan 2016 00:38:41 +0100 Subject: [PATCH 3/5] uint fmt debug/display is changed to output decimal representation --- src/uint.rs | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/uint.rs b/src/uint.rs index 038c85458..32cbd2cf1 100644 --- a/src/uint.rs +++ b/src/uint.rs @@ -595,16 +595,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")); @@ -1052,5 +1066,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"); + } } From 2899790cd3d21c1ab2aa2a3559a4c25e09e8041b Mon Sep 17 00:00:00 2001 From: debris Date: Sat, 16 Jan 2016 00:42:02 +0100 Subject: [PATCH 4/5] test for rlp display --- src/rlp/untrusted_rlp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rlp/untrusted_rlp.rs b/src/rlp/untrusted_rlp.rs index a5ee7990e..2bf33ba68 100644 --- a/src/rlp/untrusted_rlp.rs +++ b/src/rlp/untrusted_rlp.rs @@ -436,6 +436,6 @@ fn test_rlp_display() { use rustc_serialize::hex::FromHex; let data = "f84d0589010efbef67941f79b2a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470".from_hex().unwrap(); let rlp = UntrustedRlp::new(&data); - println!("{}", rlp); + assert_eq!(format!("{}", rlp), "[\"0x05\", \"0x010efbef67941f79b2\", \"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421\", \"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470\"]"); } From 69f1f310b6f337ae37a70c3d93e77abc5ff76571 Mon Sep 17 00:00:00 2001 From: arkpar Date: Sat, 16 Jan 2016 13:29:51 +0100 Subject: [PATCH 5/5] Minor timers fix --- src/network/host.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) 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 } } }