Skip to content

Commit a7f67ac

Browse files
committed
more doc tests
1 parent 1289a93 commit a7f67ac

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/lib.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ const U16_SIZE: usize = 2;
180180
const U32_SIZE: usize = 4;
181181
const U64_SIZE: usize = 8;
182182

183-
/// Implement for a type to get encoding and decoding.
183+
/// A trait for building small and fast parsers and serializers.
184184
pub trait CompactEncoding<Decode: ?Sized = Self> {
185185
/// The size in bytes required to encode `self`.
186186
fn encoded_size(&self) -> Result<usize, EncodingError>;
@@ -477,7 +477,7 @@ pub fn as_array<const N: usize>(buffer: &[u8]) -> Result<&[u8; N], EncodingError
477477
Ok(buffer.split_first_chunk::<N>().expect("checked above").0)
478478
}
479479

480-
/// Get a slice as an array of size `N`. Errors when `slice.len() != N`.
480+
/// Get a slice as a mutable array of size `N`. Errors when `slice.len() != N`.
481481
pub fn as_array_mut<const N: usize>(buffer: &mut [u8]) -> Result<&mut [u8; N], EncodingError> {
482482
let blen = buffer.len();
483483
if blen != N {
@@ -621,15 +621,33 @@ pub fn decode_usize(buffer: &[u8]) -> Result<(usize, &[u8]), EncodingError> {
621621
})
622622
}
623623

624-
/// Encoded a fixed sized array to a buffer
624+
/// Encoded a fixed sized array to a buffer, return the remainder of the buffer.
625+
/// Errors when `value.len() > buffer.len()`;
626+
/// ```
627+
/// # use compact_encoding::encode_bytes_fixed;
628+
/// let mut buffer = vec![0; 3];
629+
/// let rest = encode_bytes_fixed(&[4, 2], &mut buffer)?;
630+
/// assert_eq!(rest, &[0]);
631+
/// assert_eq!(buffer, &[4, 2, 0]);
632+
/// # Ok::<(), Box<dyn std::error::Error>>(())
633+
/// ```
625634
pub fn encode_bytes_fixed<'a, const N: usize>(
626635
value: &[u8; N],
627636
buffer: &'a mut [u8],
628637
) -> Result<&'a mut [u8], EncodingError> {
629638
write_array(value, buffer)
630639
}
631640

632-
/// Encoded a fixed sized array to a buffer
641+
/// Decode a fixed sized array from a buffer. Return the array and the remainder of the buffer.
642+
/// Errors when `buffer.len() < N`;
643+
/// ```
644+
/// # use compact_encoding::decode_bytes_fixed;
645+
/// let mut buffer = vec![1, 2, 3];
646+
/// let (arr, rest) = decode_bytes_fixed::<2>(&mut buffer)?;
647+
/// assert_eq!(arr, [1, 2]);
648+
/// assert_eq!(rest, &[3]);
649+
/// # Ok::<(), Box<dyn std::error::Error>>(())
650+
/// ```
633651
pub fn decode_bytes_fixed<const N: usize>(
634652
buffer: &[u8],
635653
) -> Result<([u8; N], &[u8]), EncodingError> {

0 commit comments

Comments
 (0)