From 257a0bf6497fc5eea64d268dd206615e6a43fe17 Mon Sep 17 00:00:00 2001 From: Nicolas Stalder Date: Thu, 10 Jun 2021 22:45:31 +0200 Subject: [PATCH] Bump heapless, removing generic-array --- Cargo.toml | 4 +-- src/lib.rs | 100 +++++++++++++++++++++++++++-------------------------- 2 files changed, 53 insertions(+), 51 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ec49f91..4ec00c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "heapless-bytes" -version = "0.2.0" +version = "0.3.0" authors = ["Nicolas Stalder "] license = "Apache-2.0 OR MIT" description = "Newtype around heapless byte Vec with efficient serde." @@ -14,7 +14,7 @@ edition = "2018" typenum = "1.11.2" [dependencies.heapless] -version = "0.6" +version = "0.7" default-features = false [dependencies.serde] diff --git a/src/lib.rs b/src/lib.rs index 3b075f7..cb0cd8a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,8 +13,6 @@ use core::{ ptr, }; -pub use heapless::consts; -pub use heapless::ArrayLength; // bring trait in scope to use `consts::Uxx::USIZE` etc., // to get the corresponding size of Bytes. pub use typenum::{IsGreaterOrEqual, True, Unsigned}; @@ -27,22 +25,22 @@ use serde::{ }; #[derive(Clone, Default, Eq)] -pub struct Bytes> { +pub struct Bytes { bytes: Vec, } -pub type Bytes8 = Bytes; -pub type Bytes16 = Bytes; -pub type Bytes32 = Bytes; -pub type Bytes64 = Bytes; +pub type Bytes8 = Bytes<8>; +pub type Bytes16 = Bytes<16>; +pub type Bytes32 = Bytes<32>; +pub type Bytes64 = Bytes<64>; -impl> From> for Bytes { +impl From> for Bytes { fn from(vec: Vec) -> Self { Self { bytes: vec } } } -impl> Bytes { +impl Bytes { /// Construct a new, empty `Bytes`. pub fn new() -> Self { Bytes::from(Vec::new()) @@ -85,8 +83,8 @@ impl> Bytes { /// Low-noise conversion between lengths. /// /// We can't implement TryInto since it would clash with blanket implementations. - pub fn try_convert_into>(&self) -> Result, ()> { - Bytes::::try_from_slice(self) + pub fn try_convert_into(&self) -> Result, ()> { + Bytes::::from_slice(self) } // #[doc(hidden)] @@ -94,7 +92,13 @@ impl> Bytes { // self.bytes.into_iter() // } - pub fn try_from_slice(slice: &[u8]) -> core::result::Result { + // pub fn try_from_slice(slice: &[u8]) -> core::result::Result { + // let mut bytes = Vec::::new(); + // bytes.extend_from_slice(slice)?; + // Ok(Self::from(bytes)) + // } + + pub fn from_slice(slice: &[u8]) -> core::result::Result { let mut bytes = Vec::::new(); bytes.extend_from_slice(slice)?; Ok(Self::from(bytes)) @@ -190,25 +194,29 @@ impl> Bytes { self.bytes.resize_default(self.bytes.capacity()).ok(); } - /// Clone into at least same size byte buffer. - pub fn to_bytes(&self) -> Bytes - where - M: ArrayLength + IsGreaterOrEqual, - { - match Bytes::::try_from_slice(self) { - Ok(byte_buf) => byte_buf, - _ => unreachable!(), - } - } + // /// Clone into at least same size byte buffer. + // pub fn to_bytes(&self) -> Bytes + // where + // M: ArrayLength + IsGreaterOrEqual, + // { + // match Bytes::::try_from_slice(self) { + // Ok(byte_buf) => byte_buf, + // _ => unreachable!(), + // } + // } /// Fallible conversion into differently sized byte buffer. - pub fn try_to_bytes(&self) -> Result, ()> - where - M: ArrayLength, + pub fn to_bytes(&self) -> Result, ()> { - Bytes::::try_from_slice(self) + Bytes::::from_slice(self) } + // /// Fallible conversion into differently sized byte buffer. + // pub fn try_to_bytes(&self) -> Result, ()> + // { + // Bytes::::from_slice(self) + // } + // pub fn deref_mut(&mut self) -> &mut [u8] { // self.bytes.deref_mut() // } @@ -219,7 +227,7 @@ impl> Bytes { T: Serialize, { let mut vec = Vec::::new(); - vec.resize_default(N::to_usize()).unwrap(); + vec.resize_default(N).unwrap(); let buffer = vec.deref_mut(); let writer = serde_cbor::ser::SliceWrite::new(buffer); @@ -256,7 +264,7 @@ impl> Bytes { // } // } -impl> Debug for Bytes { +impl Debug for Bytes { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // TODO: There has to be a better way :'-) @@ -274,19 +282,19 @@ impl> Debug for Bytes { } } -impl> AsRef<[u8]> for Bytes { +impl AsRef<[u8]> for Bytes { fn as_ref(&self) -> &[u8] { &self.bytes } } -impl> AsMut<[u8]> for Bytes { +impl AsMut<[u8]> for Bytes { fn as_mut(&mut self) -> &mut [u8] { &mut self.bytes } } -impl> Deref for Bytes { +impl Deref for Bytes { type Target = Vec; fn deref(&self) -> &Self::Target { @@ -294,7 +302,7 @@ impl> Deref for Bytes { } } -impl> DerefMut for Bytes { +impl DerefMut for Bytes { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.bytes } @@ -312,7 +320,7 @@ impl> DerefMut for Bytes { // } // } -impl, Rhs> PartialEq for Bytes +impl PartialEq for Bytes where Rhs: ?Sized + AsRef<[u8]>, { @@ -321,7 +329,7 @@ where } } -impl, Rhs> PartialOrd for Bytes +impl PartialOrd for Bytes where Rhs: ?Sized + AsRef<[u8]>, { @@ -330,13 +338,13 @@ where } } -impl> Hash for Bytes { +impl Hash for Bytes { fn hash(&self, state: &mut H) { self.bytes.hash(state); } } -impl> IntoIterator for Bytes { +impl IntoIterator for Bytes { type Item = u8; type IntoIter = as IntoIterator>::IntoIter; @@ -345,7 +353,7 @@ impl> IntoIterator for Bytes { } } -impl<'a, N: ArrayLength> IntoIterator for &'a Bytes { +impl<'a, const N: usize> IntoIterator for &'a Bytes { type Item = &'a u8; type IntoIter = <&'a [u8] as IntoIterator>::IntoIter; @@ -354,7 +362,7 @@ impl<'a, N: ArrayLength> IntoIterator for &'a Bytes { } } -impl<'a, N: ArrayLength> IntoIterator for &'a mut Bytes { +impl<'a, const N: usize> IntoIterator for &'a mut Bytes { type Item = &'a mut u8; type IntoIter = <&'a mut [u8] as IntoIterator>::IntoIter; @@ -363,9 +371,7 @@ impl<'a, N: ArrayLength> IntoIterator for &'a mut Bytes { } } -impl Serialize for Bytes -where - N: ArrayLength, +impl Serialize for Bytes { fn serialize(&self, serializer: S) -> Result where @@ -376,19 +382,15 @@ where } // TODO: can we delegate to Vec deserialization instead of reimplementing? -impl<'de, N> Deserialize<'de> for Bytes -where - N: ArrayLength, +impl<'de, const N: usize> Deserialize<'de> for Bytes { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { - struct ValueVisitor<'de, N>(PhantomData<(&'de (), N)>); + struct ValueVisitor<'de, const N: usize>(PhantomData<&'de ()>); - impl<'de, N> Visitor<'de> for ValueVisitor<'de, N> - where - N: ArrayLength, + impl<'de, const N: usize> Visitor<'de> for ValueVisitor<'de, N> { // type Value = Vec; type Value = Bytes; @@ -401,7 +403,7 @@ where where E: serde::de::Error, { - if v.len() > N::to_usize() { + if v.len() > N { // hprintln!("error! own size: {}, data size: {}", N::to_usize(), v.len()).ok(); // return Err(E::invalid_length(values.capacity() + 1, &self))?; return Err(E::invalid_length(v.len(), &self))?;