diff --git a/CHANGELOG.md b/CHANGELOG.md index 72580d5b7c..8c42dd51ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - `bytes::BufMut` is now implemented on `VecInner`. - Removed generic from `history_buf::OldestOrdered`. +- Made `LenType` opt-in. ### Fixed @@ -19,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Removed - Removed invalid `bytes::Buf` implementation. +- Removed `DefaultLenType` struct. ## [v0.9.0] - 2025-04-28 [YANKED] diff --git a/src/c_string.rs b/src/c_string.rs index ee480a48b4..25df69b7ca 100644 --- a/src/c_string.rs +++ b/src/c_string.rs @@ -1,6 +1,6 @@ //! A fixed capacity [`CString`](https://doc.rust-lang.org/std/ffi/struct.CString.html). -use crate::{len_type::DefaultLenType, vec::Vec, CapacityError, LenType}; +use crate::{vec::Vec, CapacityError, LenType}; use core::{ borrow::Borrow, cmp::Ordering, @@ -14,7 +14,7 @@ use core::{ /// /// It stores up to `N - 1` non-nul characters with a trailing nul terminator. #[derive(Clone, Hash)] -pub struct CString> { +pub struct CString { inner: Vec, } diff --git a/src/len_type.rs b/src/len_type.rs index 854dbddda6..d42a659d9a 100644 --- a/src/len_type.rs +++ b/src/len_type.rs @@ -85,186 +85,6 @@ impl_lentype!( usize ); -macro_rules! impl_lentodefault { - ($LenT:ty: $($len:literal),*) => {$( - impl SmallestLenType for Const<$len> { - type Type = $LenT; - } - )*}; -} - -/// A struct to create individual types for mapping with [`SmallestLenType`]. -/// -/// See the documentation of [`DefaultLenType`] for a detailed explanation. -pub struct Const; - -/// A trait to map [`Const`] to it's respective [`LenType`]. -/// -/// See the documentation of [`DefaultLenType`] for a detailed explanation. -#[diagnostic::on_unimplemented( - message = "Length `N` does not have a default `LenType` mapping", - note = "Provide the `LenType` explicitly, such as `usize`" -)] -pub trait SmallestLenType { - type Type: LenType; -} - -/// A type alias to perform the `const N: usize` -> `LenType` mapping. -/// -/// This is impossible to perform directly, but it is possible to write a `const N: usize` -> related `Type` mapping via a const generic argument, -/// then map from that to an unrelated type via a trait with associated types. -/// -/// [`Const`] is the "related type" in the above explaination, [`SmallestLenType`] is the mapping trait. -#[allow(rustdoc::private_intra_doc_links)] // Only publically exposed via `crate::_export` -pub type DefaultLenType = as SmallestLenType>::Type; - -impl_lentodefault!(ZeroLenType: 0); -impl_lentodefault!(u8: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255); -impl_lentodefault!(u16: 256, 300, 400, 500, 512, 600, 700, 800, 900, 1000, 1024, 2000, 2048, 4000, 4096, 8000, 8192, 16000, 16384, 32000, 32768, 65000, 65535); -#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))] -impl_lentodefault!(u32: 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648); - pub const fn check_capacity_fits() { assert!(LenT::MAX_USIZE >= N, "The capacity is larger than `LenT` can hold, increase the size of `LenT` or reduce the capacity"); } - -/// Container with 0 capacity always has length 0, so there is no need to track length of such containers at all. -/// -/// This type is used as a placeholder for length of container with 0 capacity. It allows optimizing total size of -/// containers like this to 0 bytes. -/// -/// Logically, this type always stores value 0. Because of this ZeroLenType::one() panics and should never be called. -#[doc(hidden)] -#[derive(Copy, Clone, PartialEq, PartialOrd)] -pub struct ZeroLenType; - -impl Debug for ZeroLenType { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!(f, "0") - } -} - -impl Display for ZeroLenType { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!(f, "0") - } -} - -impl Sealed for ZeroLenType { - const ZERO: Self = Self; - - const MAX: Self = Self; - const MAX_USIZE: usize = 0; - - #[inline] - fn one() -> Self { - panic!("ZeroLenType cannot represent value 1"); - } -} - -impl LenType for ZeroLenType {} - -impl Add for ZeroLenType { - type Output = Self; - - fn add(self, _rhs: Self) -> Self::Output { - Self::ZERO - } -} - -impl AddAssign for ZeroLenType { - fn add_assign(&mut self, _rhs: Self) {} -} - -impl Sub for ZeroLenType { - type Output = Self; - - fn sub(self, _rhs: Self) -> Self::Output { - Self::ZERO - } -} - -impl SubAssign for ZeroLenType { - fn sub_assign(&mut self, _rhs: Self) {} -} - -#[doc(hidden)] -#[derive(Debug, PartialEq)] -pub struct ZeroLenTypeTryFromError; - -impl TryFrom for ZeroLenType { - type Error = ZeroLenTypeTryFromError; - - fn try_from(value: usize) -> Result { - if value > 0 { - return Err(ZeroLenTypeTryFromError); - } - - Ok(Self::ZERO) - } -} - -impl TryInto for ZeroLenType { - type Error = (); - - fn try_into(self) -> Result { - Ok(0) - } -} - -#[cfg(test)] -mod tests { - use crate::len_type::{Sealed, ZeroLenType, ZeroLenTypeTryFromError}; - - #[test] - pub fn test_zero_len_type_conversions() { - assert_eq!(ZeroLenType::into_usize(ZeroLenType::ZERO), 0_usize); - assert_eq!(ZeroLenType::from_usize(0_usize), ZeroLenType::ZERO); - - assert_eq!(ZeroLenType::ZERO.try_into(), Ok(0_usize)); - assert_eq!(ZeroLenType::try_from(0_usize), Ok(ZeroLenType::ZERO)); - assert_eq!(ZeroLenType::try_from(1_usize), Err(ZeroLenTypeTryFromError)); - } - - #[test] - #[should_panic] - pub fn test_zero_len_type_one() { - ZeroLenType::one(); - } - - #[test] - #[should_panic] - pub fn test_zero_len_type_one_usize() { - ZeroLenType::from_usize(1); - } - - #[test] - pub fn test_zero_len_type_constants() { - assert_eq!(ZeroLenType::ZERO, ZeroLenType); - assert_eq!(ZeroLenType::MAX, ZeroLenType); - assert_eq!(ZeroLenType::MAX_USIZE, 0_usize); - } - - #[test] - pub fn test_zero_len_type_size() { - assert_eq!(core::mem::size_of::(), 0); - } - - #[test] - pub fn test_zero_len_type_ops() { - assert_eq!(ZeroLenType::ZERO + ZeroLenType::ZERO, ZeroLenType::ZERO); - assert_eq!(ZeroLenType::ZERO - ZeroLenType::ZERO, ZeroLenType::ZERO); - - let mut zero = ZeroLenType::ZERO; - zero += ZeroLenType::ZERO; - assert_eq!(zero, ZeroLenType::ZERO); - zero -= ZeroLenType::ZERO; - assert_eq!(zero, ZeroLenType::ZERO); - } - - #[test] - pub fn test_zero_len_type_debug() { - assert_eq!(format!("{}", ZeroLenType), "0"); - assert_eq!(format!("{:?}", ZeroLenType), "0"); - } -} diff --git a/src/lib.rs b/src/lib.rs index 80c57dfd8b..f9880690cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -241,7 +241,6 @@ mod ufmt; /// Do not use. Used for macros only. Not covered by semver guarantees. #[doc(hidden)] pub mod _export { - pub use crate::len_type::DefaultLenType; pub use crate::string::format; } diff --git a/src/sorted_linked_list.rs b/src/sorted_linked_list.rs index 899de283fd..d319fe0f6d 100644 --- a/src/sorted_linked_list.rs +++ b/src/sorted_linked_list.rs @@ -8,7 +8,7 @@ //! //! ``` //! use heapless::sorted_linked_list::{Max, SortedLinkedList}; -//! let mut ll: SortedLinkedList<_, Max, 3> = SortedLinkedList::new_u8(); +//! let mut ll: SortedLinkedList<_, Max, 3, u8> = SortedLinkedList::new_u8(); //! //! // The largest value will always be first //! ll.push(1).unwrap(); @@ -156,7 +156,7 @@ pub use storage::{ OwnedSortedLinkedListStorage, SortedLinkedListStorage, ViewSortedLinkedListStorage, }; -use crate::len_type::{DefaultLenType, LenType}; +use crate::len_type::LenType; /// Marker for Min sorted [`SortedLinkedList`]. pub struct Min; @@ -212,7 +212,7 @@ where } /// The linked list. -pub type SortedLinkedList> = +pub type SortedLinkedList = SortedLinkedListInner>; /// The linked list. @@ -391,7 +391,7 @@ where /// /// ``` /// use heapless::sorted_linked_list::{Max, SortedLinkedList}; - /// let mut ll: SortedLinkedList<_, Max, 3> = SortedLinkedList::new_u8(); + /// let mut ll: SortedLinkedList<_, Max, 3, u8> = SortedLinkedList::new_u8(); /// /// // The largest value will always be first /// ll.push(1).unwrap(); @@ -421,7 +421,7 @@ where /// /// ``` /// use heapless::sorted_linked_list::{Max, Min, SortedLinkedList}; - /// let mut ll_max: SortedLinkedList<_, Max, 3> = SortedLinkedList::new_u8(); + /// let mut ll_max: SortedLinkedList<_, Max, 3, u8> = SortedLinkedList::new_u8(); /// /// // The largest value will always be first /// ll_max.push(1).unwrap(); @@ -431,7 +431,7 @@ where /// ll_max.push(3).unwrap(); /// assert_eq!(ll_max.peek(), Some(&3)); /// - /// let mut ll_min: SortedLinkedList<_, Min, 3> = SortedLinkedList::new_u8(); + /// let mut ll_min: SortedLinkedList<_, Min, 3, u8> = SortedLinkedList::new_u8(); /// /// // The Smallest value will always be first /// ll_min.push(3).unwrap(); @@ -470,7 +470,7 @@ where /// /// ``` /// use heapless::sorted_linked_list::{Max, SortedLinkedList}; - /// let mut ll: SortedLinkedList<_, Max, 3> = SortedLinkedList::new_u8(); + /// let mut ll: SortedLinkedList<_, Max, 3, u8> = SortedLinkedList::new_u8(); /// /// ll.push(1).unwrap(); /// ll.push(2).unwrap(); @@ -493,7 +493,7 @@ where /// /// ``` /// use heapless::sorted_linked_list::{Max, SortedLinkedList}; - /// let mut ll: SortedLinkedList<_, Max, 3> = SortedLinkedList::new_u8(); + /// let mut ll: SortedLinkedList<_, Max, 3, u8> = SortedLinkedList::new_u8(); /// /// assert_eq!(ll.is_full(), false); /// @@ -515,7 +515,7 @@ where /// /// ``` /// use heapless::sorted_linked_list::{Max, SortedLinkedList}; - /// let mut ll: SortedLinkedList<_, Max, 3> = SortedLinkedList::new_u8(); + /// let mut ll: SortedLinkedList<_, Max, 3, u8> = SortedLinkedList::new_u8(); /// /// assert_eq!(ll.is_empty(), true); /// @@ -541,7 +541,7 @@ where /// /// ``` /// use heapless::sorted_linked_list::{Max, SortedLinkedList}; - /// let mut ll: SortedLinkedList<_, Max, 3> = SortedLinkedList::new_u8(); + /// let mut ll: SortedLinkedList<_, Max, 3, u8> = SortedLinkedList::new_u8(); /// /// ll.push(1).unwrap(); /// ll.push(2).unwrap(); @@ -565,7 +565,7 @@ where /// /// ``` /// use heapless::sorted_linked_list::{Max, SortedLinkedList}; - /// let mut ll: SortedLinkedList<_, Max, 3> = SortedLinkedList::new_u8(); + /// let mut ll: SortedLinkedList<_, Max, 3, u8> = SortedLinkedList::new_u8(); /// /// ll.push(1).unwrap(); /// ll.push(2).unwrap(); @@ -695,7 +695,7 @@ where /// /// ``` /// use heapless::sorted_linked_list::{Max, SortedLinkedList}; - /// let mut ll: SortedLinkedList<_, Max, 3> = SortedLinkedList::new_u8(); + /// let mut ll: SortedLinkedList<_, Max, 3, u8> = SortedLinkedList::new_u8(); /// /// ll.push(1).unwrap(); /// ll.push(2).unwrap(); @@ -725,7 +725,7 @@ where /// /// ``` /// use heapless::sorted_linked_list::{Max, SortedLinkedList}; - /// let mut ll: SortedLinkedList<_, Max, 3> = SortedLinkedList::new_u8(); + /// let mut ll: SortedLinkedList<_, Max, 3, u8> = SortedLinkedList::new_u8(); /// /// ll.push(1).unwrap(); /// ll.push(2).unwrap(); @@ -863,7 +863,7 @@ mod tests { #[test] fn test_peek() { - let mut ll: SortedLinkedList = SortedLinkedList::new_u8(); + let mut ll: SortedLinkedList = SortedLinkedList::new_u8(); ll.push(1).unwrap(); assert_eq!(ll.peek().unwrap(), &1); @@ -874,7 +874,7 @@ mod tests { ll.push(3).unwrap(); assert_eq!(ll.peek().unwrap(), &3); - let mut ll: SortedLinkedList = SortedLinkedList::new_u8(); + let mut ll: SortedLinkedList = SortedLinkedList::new_u8(); ll.push(2).unwrap(); assert_eq!(ll.peek().unwrap(), &2); @@ -888,7 +888,7 @@ mod tests { #[test] fn test_full() { - let mut ll: SortedLinkedList = SortedLinkedList::new_u8(); + let mut ll: SortedLinkedList = SortedLinkedList::new_u8(); ll.push(1).unwrap(); ll.push(2).unwrap(); ll.push(3).unwrap(); @@ -898,7 +898,7 @@ mod tests { #[test] fn test_empty() { - let ll: SortedLinkedList = SortedLinkedList::new_u8(); + let ll: SortedLinkedList = SortedLinkedList::new_u8(); assert!(ll.is_empty()); } @@ -913,7 +913,7 @@ mod tests { #[test] fn test_rejected_push() { - let mut ll: SortedLinkedList = SortedLinkedList::new_u8(); + let mut ll: SortedLinkedList = SortedLinkedList::new_u8(); ll.push(1).unwrap(); ll.push(2).unwrap(); ll.push(3).unwrap(); @@ -926,7 +926,7 @@ mod tests { #[test] fn test_updating() { - let mut ll: SortedLinkedList = SortedLinkedList::new_u8(); + let mut ll: SortedLinkedList = SortedLinkedList::new_u8(); ll.push(1).unwrap(); ll.push(2).unwrap(); ll.push(3).unwrap(); @@ -953,7 +953,7 @@ mod tests { #[test] fn test_updating_1() { - let mut ll: SortedLinkedList = SortedLinkedList::new_u8(); + let mut ll: SortedLinkedList = SortedLinkedList::new_u8(); ll.push(1).unwrap(); let v = ll.pop().unwrap(); @@ -963,7 +963,7 @@ mod tests { #[test] fn test_updating_2() { - let mut ll: SortedLinkedList = SortedLinkedList::new_u8(); + let mut ll: SortedLinkedList = SortedLinkedList::new_u8(); ll.push(1).unwrap(); let mut find = ll.find_mut(|v| *v == 1).unwrap(); diff --git a/src/string/mod.rs b/src/string/mod.rs index e58a542d76..668dc95407 100644 --- a/src/string/mod.rs +++ b/src/string/mod.rs @@ -11,7 +11,7 @@ use core::{ str::{self, Utf8Error}, }; -use crate::{len_type::DefaultLenType, CapacityError}; +use crate::CapacityError; use crate::{ len_type::LenType, vec::{OwnedVecStorage, Vec, VecInner, ViewVecStorage}, @@ -147,10 +147,10 @@ pub struct StringInner { } /// A fixed capacity [`String`](https://doc.rust-lang.org/std/string/struct.String.html). -pub type String> = StringInner>; +pub type String = StringInner>; /// A dynamic capacity [`String`](https://doc.rust-lang.org/std/string/struct.String.html). -pub type StringView = StringInner; +pub type StringView = StringInner; impl String { /// Constructs a new, empty `String` with a fixed capacity of `N` bytes. @@ -368,7 +368,7 @@ impl StringInner { /// ```rust /// # use heapless::string::{String, StringView}; /// let s: String<10, _> = String::try_from("hello").unwrap(); - /// let view: &StringView = s.as_view(); + /// let view: &StringView = s.as_view(); /// ``` /// /// It is often preferable to do the same through type coerction, since `String` implements `Unsize`: @@ -376,7 +376,7 @@ impl StringInner { /// ```rust /// # use heapless::string::{String, StringView}; /// let s: String<10, _> = String::try_from("hello").unwrap(); - /// let view: &StringView = &s; + /// let view: &StringView = &s; /// ``` #[inline] pub fn as_view(&self) -> &StringView { @@ -389,7 +389,7 @@ impl StringInner { /// ```rust /// # use heapless::string::{String, StringView}; /// let mut s: String<10> = String::try_from("hello").unwrap(); - /// let view: &mut StringView<_> = s.as_mut_view(); + /// let view: &mut StringView = s.as_mut_view(); /// ``` /// /// It is often preferable to do the same through type coerction, since `String` implements `Unsize`: @@ -397,7 +397,7 @@ impl StringInner { /// ```rust /// # use heapless::string::{String, StringView}; /// let mut s: String<10> = String::try_from("hello").unwrap(); - /// let view: &mut StringView<_> = &mut s; + /// let view: &mut StringView = &mut s; /// ``` #[inline] pub fn as_mut_view(&mut self) -> &mut StringView { @@ -938,8 +938,12 @@ pub fn format( macro_rules! format { // Without semicolon as separator to disambiguate between arms, Rust just // chooses the first so that the format string would land in $max. + ($max:expr; $lenT:path; $($arg:tt)*) => {{ + let res = $crate::_export::format::<$max, $lenT>(core::format_args!($($arg)*)); + res + }}; ($max:expr; $($arg:tt)*) => {{ - let res = $crate::_export::format::<$max, $crate::_export::DefaultLenType<$max>>(core::format_args!($($arg)*)); + let res = $crate::_export::format::<$max, usize>(core::format_args!($($arg)*)); res }}; ($($arg:tt)*) => {{ @@ -1236,29 +1240,4 @@ mod tests { let formatted = format!(2; "123"); assert_eq!(formatted, Err(core::fmt::Error)); } - - #[test] - fn zero_capacity() { - let mut s: String<0> = String::new(); - // Validate capacity - assert_eq!(s.capacity(), 0); - - // Make sure there is no capacity - assert!(s.push('a').is_err()); - - // Validate length - assert_eq!(s.len(), 0); - - // Validate pop - assert_eq!(s.pop(), None); - - // Validate slice - assert_eq!(s.as_str(), ""); - - // Validate empty - assert!(s.is_empty()); - - // Size of string with zero capacity should be 0 bytes because of `ZeroLenType` optimization - assert_eq!(core::mem::size_of_val(&s), 0); - } } diff --git a/src/vec/mod.rs b/src/vec/mod.rs index 13ffeaab73..9eccbfab62 100644 --- a/src/vec/mod.rs +++ b/src/vec/mod.rs @@ -11,7 +11,7 @@ use core::{ slice, }; -use crate::len_type::{check_capacity_fits, DefaultLenType, LenType}; +use crate::len_type::{check_capacity_fits, LenType}; use crate::CapacityError; mod drain; @@ -255,8 +255,7 @@ pub struct VecInner + ?Sized> { /// For uncommmon capacity values, or in generic scenarios, you may have to provide the `LenT` generic yourself. /// /// This should be the smallest unsigned integer type that your capacity fits in, or `usize` if you don't want to consider this. -pub type Vec> = - VecInner>; +pub type Vec = VecInner>; /// A [`Vec`] with dynamic capacity /// @@ -1806,7 +1805,7 @@ mod tests { { let v: Vec = Vec::new(); let v: Box> = Box::new(v); - let mut v: Box> = v; + let mut v: Box> = v; v.push(Droppable::new()).ok().unwrap(); v.push(Droppable::new()).ok().unwrap(); assert_eq!(Droppable::count(), 2); @@ -1819,7 +1818,7 @@ mod tests { { let v: Vec = Vec::new(); let v: Box> = Box::new(v); - let mut v: Box> = v; + let mut v: Box> = v; v.push(Droppable::new()).ok().unwrap(); v.push(Droppable::new()).ok().unwrap(); assert_eq!(Droppable::count(), 2); @@ -2181,34 +2180,6 @@ mod tests { assert!(!v.ends_with(b"a")); } - #[test] - fn zero_capacity() { - let mut v: Vec = Vec::new(); - // Validate capacity - assert_eq!(v.capacity(), 0); - - // Make sure there is no capacity - assert!(v.push(1).is_err()); - - // Validate length - assert_eq!(v.len(), 0); - - // Validate pop - assert_eq!(v.pop(), None); - - // Validate slice - assert_eq!(v.as_slice(), &[]); - - // Validate empty - assert!(v.is_empty()); - - // Validate full - assert!(v.is_full()); - - // Size of vector with zero capacity should be 0 bytes because of `ZeroLenType` optimization - assert_eq!(core::mem::size_of_val(&v), 0); - } - #[test] fn spare_capacity_mut() { let mut v: Vec<_, 4> = Vec::new();