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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use Debug;
use BytesBuf;
use ;
/// Allows for reading of bytes.
///
/// Only supports asynchronous access, exposing the read byte sequences as `bytesbuf::BytesBuf`.
///
/// # Ownership
///
/// The methods on this trait accept `&mut self` and take an exclusive reference to the source for
/// the duration of the operation. This implies that only one read operation can be concurrently
/// executed on the object.
///
/// # Memory management for efficient I/O
///
/// For optimal efficiency when performing I/O, reads should be performed into memory optimized
/// for the underlying I/O endpoint. This is achieved by reserving memory from the implementation's
/// memory provider before performing the read operation.
///
/// There are three ways to ensure you are using memory suitable for optimally efficient I/O:
///
/// 1. If you call methods that do not accept a `BytesBuf` (such as `read_any()`), the
/// implementation will reserve memory from its memory provider internally. This is the simplest way
/// to perform reads but only a limited API surface is available in this mode.
/// 2. You may call [`Memory::reserve()`][2] on the implementation to reserve memory from its memory
/// provider explicitly. This allows you to control the memory allocation more finely and
/// potentially reuse existing buffers, improving efficiency.
/// 3. You may sometimes want to call `reserve()` at certain times when Rust borrowing rules do
/// not allow you to call it directly on the implementation because it has already been borrowed.
/// In this case, you can obtain an independent reference to the memory provider first via
/// [`HasMemory::memory()`][1], which allows you to bypass the need to borrow the implementing object itself.
///
/// Some implementations do not perform real I/O and only move data around in memory. Such
/// implementations typically do not have any special memory requirements and will operate
/// with the same efficiency regardless of which buffers the data is in. Any relaxed behaviors
/// like this will typically be described in the implementation's API documentation.
///
/// # Thread safety
///
/// This trait requires `Send` from both the implementation and any returned futures.
///
/// [1]: bytesbuf::mem::HasMemory::memory
/// [2]: bytesbuf::mem::Memory::reserve