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
#![deny(missing_docs)]
#![deprecated="This was a failed attempt at finding a suitable abstraction. The async-codec crate might be what you need instead."]
extern crate futures_core;
extern crate futures_io;
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use futures_core::Future;
use futures_io::{AsyncRead, AsyncWrite, Error as FutIoErr};
pub trait AsyncWriterFuture<W: AsyncWrite>
: Future<Item = (W, usize), Error = (W, FutIoErr)> {
fn already_written(&self) -> usize;
}
pub trait AsyncWriterFutureLen<W: AsyncWrite>: AsyncWriterFuture<W> {
fn remaining_bytes(&self) -> usize;
}
pub trait AsyncSerialize<W: AsyncWrite>: AsyncWriterFuture<W> {
type Serialized;
fn from_val(writer: W, val: Self::Serialized) -> Self;
}
pub trait AsyncSerializeLen<W: AsyncWrite>
: AsyncSerialize<W> + AsyncWriterFutureLen<W> {
fn total_bytes(val: &Self::Serialized) -> usize;
}
pub trait AsyncSerializeRef<'val, W: AsyncWrite>: AsyncWriterFuture<W> {
type Serialized;
fn from_ref(writer: W, val: &'val Self::Serialized) -> Self;
}
pub trait AsyncSerializeRefLen<'val, W: AsyncWrite>
: AsyncSerializeRef<'val, W> + AsyncWriterFutureLen<W> {
fn total_bytes(val: &Self::Serialized) -> usize;
}
pub trait AsyncDeserialize<R: AsyncRead, S, E>
: Future<Item = (R, S, usize), Error = (R, DeserializeError<E>)> {
fn from_reader(reader: R) -> Self;
fn already_read(&self) -> usize;
}
#[derive(Debug)]
pub enum DeserializeError<E> {
ReaderError(FutIoErr),
DataError(E),
}
impl<E: Display> Display for DeserializeError<E> {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
match *self {
DeserializeError::ReaderError(ref err) => {
write!(f, "Deserialize reader error: {}", err)
}
DeserializeError::DataError(ref err) => write!(f, "Deserialize data error: {}", err),
}
}
}
impl<E: Error> Error for DeserializeError<E> {
fn description(&self) -> &str {
match *self {
DeserializeError::ReaderError(ref err) => err.description(),
DeserializeError::DataError(ref err) => err.description(),
}
}
fn cause(&self) -> Option<&Error> {
match *self {
DeserializeError::ReaderError(ref err) => Some(err),
DeserializeError::DataError(ref err) => Some(err),
}
}
}
impl<E> From<FutIoErr> for DeserializeError<E> {
fn from(err: FutIoErr) -> DeserializeError<E> {
DeserializeError::ReaderError(err)
}
}