pbcodec/
traits.rs

1use std;
2use std::io::{Read, Write};
3use futures::Future;
4
5use {Error, Result};
6use future::decode::{DecodeInto, DecodeMessage, DecodeTryInto};
7use future::encode::EncodeMessage;
8use wire::WireType;
9
10pub trait Tag: Default {
11    fn number() -> u32;
12}
13
14// TODO(?): s/FieldType/WireType/
15pub trait FieldType: Default {
16    fn wire_type() -> WireType;
17}
18
19pub trait MapKey: Default + FieldType {}
20
21pub trait Map: Default {
22    type Key: MapKey;
23    type Value: FieldType;
24    type IntoIter: Iterator<Item = (Self::Key, Self::Value)>;
25    fn insert(&mut self, key: Self::Key, value: Self::Value);
26    fn into_iter(self) -> Self::IntoIter;
27    fn iter<'a>(&'a self) -> Box<Iterator<Item = (&'a Self::Key, &'a Self::Value)> + 'a>;
28}
29impl<K, V> Map for std::collections::BTreeMap<K, V>
30where
31    K: MapKey + Ord,
32    V: FieldType,
33{
34    type Key = K;
35    type Value = V;
36    type IntoIter = std::collections::btree_map::IntoIter<K, V>;
37    fn insert(&mut self, key: Self::Key, value: Self::Value) {
38        std::collections::BTreeMap::insert(self, key, value);
39    }
40    fn into_iter(self) -> Self::IntoIter {
41        std::iter::IntoIterator::into_iter(self)
42    }
43    fn iter<'a>(&'a self) -> Box<Iterator<Item = (&'a Self::Key, &'a Self::Value)> + 'a> {
44        Box::new(std::collections::BTreeMap::iter(self))
45    }
46}
47impl<K, V> Map for std::collections::HashMap<K, V>
48where
49    K: MapKey + Eq + std::hash::Hash,
50    V: FieldType,
51{
52    type Key = K;
53    type Value = V;
54    type IntoIter = std::collections::hash_map::IntoIter<K, V>;
55    fn insert(&mut self, key: Self::Key, value: Self::Value) {
56        std::collections::HashMap::insert(self, key, value);
57    }
58    fn into_iter(self) -> Self::IntoIter {
59        std::iter::IntoIterator::into_iter(self)
60    }
61    fn iter<'a>(&'a self) -> Box<Iterator<Item = (&'a Self::Key, &'a Self::Value)> + 'a> {
62        Box::new(std::collections::HashMap::iter(self))
63    }
64}
65
66pub trait Packable: FieldType {}
67
68pub trait Field: Default {}
69
70pub trait SingularField: Field {}
71
72pub trait Message: Sized + Default {
73    type Base: Message;
74    fn from_base(base: Self::Base) -> Result<Self>;
75    fn into_base(self) -> Self::Base;
76    fn encode_message<W: Write>(self, writer: W) -> EncodeMessage<W, Self>
77    where
78        Self::Base: Encode<W>,
79    {
80        EncodeMessage::new(writer, self)
81    }
82    fn decode_message<R: Read>(reader: R) -> DecodeMessage<R, Self>
83    where
84        Self::Base: Decode<R>,
85    {
86        DecodeMessage::new(reader)
87    }
88}
89
90pub trait Encode<W: Write>: Sized {
91    type Future: Future<Item = W, Error = Error<W>>;
92    fn encode(self, writer: W) -> Self::Future;
93    fn encoded_size(&self) -> u64;
94}
95
96pub trait Decode<R: Read>: Sized {
97    type Future: Future<Item = (R, Self), Error = Error<R>>;
98    fn decode(reader: R) -> Self::Future;
99    fn decode_into<T>(reader: R) -> DecodeInto<R, Self, T>
100    where
101        T: From<Self>,
102    {
103        DecodeInto::new(reader)
104    }
105    fn decode_try_into<T>(reader: R) -> DecodeTryInto<R, Self, T>
106    where
107        T: TryFrom<Self>,
108    {
109        DecodeTryInto::new(reader)
110    }
111}
112
113pub trait DecodeField<R: Read>: Field {
114    type Future: Future<Item = (R, Self), Error = Error<R>>;
115    fn is_target(tag: u32) -> bool;
116    fn decode_field(
117        self,
118        reader: R,
119        tag: u32,
120        wire_type: WireType,
121    ) -> std::result::Result<Self::Future, Error<R>>;
122}
123
124pub trait TryFrom<F>: Sized {
125    fn try_from(f: F) -> Result<Self>;
126}