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