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
#![deny(missing_docs)]
extern crate futures_core;
extern crate futures_io;
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use futures_core::Poll;
use futures_core::task::Context;
use futures_io::{AsyncWrite, AsyncRead, Error as FutIoErr};
pub trait AsyncEncode<W: AsyncWrite> {
fn poll_encode(&mut self, cx: &mut Context, writer: &mut W) -> Poll<usize, FutIoErr>;
}
pub trait AsyncEncodeLen<W: AsyncWrite>: AsyncEncode<W> {
fn remaining_bytes(&self) -> usize;
}
pub trait AsyncDecode<R: AsyncRead> {
type Item;
type Error;
fn poll_decode(&mut self,
cx: &mut Context,
reader: &mut R)
-> Poll<(Option<Self::Item>, usize), DecodeError<Self::Error>>;
}
#[derive(Debug)]
pub enum DecodeError<E> {
ReaderError(FutIoErr),
DataError(E),
}
impl<E: Display> Display for DecodeError<E> {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
match *self {
DecodeError::ReaderError(ref err) => write!(f, "Decode reader error: {}", err),
DecodeError::DataError(ref err) => write!(f, "Decode data error: {}", err),
}
}
}
impl<E: Error> Error for DecodeError<E> {
fn description(&self) -> &str {
match *self {
DecodeError::ReaderError(ref err) => err.description(),
DecodeError::DataError(ref err) => err.description(),
}
}
fn cause(&self) -> Option<&Error> {
match *self {
DecodeError::ReaderError(ref err) => Some(err),
DecodeError::DataError(ref err) => Some(err),
}
}
}
impl<E> From<FutIoErr> for DecodeError<E> {
fn from(err: FutIoErr) -> DecodeError<E> {
DecodeError::ReaderError(err)
}
}