Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add array conversions #6

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 78 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,6 @@ impl<const N: usize> Bytes<N> {
Bytes::from(Vec::new())
}

// /// Construct a new, empty `Bytes<N>` with the specified capacity.
// pub fn with_capacity(cap: usize) -> Self {
// Bytes<N>::from(Vec::with_capacity(cap))
// }

/// Wrap existing bytes in a `Bytes<N>`.
pub fn from<T: Into<Vec<u8, N>>>(bytes: T) -> Self {
Bytes {
bytes: bytes.into(),
}
}

/// Unwraps the Vec<u8, N>, same as `into_vec`.
pub fn into_inner(self) -> Vec<u8, N> {
self.bytes
Expand Down Expand Up @@ -183,6 +171,30 @@ impl<const N: usize> Bytes<N> {
self.bytes.resize_default(self.bytes.capacity()).ok();
}

/// Copy the contents of this `Bytes` instance into a new instance with a higher capacity.
///
/// ```
/// # use heapless_bytes::Bytes;
/// let bytes32: Bytes<32> = Bytes::from([0; 32]);
/// let bytes64: Bytes<64> = bytes32.increase_capacity();
/// assert_eq!(bytes64.len(), 32);
/// assert_eq!(bytes64.capacity(), 64);
/// ```
///
/// Decreasing the capacity causes a compiler error:
/// ```compile_fail
/// # use heapless_bytes::Bytes;
/// let bytes32: Bytes<32> = Bytes::from([0; 32]);
/// let bytes16: Bytes<16> = bytes32.increase_capacity();
/// ```
pub fn increase_capacity<const M: usize>(&self) -> Bytes<M> {
let () = AssertLessThanEq::<N, M>::ASSERT;
let mut bytes = Vec::new();
// bytes has length 0 and capacity M, self has length N, N <= M, so this can never panic
bytes.extend_from_slice(self.as_slice()).unwrap();
bytes.into()
}

/// Fallible conversion into differently sized byte buffer.
pub fn to_bytes<const M: usize>(&self) -> Result<Bytes<M>, ()> {
Bytes::<M>::from_slice(self)
Expand Down Expand Up @@ -211,6 +223,60 @@ impl<const N: usize> Bytes<N> {
}
}

/// Construct a `Bytes<N>` instance from an array with `N` elements.
///
/// Currently, the array is copied, but a more efficient implementation could be used in the
/// future.
///
/// ```
/// # use heapless_bytes::Bytes;
/// let bytes: Bytes<3> = Bytes::from([0, 1, 2]);
/// ```
///
/// Length mismatches cause a compiler error:
/// ```compile_fail
/// # use heapless_bytes::Bytes;
/// let bytes: Bytes<3> = Bytes::from([0, 1]); // does not compile
/// ```
/// ```compile_fail
/// # use heapless_bytes::Bytes;
/// let bytes: Bytes<3> = Bytes::from([0, 1, 2, 3]); // does not compile
/// ```
impl<const N: usize> From<[u8; N]> for Bytes<N> {
fn from(bytes: [u8; N]) -> Self {
Self::from(&bytes)
}
}

struct AssertLessThanEq<const I: usize, const J: usize>;

impl<const I: usize, const J: usize> AssertLessThanEq<I, J> {
const ASSERT: () = assert!(I <= J, "Cannot convert infallibly between two arrays when the capacity of the new array is not sufficient");
}

/// Construct a `Bytes<N>` instance by copying from an array with `N` or less elements.
///
/// ```
/// # use heapless_bytes::Bytes;
/// let bytes: Bytes<3> = Bytes::from(&[0, 1, 2]);
/// let shorter_bytes: Bytes<3> = Bytes::from(&[0, 1]);
/// ```
///
/// Overlong input data causes a compiler error:
/// ```compile_fail
/// # use heapless_bytes::Bytes;
/// let bytes: Bytes<3> = Bytes::from(&[0, 1, 2, 3]); // does not compile
/// ```
impl<const N: usize, const M: usize> From<&[u8; M]> for Bytes<N> {
fn from(bytes: &[u8; M]) -> Self {
let () = AssertLessThanEq::<M, N>::ASSERT;
let mut vec = Vec::new();
// vec has length 0 and capacity N, bytes has length M, M <= N, so this can never panic
vec.extend_from_slice(bytes).unwrap();
vec.into()
}
}

// impl<N, E, F> TryFrom<F> for Bytes<N>
// where
// N: ArrayLength<u8>,
Expand Down