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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use core::{
pin::Pin,
task::{Poll, Context}
};
use crate::{backend::{self, Decode, PollDecodeStatus, StartDecodeStatus}, io};
pub fn deserializer<F, R>(format: F, reader: R) -> Deserializer<F, R> {
Deserializer::new(format, reader)
}
pub struct Deserializer<F, R> {
pub format: F,
pub reader: R,
}
impl<F, R> Deserializer<F, R> {
pub fn new(format: F, reader: R) -> Deserializer<F, R> {
Deserializer{
format,
reader,
}
}
pub fn into_stream<D>(self) -> Deserialize<F, R, D>
where
F: backend::FormatDecode,
R: io::AsyncBufRead + Unpin,
D: backend::Decodable,
{
Deserialize::new(self)
}
pub fn deserialize<D>(&mut self) -> D::Future<'_, F, R>
where
F: backend::FormatDeserialize,
R: io::AsyncBufRead + Unpin,
D: backend::AsyncDeserialize,
{
D::deserialize(&self.format, &mut self.reader)
}
}
enum State<F, D>
where
F: backend::FormatDecode,
D: backend::Decodable,
{
Ready,
Pending(D::Decoder<F>),
Error,
}
pub struct Deserialize<F, R, D>
where
F: backend::FormatDecode,
D: backend::Decodable,
{
deserializer: Deserializer<F, R>,
state: State<F, D>,
}
impl<F, R, D> Deserialize<F, R, D>
where
F: backend::FormatDecode,
D: backend::Decodable,
{
pub fn new(deserializer: Deserializer<F, R>) -> Self {
Self {
deserializer,
state: State::Ready,
}
}
pub fn is_ready(&self) -> bool {
matches!(self.state, State::Ready)
}
pub fn try_into_inner(self) -> Result<Deserializer<F, R>, F> {
if let State::Ready = self.state {
Ok(self.deserializer)
} else {
Err(self.deserializer.format)
}
}
}
impl<F, R, D> Unpin for Deserialize<F, R, D>
where
F: backend::FormatDecode,
D: backend::Decodable,
{}
impl<F, R, D> futures::Stream for Deserialize<F, R, D>
where
F: backend::FormatDecode,
R: io::AsyncBufRead + Unpin,
D: backend::Decodable,
{
type Item = D;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let Self {
deserializer,
state,
} = &mut *self;
match state {
State::Ready => match D::Decoder::start_decode(&deserializer.format, &mut deserializer.reader, cx) {
StartDecodeStatus::Fini(d) => Poll::Ready(Some(d)),
StartDecodeStatus::Pending(dec) => {
*state = State::Pending(dec);
Poll::Pending
}
StartDecodeStatus::Error(_) => {
*state = State::Error;
Poll::Ready(None)
}
}
State::Pending(p) => match p.poll_decode(&deserializer.format, &mut deserializer.reader, cx) {
PollDecodeStatus::Fini(d) => {
*state = State::Ready;
Poll::Ready(Some(d))
}
PollDecodeStatus::Pending => Poll::Pending,
PollDecodeStatus::Error(_) => {
*state = State::Error;
Poll::Ready(None)
}
}
State::Error => Poll::Ready(None)
}
}
}