Skip to content

Commit 2e5e0f1

Browse files
committed
More docs! (and other fixes)
1 parent fb6e83e commit 2e5e0f1

File tree

9 files changed

+60
-10
lines changed

9 files changed

+60
-10
lines changed

atom/src/space.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! A collection of tools to assist reading and writing custom Atom types in Atom byte buffers (referred as **Spaces**).
2+
#![deny(missing_docs)]
23

34
mod aligned;
45
mod allocator;
@@ -15,4 +16,4 @@ pub use atom_writer::AtomWriter;
1516
pub use cursor::SpaceCursor;
1617
pub use reader::SpaceReader;
1718
pub use terminated::Terminated;
18-
pub use vec::{AlignedVec, VecSpaceCursor};
19+
pub use vec::{AlignedVec, AlignedVecCursor};

atom/src/space/aligned.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,20 @@ pub struct AlignedSpace<T> {
4949
data: [u8],
5050
}
5151

52+
/// A slice of bytes that is properly aligned to contain Atoms (i.e. is 64-bit aligned).
53+
///
54+
/// See [`AlignedSpace`] for more information.
5255
pub type AtomSpace = AlignedSpace<AtomHeader>;
5356

5457
impl<T: 'static> AlignedSpace<T> {
58+
/// Creates a new boxed space from an already aligned, potentially uninitialized boxed slice of T.
59+
///
60+
/// This method provides a lightweight conversion: it does not copy the contents nor perform any
61+
/// (re)allocations.
5562
#[inline]
5663
pub fn from_boxed_uninit_slice(slice: Box<[MaybeUninit<T>]>) -> Box<Self> {
57-
// Safety: casting MaybeUninit<T> to [u8] is always safe
64+
// SAFETY: casting MaybeUninit<T> to [u8] is always safe.
65+
// SAFETY: because this is MaybeUninit<T>, the slice is always aligned.
5866
unsafe { core::mem::transmute(slice) }
5967
}
6068

atom/src/space/allocator.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use core::mem::{size_of, size_of_val, MaybeUninit};
1111
/// This structure allows simultaneous access to both the newly allocated slice, and all previously
1212
/// allocated bytes.
1313
pub struct SpaceWriterSplitAllocation<'a> {
14+
/// All previously allocated bytes. This is guaranteed to be contiguous with `allocated`.
1415
pub previous: &'a mut [u8],
16+
/// The requested newly-allocated bytes
1517
pub allocated: &'a mut [u8],
1618
}
1719

