Impl encodable/decodable for the array newtypes

This commit is contained in:
Andrew Poelstra 2014-09-04 20:09:18 -05:00
parent 17daebf15d
commit 71312b032a
2 changed files with 32 additions and 0 deletions

View File

@ -89,6 +89,35 @@ macro_rules! impl_array_newtype(
}
}
}
impl<D: ::serialize::Decoder<E>, E> ::serialize::Decodable<D, E> for $thing {
fn decode(d: &mut D) -> ::std::prelude::Result<$thing, E> {
use serialize::Decodable;
::assert_type_is_copy::<$ty>();
d.read_seq(|d, len| {
if len != $len {
Err(d.error("Invalid length"))
} else {
unsafe {
use std::mem;
let mut ret: [$ty, ..$len] = mem::uninitialized();
for i in range(0, len) {
ret[i] = try!(d.read_seq_elt(i, |d| Decodable::decode(d)));
}
Ok($thing(ret))
}
}
})
}
}
impl<E: ::serialize::Encoder<S>, S> ::serialize::Encodable<E, S> for $thing {
fn encode(&self, e: &mut E) -> ::std::prelude::Result<(), S> {
self.as_slice().encode(e)
}
}
}
)

View File

@ -56,6 +56,9 @@ pub mod constants;
pub mod ffi;
pub mod key;
/// I dunno where else to put this..
fn assert_type_is_copy<T: Copy>() { }
/// A tag used for recovering the public key from a compact signature
pub struct RecoveryId(i32);