decodable implementations

This commit is contained in:
debris 2015-12-08 00:27:12 +01:00
parent 0de75185aa
commit cb5ec8c0af
2 changed files with 15 additions and 7 deletions

View File

@ -37,7 +37,7 @@ pub trait Decoder {
}
pub trait Decodable: Sized {
fn decode<T, D>(decoder: &D) -> Result<T, DecoderError> where D: Decoder;
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder;
}
pub trait View<'a, 'view>: Sized {

View File

@ -312,19 +312,27 @@ impl<'a> Decoder for BasicDecoder<'a> {
}
impl<T> Decodable for T where T: FromBytes {
fn decode<R, D>(decoder: &D) -> Result<R, DecoderError> where D: Decoder {
unimplemented!()
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
decoder.read_value(| bytes | {
Ok(try!(T::from_bytes(bytes)))
})
}
}
impl<T> Decodable for Vec<T> where T: Decodable {
fn decode<R, D>(decoder: &D) -> Result<R, DecoderError> where D: Decoder {
unimplemented!()
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
decoder.read_list(| decoders | {
decoders.iter().map(|d| T::decode(d)).collect()
})
}
}
impl Decodable for Vec<u8> {
fn decode<R, D>(decoder: &D) -> Result<R, DecoderError> where D: Decoder {
unimplemented!()
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
decoder.read_value(| bytes | {
let mut res = vec![];
res.extend(bytes);
Ok(res)
})
}
}