@@ -21,12 +23,19 @@ pub struct SpaceWriterSplitAllocation<'a> {
2123
/// previous one.
2224
///
2325
/// This trait is very bare-bones, in order to be trait-object-safe. As an user, you probably want
24-
/// to use the [`SpaceWriter`] trait, a child trait with many more utilities available, and with a
26+
/// to use the [`SpaceWriter`] trait, an extension trait with many more utilities available, and with a
2527
/// blanket implementation for all types that implement [`SpaceAllocator`].
2628
///
2729
/// The term "allocate" is used very loosely here, as even a simple cursor over a mutable byte
2830
/// buffer (e.g. [`SpaceCursor`](crate::space::SpaceCursor)) can "allocate" bytes using this trait.
2931
///
32+
/// "Allocations" made using this trait dot not imply actual system allocations. However, some
33+
/// implementations (such as [`AlignedVecCursor`](crate::space::AlignedVecCursor)) might perform a
34+
/// system reallocation if the requested space exceeds its internal capacity.
35+
///
36+
/// Other implementations, such as [`SpaceCursor`](crate::space::SpaceCursor), are guaranteed to
37+
/// never allocate, and will instead return an error when exceeding its capacity.
38+
///
3039
/// This trait is useful to abstract over many types of buffers, including ones than can track the
3140
/// amount of allocated bytes into an atom header (i.e. [`AtomWriter`]).
3241
pub trait SpaceAllocator {
@@ -79,6 +88,7 @@ pub trait SpaceAllocator {
7988
fn remaining_bytes(&self) -> &[u8];
8089
}
8190

91+
///
8292
pub trait SpaceWriter: SpaceAllocator + Sized {
8393
/// Allocates and returns a new mutable byte buffer of the requested size, in bytes.
8494
///

atom/src/space/cursor.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ use crate::space::error::AtomWriteError;
22
use crate::space::{SpaceAllocator, SpaceWriterSplitAllocation};
33

44
/// A lightweight [`SpaceWriter`](crate::space::SpaceWriter) that writes into a mutable byte buffer using a cursor.
5+
///
6+
/// This cursor is backed by a simple mutable byte slice, and is therefore guaranteed to never
7+
/// allocate.
8+
///
9+
/// If the capacity of the underlying buffer is exceeded, an [`AtomWriteError::OutOfSpace`] error is
10+
/// returned.
511
pub struct SpaceCursor<'a> {
612
data: &'a mut [u8],
713
allocated_length: usize,

atom/src/space/error.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Errors related to the alignment, reading or writing of Atoms.
2+
13
use std::error::Error;
24
use std::fmt::{Display, Formatter};
35
use urid::{Uri, URID};
@@ -136,6 +138,7 @@ pub enum AtomWriteError {
136138
/// An user-friendly error message
137139
error_message: &'static str,
138140
},
141+
/// An alignment error occurred when attempting to write to the underlying buffer.
139142
AlignmentError(AlignmentError),
140143
}
141144

@@ -182,27 +185,43 @@ impl Display for AtomWriteError {
182185

183186
impl Error for AtomWriteError {}
184187

188+
/// Errors that can occur while writing atoms to a byte buffer.
185189
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
186190
#[non_exhaustive]
187191
pub enum AtomReadError {
192+
/// The URID of the atom being currently read does not match the requested type's URID.
188193
AtomUridMismatch {
194+
/// The full URI of the requested type
189195
expected_uri: &'static Uri,
196+
/// The URID of the requested type
190197
expected_urid: URID,
198+
/// The atom's actual URID
191199
found_urid: URID,
192200
},
201+
/// The URID of the atom being currently read is not actually a valid URID (e.g. zero).
193202
InvalidUrid {
203+
/// The full URI of the requested type
194204
expected_uri: &'static Uri,
205+
/// The URID of the requested type
195206
expected_urid: URID,
207+
/// The value found in the Atom's URID header.
196208
found_urid: u32,
197209
},
210+
/// A read operation tried to occur outside of the buffer's bounds
198211
ReadingOutOfBounds {
212+
/// The amount of available bytes in the buffer
199213
available: usize,
214+
/// The requested amount of bytes
200215
requested: usize,
201216
},
217+
/// The read Atom value was invalid for the given Atom type.
202218
InvalidAtomValue {
219+
/// The Atom type being read
203220
reading_type_uri: &'static Uri,
221+
/// The Atom-specific error message
204222
error_message: &'static str,
205223
},
224+
/// An alignment error curred when trying to read from the underlying buffer.
206225
AlignmentError(AlignmentError),
207226
}
208227

@@ -255,9 +274,12 @@ impl Display for AtomReadError {
255274

256275
impl Error for AtomReadError {}
257276

277+
/// The global atom error type, that encompasses both read and write errors, for convenience.
258278
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
259279
pub enum AtomError {
280+
/// An error occurred when trying to read an atom from a buffer.
260281
ReadError(AtomReadError),
282+
/// An error occurred when trying to write an atom into a buffer.
261283
WriteError(AtomWriteError),
262284
}
263285

atom/src/space/reader.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ fn split_space<T: 'static>(
2121
}
2222

2323
impl<'a> SpaceReader<'a> {
24+
/// Creates a new reader from a byte slice.
2425
#[inline]
2526
pub fn new(space: &'a [u8]) -> Self {
2627
SpaceReader { space }
@@ -166,6 +167,7 @@ impl<'a> SpaceReader<'a> {
166167
Ok(atom)
167168
}
168169

170+
/// Returns the currently remaining underlying bytes.
169171
#[inline]
170172
pub fn remaining_bytes(&self) -> &'a [u8] {
171173
self.space

atom/src/space/vec.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ impl<T: Copy + 'static> AlignedVec<T> {
124124
/// Unlike other [`SpaceWriter`](crate::space::SpaceWriter) implementations, this cursor grows the underlying
125125
/// [`AlignedVec`] buffer if it runs out of space, instead of failing.
126126
#[inline]
127-
pub fn write(&mut self) -> VecSpaceCursor<T> {
128-
VecSpaceCursor {
127+
pub fn write(&mut self) -> AlignedVecCursor<T> {
128+
AlignedVecCursor {
129129
vec: self,
130130
allocated_length: 0,
131131
}
@@ -153,12 +153,12 @@ impl<T: Copy + 'static> AlignedVec<T> {
153153
/// [`AlignedVec`] buffer if it runs out of space, instead of failing.
154154
///
155155
/// This cursor is obtained through the [`AlignedVec::write`] method.
156-
pub struct VecSpaceCursor<'vec, T> {
156+
pub struct AlignedVecCursor<'vec, T> {
157157
vec: &'vec mut AlignedVec<T>,
158158
allocated_length: usize,
159159
}
160160

161-
impl<'vec, T: Copy + 'static> SpaceAllocator for VecSpaceCursor<'vec, T> {
161+
impl<'vec, T: Copy + 'static> SpaceAllocator for AlignedVecCursor<'vec, T> {
162162
fn allocate_and_split(
163163
&mut self,
164164
size: usize,

atom/src/unidentified.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use urid::URID;
55

66
/// An atom of yet unknown type.
77
///
8-
/// This is used by reading handles that have to return a reference to an atom, but can not check it's type. This struct contains a `Space` containing the header and the body of the atom and can identify/read the atom from it.
8+
/// This is used by reading handles that have to return a reference to an atom, but cannot check its type.
9+
/// This struct contains a `Space` containing the header and the body of the atom and can identify/read the atom from it.
910
#[repr(C)]
1011
pub struct UnidentifiedAtom {
1112
header: AtomHeader,

state/src/raw.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ impl<'a> StoreHandle<'a> {
101101

102102
/// Writing handle for properties.
103103
pub struct StatePropertyWriter<'a> {
104-
cursor: VecSpaceCursor<'a, AtomHeader>,
104+
cursor: AlignedVecCursor<'a, AtomHeader>,
105105
initialized: bool,
106106
}
107107

108108
impl<'a> StatePropertyWriter<'a> {
109109
/// Create a new property writer that uses the given space head.
110-
pub fn new(cursor: VecSpaceCursor<'a, AtomHeader>) -> Self {
110+
pub fn new(cursor: AlignedVecCursor<'a, AtomHeader>) -> Self {
111111
Self {
112112
cursor,
113113
initialized: false,

0 commit comments

Comments
 (0)