Remove default fature requirement and add an heapless-0.8 feature

This commit is contained in:
Sosthène Guédon
2024-06-20 09:50:08 +02:00
parent 818c08df41
commit 287bb1ca31
2 changed files with 6 additions and 4 deletions
+2 -1
View File
@@ -19,4 +19,5 @@ serde = { version = "1.0", default-features = false }
serde_test = "1.0.176"
[features]
default = []
# From/Into implementation to `heapless::Vec<u8, N>`
"heapless-0.8" = []
+4 -3
View File
@@ -5,9 +5,6 @@
#![cfg_attr(not(test), no_std)]
#![allow(clippy::result_unit_err)]
#[cfg(not(feature = "default"))]
compile_error!("default features are required so that functionality can be put behind a feature flag in the future");
use core::{
cmp::Ordering,
fmt::{self, Debug},
@@ -32,12 +29,14 @@ pub type Bytes16 = Bytes<16>;
pub type Bytes32 = Bytes<32>;
pub type Bytes64 = Bytes<64>;
#[cfg(feature = "heapless-0.8")]
impl<const N: usize, const M: usize> From<Vec<u8, M>> for Bytes<N> {
fn from(vec: Vec<u8, M>) -> Self {
Bytes { bytes: vec }.increase_capacity()
}
}
#[cfg(feature = "heapless-0.8")]
impl<const N: usize, const M: usize> From<Bytes<M>> for Vec<u8, N> {
fn from(value: Bytes<M>) -> Self {
value.increase_capacity().bytes
@@ -600,7 +599,9 @@ mod tests {
fn from() {
let _: Bytes<10> = [0; 10].into();
let _: Bytes<10> = (&[0; 8]).into();
#[cfg(feature = "heapless-0.8")]
let _: Bytes<10> = Vec::<u8, 10>::new().into();
#[cfg(feature = "heapless-0.8")]
let _: Bytes<10> = Vec::<u8, 9>::new().into();
}
}