RlpStream out returns value instead of Result

This commit is contained in:
debris 2015-11-30 11:24:03 +01:00
parent 78965b3c5a
commit 2d7ae3a18a
3 changed files with 16 additions and 33 deletions

View File

@ -130,7 +130,7 @@ impl OverlayDB {
let mut s = RlpStream::new_list(2); let mut s = RlpStream::new_list(2);
s.append(&payload.1); s.append(&payload.1);
s.append(&payload.0); s.append(&payload.0);
self.backing.put(&key.bytes(), &s.out().unwrap()).expect("Low-level database error. Some issue with your hard disk?"); self.backing.put(&key.bytes(), &s.out()).expect("Low-level database error. Some issue with your hard disk?");
} }
} }

View File

@ -550,7 +550,7 @@ impl RlpStream {
/// fn main () { /// fn main () {
/// let mut stream = RlpStream::new_list(2); /// let mut stream = RlpStream::new_list(2);
/// stream.append(&"cat").append(&"dog"); /// stream.append(&"cat").append(&"dog");
/// let out = stream.out().unwrap(); /// let out = stream.out();
/// assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']); /// assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
/// } /// }
/// ``` /// ```
@ -575,7 +575,7 @@ impl RlpStream {
/// let mut stream = RlpStream::new_list(2); /// let mut stream = RlpStream::new_list(2);
/// stream.append_list(2).append(&"cat").append(&"dog"); /// stream.append_list(2).append(&"cat").append(&"dog");
/// stream.append(&""); /// stream.append(&"");
/// let out = stream.out().unwrap(); /// let out = stream.out();
/// assert_eq!(out, vec![0xca, 0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g', 0x80]); /// assert_eq!(out, vec![0xca, 0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g', 0x80]);
/// } /// }
/// ``` /// ```
@ -604,7 +604,7 @@ impl RlpStream {
/// fn main () { /// fn main () {
/// let mut stream = RlpStream::new_list(2); /// let mut stream = RlpStream::new_list(2);
/// stream.append_null().append_null(); /// stream.append_null().append_null();
/// let out = stream.out().unwrap(); /// let out = stream.out();
/// assert_eq!(out, vec![0xc2, 0x80, 0x80]); /// assert_eq!(out, vec![0xc2, 0x80, 0x80]);
/// } /// }
/// ``` /// ```
@ -643,7 +643,7 @@ impl RlpStream {
/// assert_eq!(stream.is_finished(), false); /// assert_eq!(stream.is_finished(), false);
/// stream.append(&"dog"); /// stream.append(&"dog");
/// assert_eq!(stream.is_finished(), true); /// assert_eq!(stream.is_finished(), true);
/// let out = stream.out().unwrap(); /// let out = stream.out();
/// assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']); /// assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
/// } /// }
pub fn is_finished(&self) -> bool { pub fn is_finished(&self) -> bool {
@ -652,11 +652,11 @@ impl RlpStream {
/// Streams out encoded bytes. /// Streams out encoded bytes.
/// ///
/// Returns an error if stream is not finished. /// panic! if stream is not finished
pub fn out(self) -> Result<Vec<u8>, EncoderError> { pub fn out(self) -> Vec<u8> {
match self.is_finished() { match self.is_finished() {
true => Ok(self.encoder.out()), true => self.encoder.out(),
false => Err(EncoderError::StreamIsUnfinished), false => panic!()
} }
} }
@ -701,23 +701,6 @@ pub fn encode<E>(object: &E) -> Vec<u8> where E: Encodable
encoder.out() encoder.out()
} }
#[derive(Debug)]
pub enum EncoderError {
StreamIsUnfinished,
}
impl StdError for EncoderError {
fn description(&self) -> &str {
"encoder error"
}
}
impl fmt::Display for EncoderError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self, f)
}
}
pub trait Encodable { pub trait Encodable {
fn encode<E>(&self, encoder: &mut E) -> () where E: Encoder; fn encode<E>(&self, encoder: &mut E) -> () where E: Encoder;
} }
@ -1035,7 +1018,7 @@ mod tests {
fn rlp_stream() { fn rlp_stream() {
let mut stream = RlpStream::new_list(2); let mut stream = RlpStream::new_list(2);
stream.append(&"cat").append(&"dog"); stream.append(&"cat").append(&"dog");
let out = stream.out().unwrap(); let out = stream.out();
assert_eq!(out, assert_eq!(out,
vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']); vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
} }
@ -1046,7 +1029,7 @@ mod tests {
stream.append_list(0); stream.append_list(0);
stream.append_list(1).append_list(0); stream.append_list(1).append_list(0);
stream.append_list(2).append_list(0).append_list(1).append_list(0); stream.append_list(2).append_list(0).append_list(1).append_list(0);
let out = stream.out().unwrap(); let out = stream.out();
assert_eq!(out, vec![0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0]); assert_eq!(out, vec![0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0]);
} }
@ -1057,7 +1040,7 @@ mod tests {
for _ in 0..17 { for _ in 0..17 {
stream.append(&""); stream.append(&"");
} }
let out = stream.out().unwrap(); let out = stream.out();
assert_eq!(out, vec![0xd1, 0x80, 0x80, 0x80, 0x80, 0x80, assert_eq!(out, vec![0xd1, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80]); 0x80, 0x80, 0x80, 0x80, 0x80, 0x80]);
@ -1073,9 +1056,9 @@ mod tests {
stream.append(&"aaa"); stream.append(&"aaa");
res.extend(vec![0x83, b'a', b'a', b'a']); res.extend(vec![0x83, b'a', b'a', b'a']);
} }
let out = stream.out().unwrap(); let out = stream.out();
assert_eq!(out, res); assert_eq!(out, res);
} };
struct DTestPair<T>(T, Vec<u8>) where T: rlp::Decodable + fmt::Debug + cmp::Eq; struct DTestPair<T>(T, Vec<u8>) where T: rlp::Decodable + fmt::Debug + cmp::Eq;

View File

@ -177,7 +177,7 @@ pub fn hash256(vec: &[NibblePair]) -> H256 {
_ => { _ => {
let mut stream = RlpStream::new(); let mut stream = RlpStream::new();
hash256rlp(&vec, 0, &mut stream); hash256rlp(&vec, 0, &mut stream);
stream.out().unwrap() stream.out()
} }
}; };
@ -251,7 +251,7 @@ fn hash256rlp(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) {
fn hash256aux(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) { fn hash256aux(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) {
let mut s = RlpStream::new(); let mut s = RlpStream::new();
hash256rlp(vec, pre_len, &mut s); hash256rlp(vec, pre_len, &mut s);
let out = s.out().unwrap(); let out = s.out();
match out.len() { match out.len() {
0...31 => stream.append_raw(&out, 1), 0...31 => stream.append_raw(&out, 1),
_ => stream.append(&out.sha3()) _ => stream.append(&out.sha3())