diff --git a/src/lib.rs b/src/lib.rs index c7c25c4..6b5845d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,18 +42,6 @@ impl Bytes { Bytes::from(Vec::new()) } - // /// Construct a new, empty `Bytes` with the specified capacity. - // pub fn with_capacity(cap: usize) -> Self { - // Bytes::from(Vec::with_capacity(cap)) - // } - - /// Wrap existing bytes in a `Bytes`. - pub fn from>>(bytes: T) -> Self { - Bytes { - bytes: bytes.into(), - } - } - /// Unwraps the Vec, same as `into_vec`. pub fn into_inner(self) -> Vec { self.bytes @@ -183,6 +171,30 @@ impl Bytes { 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(&self) -> Bytes { + let () = AssertLessThanEq::::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(&self) -> Result, ()> { Bytes::::from_slice(self) @@ -211,6 +223,60 @@ impl Bytes { } } +/// Construct a `Bytes` 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 From<[u8; N]> for Bytes { + fn from(bytes: [u8; N]) -> Self { + Self::from(&bytes) + } +} + +struct AssertLessThanEq; + +impl AssertLessThanEq { + const ASSERT: () = assert!(I <= J, "Cannot convert infallibly between two arrays when the capacity of the new array is not sufficient"); +} + +/// Construct a `Bytes` 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 From<&[u8; M]> for Bytes { + fn from(bytes: &[u8; M]) -> Self { + let () = AssertLessThanEq::::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 TryFrom for Bytes // where // N: ArrayLength,