Skip to content

Commit f623049

Browse files
Serban Iorgaalxiord
authored andcommitted
[bytes.rs] documentation improvements and fixes
Signed-off-by: Serban Iorga <seriorga@amazon.com>
1 parent 5203197 commit f623049

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

src/bytes.rs

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -145,45 +145,61 @@ pub trait Bytes<A> {
145145
/// Associated error codes
146146
type E;
147147

148-
/// Writes a slice into the container at the specified address.
148+
/// Writes a slice into the container at `addr`.
149+
///
149150
/// Returns the number of bytes written. The number of bytes written can
150151
/// be less than the length of the slice if there isn't enough room in the
151152
/// container.
152153
fn write(&self, buf: &[u8], addr: A) -> Result<usize, Self::E>;
153154

154-
/// Reads to a slice from the container at the specified address.
155+
/// Reads data from the container at `addr` into a slice.
156+
///
155157
/// Returns the number of bytes read. The number of bytes read can be less than the length
156-
/// of the slice if there isn't enough room within the container.
158+
/// of the slice if there isn't enough data within the container.
157159
fn read(&self, buf: &mut [u8], addr: A) -> Result<usize, Self::E>;
158160

159-
/// Writes the entire contents of a slice into the container at the specified address.
161+
/// Writes the entire content of a slice into the container at `addr`.
162+
///
163+
/// # Errors
160164
///
161-
/// Returns an error if there isn't enough room within the container to complete the entire
162-
/// write. Part of the data may have been written nevertheless.
165+
/// Returns an error if there isn't enough space within the container to write the entire slice.
166+
/// Part of the data may have been copied nevertheless.
163167
fn write_slice(&self, buf: &[u8], addr: A) -> Result<(), Self::E>;
164168

165-
/// Reads from the container at the specified address to fill the entire buffer.
169+
/// Reads data from the container at `addr` to fill an entire slice.
166170
///
167-
/// Returns an error if there isn't enough room within the container to fill the entire buffer.
168-
/// Part of the buffer may have been filled nevertheless.
171+
/// # Errors
172+
///
173+
/// Returns an error if there isn't enough data within the container to fill the entire slice.
174+
/// Part of the data may have been copied nevertheless.
169175
fn read_slice(&self, buf: &mut [u8], addr: A) -> Result<(), Self::E>;
170176

171-
/// Writes an object into the container at the specified address.
172-
/// Returns Ok(()) if the object fits, or Err if it extends past the end.
177+
/// Writes an object into the container at `addr`.
178+
///
179+
/// # Errors
180+
///
181+
/// Returns an error if the object doesn't fit inside the container.
173182
fn write_obj<T: ByteValued>(&self, val: T, addr: A) -> Result<(), Self::E> {
174183
self.write_slice(val.as_slice(), addr)
175184
}
176185

177-
/// Reads an object from the container at the given address.
186+
/// Reads an object from the container at `addr`.
187+
///
178188
/// Reading from a volatile area isn't strictly safe as it could change mid-read.
179189
/// However, as long as the type T is plain old data and can handle random initialization,
180190
/// everything will be OK.
191+
///
192+
/// # Errors
193+
///
194+
/// Returns an error if there's not enough data inside the container.
181195
fn read_obj<T: ByteValued>(&self, addr: A) -> Result<T, Self::E> {
182196
let mut result: T = Default::default();
183197
self.read_slice(result.as_mut_slice(), addr).map(|_| result)
184198
}
185199

186-
/// Writes data from a readable object like a File and writes it into the container.
200+
/// Reads up to `count` bytes from an object and writes them into the container at `addr`.
201+
///
202+
/// Returns the number of bytes written into the container.
187203
///
188204
/// # Arguments
189205
/// * `addr` - Begin writing at this address.
@@ -193,32 +209,44 @@ pub trait Bytes<A> {
193209
where
194210
F: Read;
195211

196-
/// Writes data from a readable object like a File and writes it into the container.
212+
/// Reads exactly `count` bytes from an object and writes them into the container at `addr`.
213+
///
214+
/// # Errors
215+
///
216+
/// Returns an error if `count` bytes couldn't have been copied from `src` to the container.
217+
/// Part of the data may have been copied nevertheless.
197218
///
198219
/// # Arguments
199220
/// * `addr` - Begin writing at this address.
200221
/// * `src` - Copy from `src` into the container.
201-
/// * `count` - Copy `count` bytes from `src` into the container.
222+
/// * `count` - Copy exactly `count` bytes from `src` into the container.
202223
fn read_exact_from<F>(&self, addr: A, src: &mut F, count: usize) -> Result<(), Self::E>
203224
where
204225
F: Read;
205226

206-
/// Reads data from the container to a writable object.
227+
/// Reads up to `count` bytes from the container at `addr` and writes them it into an object.
228+
///
229+
/// Returns the number of bytes written into the object.
207230
///
208231
/// # Arguments
209-
/// * `addr` - Begin reading from this addr.
232+
/// * `addr` - Begin reading from this address.
210233
/// * `dst` - Copy from the container to `dst`.
211234
/// * `count` - Copy `count` bytes from the container to `dst`.
212235
fn write_to<F>(&self, addr: A, dst: &mut F, count: usize) -> Result<usize, Self::E>
213236
where
214237
F: Write;
215238

216-
/// Reads data from the container to a writable object.
239+
/// Reads exactly `count` bytes from the container at `addr` and writes them into an object.
240+
///
241+
/// # Errors
242+
///
243+
/// Returns an error if `count` bytes couldn't have been copied from the container to `dst`.
244+
/// Part of the data may have been copied nevertheless.
217245
///
218246
/// # Arguments
219-
/// * `addr` - Begin reading from this addr.
247+
/// * `addr` - Begin reading from this address.
220248
/// * `dst` - Copy from the container to `dst`.
221-
/// * `count` - Copy `count` bytes from the container to `dst`.
249+
/// * `count` - Copy exactly `count` bytes from the container to `dst`.
222250
fn write_all_to<F>(&self, addr: A, dst: &mut F, count: usize) -> Result<(), Self::E>
223251
where
224252
F: Write;

0 commit comments

Comments
 (0)