Clean up U256 serialization.

This commit is contained in:
Sean Bowe 2016-09-18 04:08:56 -06:00
parent fe3dfc3e29
commit b20157fef1
No known key found for this signature in database
GPG Key ID: 95684257D8F8B031
1 changed files with 10 additions and 12 deletions

View File

@ -79,14 +79,13 @@ impl U512 {
impl Encodable for U256 { impl Encodable for U256 {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
let mut buf = [0; 32]; let mut buf = [0; (4 * 8)];
BigEndian::write_u64(&mut buf[0..], self.0[3]); for (l, i) in (0..4).rev().zip((0..4).map(|i| i * 8)) {
BigEndian::write_u64(&mut buf[8..], self.0[2]); BigEndian::write_u64(&mut buf[i..], self.0[l]);
BigEndian::write_u64(&mut buf[16..], self.0[1]); }
BigEndian::write_u64(&mut buf[24..], self.0[0]);
for i in 0..32 { for i in 0..(4 * 8) {
try!(s.emit_u8(buf[i])); try!(s.emit_u8(buf[i]));
} }
@ -96,17 +95,16 @@ impl Encodable for U256 {
impl Decodable for U256 { impl Decodable for U256 {
fn decode<S: Decoder>(s: &mut S) -> Result<U256, S::Error> { fn decode<S: Decoder>(s: &mut S) -> Result<U256, S::Error> {
let mut buf = [0; 32]; let mut buf = [0; (4 * 8)];
for i in 0..32 { for i in 0..(4 * 8) {
buf[i] = try!(s.read_u8()); buf[i] = try!(s.read_u8());
} }
let mut n = [0; 4]; let mut n = [0; 4];
n[3] = BigEndian::read_u64(&buf[0..]); for (l, i) in (0..4).rev().zip((0..4).map(|i| i * 8)) {
n[2] = BigEndian::read_u64(&buf[8..]); n[l] = BigEndian::read_u64(&buf[i..]);
n[1] = BigEndian::read_u64(&buf[16..]); }
n[0] = BigEndian::read_u64(&buf[24..]);
Ok(U256(n)) Ok(U256(n))
} }