Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Don't allocate in expect_valid_rlp unless necessary (#8867)
Browse files Browse the repository at this point in the history
* don't allocate via format! in case there's no error

* fix test?
  • Loading branch information
ordian authored and debris committed Jun 12, 2018
1 parent 861d829 commit 2a470de
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions ethcore/src/views/view_rlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ impl<'a, 'view> ViewRlp<'a> where 'a : 'view {

/// Returns a new instance replacing existing rlp with new rlp, maintaining debug info
fn new_from_rlp(&self, rlp: Rlp<'a>) -> Self {
ViewRlp {
rlp,
ViewRlp {
rlp,
file: self.file,
line: self.line
line: self.line
}
}

Expand All @@ -53,7 +53,12 @@ impl<'a, 'view> ViewRlp<'a> where 'a : 'view {
}

fn expect_valid_rlp<T>(&self, r: Result<T, DecoderError>) -> T {
r.expect(&format!("View rlp is trusted and should be valid. Constructed in {} on line {}", self.file, self.line))
r.unwrap_or_else(|e| panic!(
"View rlp is trusted and should be valid. Constructed in {} on line {}: {}",
self.file,
self.line,
e
))
}

/// Returns rlp at the given index, panics if no rlp at that index
Expand All @@ -75,7 +80,7 @@ impl<'a, 'view> ViewRlp<'a> where 'a : 'view {
/// Returns decoded value at the given index, panics not present or valid at that index
pub fn val_at<T>(&self, index: usize) -> T where T : Decodable {
self.expect_valid_rlp(self.rlp.val_at(index))
}
}

/// Returns decoded list of values, panics if rlp is invalid
pub fn list_at<T>(&self, index: usize) -> Vec<T> where T: Decodable {
Expand Down

0 comments on commit 2a470de

Please sign in to comment.