From 7020f4afff4b393acb579604b0e05504338fe72f Mon Sep 17 00:00:00 2001 From: cooronx <2197083441@qq.com> Date: Fri, 9 Oct 2026 22:55:14 +0800 Subject: [PATCH 1/3] feat: hide allocator functions when unusable --- src/lib.rs | 51 +++++++++++++++++++++++++++++++++------------------ src/macros.rs | 21 +++++++++++++++++++++ 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5deaf7e0..b94ed0b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,7 @@ mod conversions; mod error; mod iterators; mod locatedlength; +#[macro_use] mod macros; #[cfg(feature = "malloc_size_of")] mod mallocsizeof; @@ -1349,32 +1350,46 @@ impl SmallVec { self.length.add(length); } } +} - pub const fn new_in(allocator: Heap) -> SmallVec { - Self { - length: LocatedLength::new(0, false), - raw: RawSmallVec::new(), - allocator +impl SmallVec { + public! { + #[cfg(feature = "allocator-api")] + const fn new_in(allocator: Heap) -> SmallVec { + Self { + length: LocatedLength::new(0, false), + raw: RawSmallVec::new(), + allocator + } } } - pub fn try_with_capacity_in(capacity: usize, allocator: Heap) -> Result { - let mut this = Self::new_in(allocator); - if capacity > Self::inline_size() && !Self::IS_ZST { - // SAFETY: we checked all the preconditions - unsafe { - this.raw - .try_grow_raw(LocatedLength::new(0, false), capacity, &this.allocator) - }?; + public! { + #[cfg(feature = "allocator-api")] + fn try_with_capacity_in( + capacity: usize, + allocator: Heap + ) -> Result { + let mut this = Self::new_in(allocator); + if capacity > Self::inline_size() && !Self::IS_ZST { + // SAFETY: we checked all the preconditions + unsafe { + this.raw + .try_grow_raw(LocatedLength::new(0, false), capacity, &this.allocator) + }?; - // SAFETY: the allocation succeeded, so self.raw.heap is now active - this.length.set_location::(); + // SAFETY: the allocation succeeded, so self.raw.heap is now active + this.length.set_location::(); + } + Ok(this) } - Ok(this) } - pub fn with_capacity_in(capacity: usize, allocator: Heap) -> Self { - Self::try_with_capacity_in(capacity, allocator).unwrap_or_else(SmallVecError::handle) + public! { + #[cfg(feature = "allocator-api")] + fn with_capacity_in(capacity: usize, allocator: Heap) -> Self { + Self::try_with_capacity_in(capacity, allocator).unwrap_or_else(SmallVecError::handle) + } } } diff --git a/src/macros.rs b/src/macros.rs index b7da06c6..1e9cee0c 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -19,3 +19,24 @@ macro_rules! smallvec_inline { $crate::SmallVec::from_buf([$($($x),+)?]) }); } + +macro_rules! public { + (#[cfg($($cond:tt)*)] $(#[$attr:meta])* const fn $($rest:tt)*) => { + #[cfg($($cond)*)] + $(#[$attr])* + pub const fn $($rest)* + + #[cfg(not($($cond)*))] + $(#[$attr])* + const fn $($rest)* + }; + (#[cfg($($cond:tt)*)] $(#[$attr:meta])* fn $($rest:tt)*) => { + #[cfg($($cond)*)] + $(#[$attr])* + pub fn $($rest)* + + #[cfg(not($($cond)*))] + $(#[$attr])* + fn $($rest)* + }; +} From c2c7c39cf6fcd0e6e2882554c1292f9bea79a8df Mon Sep 17 00:00:00 2001 From: fansijing <2197083441@qq.com> Date: Wed, 21 Oct 2026 09:17:20 +0800 Subject: [PATCH 2/3] feat: use syntax macro for allocator function visibility --- src/lib.rs | 52 +++++++++++++++++++++------------------------------ src/macros.rs | 21 --------------------- 2 files changed, 21 insertions(+), 52 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b94ed0b2..33042b50 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,7 +18,6 @@ mod conversions; mod error; mod iterators; mod locatedlength; -#[macro_use] mod macros; #[cfg(feature = "malloc_size_of")] mod mallocsizeof; @@ -1353,43 +1352,34 @@ impl SmallVec { } impl SmallVec { - public! { - #[cfg(feature = "allocator-api")] - const fn new_in(allocator: Heap) -> SmallVec { - Self { - length: LocatedLength::new(0, false), - raw: RawSmallVec::new(), - allocator - } + #[cfg_attr(feature = "allocator-api", syntax(pub))] + const fn new_in(allocator: Heap) -> SmallVec { + Self { + length: LocatedLength::new(0, false), + raw: RawSmallVec::new(), + allocator } } - public! { - #[cfg(feature = "allocator-api")] - fn try_with_capacity_in( - capacity: usize, - allocator: Heap - ) -> Result { - let mut this = Self::new_in(allocator); - if capacity > Self::inline_size() && !Self::IS_ZST { - // SAFETY: we checked all the preconditions - unsafe { - this.raw - .try_grow_raw(LocatedLength::new(0, false), capacity, &this.allocator) - }?; + #[cfg_attr(feature = "allocator-api", syntax(pub))] + fn try_with_capacity_in(capacity: usize, allocator: Heap) -> Result { + let mut this = Self::new_in(allocator); + if capacity > Self::inline_size() && !Self::IS_ZST { + // SAFETY: we checked all the preconditions + unsafe { + this.raw + .try_grow_raw(LocatedLength::new(0, false), capacity, &this.allocator) + }?; - // SAFETY: the allocation succeeded, so self.raw.heap is now active - this.length.set_location::(); - } - Ok(this) + // SAFETY: the allocation succeeded, so self.raw.heap is now active + this.length.set_location::(); } + Ok(this) } - public! { - #[cfg(feature = "allocator-api")] - fn with_capacity_in(capacity: usize, allocator: Heap) -> Self { - Self::try_with_capacity_in(capacity, allocator).unwrap_or_else(SmallVecError::handle) - } + #[cfg_attr(feature = "allocator-api", syntax(pub))] + fn with_capacity_in(capacity: usize, allocator: Heap) -> Self { + Self::try_with_capacity_in(capacity, allocator).unwrap_or_else(SmallVecError::handle) } } diff --git a/src/macros.rs b/src/macros.rs index 1e9cee0c..b7da06c6 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -19,24 +19,3 @@ macro_rules! smallvec_inline { $crate::SmallVec::from_buf([$($($x),+)?]) }); } - -macro_rules! public { - (#[cfg($($cond:tt)*)] $(#[$attr:meta])* const fn $($rest:tt)*) => { - #[cfg($($cond)*)] - $(#[$attr])* - pub const fn $($rest)* - - #[cfg(not($($cond)*))] - $(#[$attr])* - const fn $($rest)* - }; - (#[cfg($($cond:tt)*)] $(#[$attr:meta])* fn $($rest:tt)*) => { - #[cfg($($cond)*)] - $(#[$attr])* - pub fn $($rest)* - - #[cfg(not($($cond)*))] - $(#[$attr])* - fn $($rest)* - }; -} From 674619cc41500a1d14c38d23d9240c6952598a5d Mon Sep 17 00:00:00 2001 From: fansijing <2197083441@qq.com> Date: Wed, 21 Oct 2026 09:22:51 +0800 Subject: [PATCH 3/3] fix: put allocator constructors into the origin impl block --- src/lib.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 33042b50..7f094fb2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1349,9 +1349,7 @@ impl SmallVec { self.length.add(length); } } -} -impl SmallVec { #[cfg_attr(feature = "allocator-api", syntax(pub))] const fn new_in(allocator: Heap) -> SmallVec { Self {