From 1861ee4930dffc0602420605f73be9e1832ab586 Mon Sep 17 00:00:00 2001 From: Zachary S Date: Fri, 21 Aug 2026 13:56:37 -0500 Subject: [PATCH 1/2] Add internals cargo feature. Currently, only the `RawSmallVec` type (and its fields) are exposed under this feature. --- Cargo.toml | 1 + src/lib.rs | 28 ++++++++++++++++++---------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a2a1b256..1887244d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ std = [] specialization = [] may_dangle = [] serde = ["dep:serde_core"] +internals = [] [dependencies] bytes = { version = "1", optional = true, default-features = false } diff --git a/src/lib.rs b/src/lib.rs index 98b6f2f2..5f5948df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -114,18 +114,26 @@ impl core::fmt::Display for CollectionAllocErr { impl core::error::Error for CollectionAllocErr {} -/// Either a stack array with `length <= N` or a heap array -/// whose pointer and capacity are stored here. -/// -/// We store a `NonNull` instead of a `*mut T`, so that -/// niche-optimization can be performed and the type is covariant -/// with respect to `T`. -#[repr(C)] -pub union RawSmallVec { - inline: ManuallyDrop>, - heap: (NonNull, usize), +mod raw { + use super::{ManuallyDrop, MaybeUninit, NonNull}; + + /// Either a stack array with `length <= N` or a heap array + /// whose pointer and capacity are stored here. + /// + /// We store a `NonNull` instead of a `*mut T` so that type is covariant + /// with respect to `T`, and since the heap pointer is never null. + #[repr(C)] + pub union RawSmallVec { + pub inline: ManuallyDrop>, + pub heap: (NonNull, usize), + } } +#[cfg(feature = "internals")] +pub use raw::RawSmallVec; +#[cfg(not(feature = "internals"))] +use raw::RawSmallVec; + #[inline] fn infallible(result: Result) -> T { match result { From 6d5dfcf9e55282accfb266b71573490518c12c65 Mon Sep 17 00:00:00 2001 From: Zachary S Date: Fri, 21 Aug 2026 15:19:48 -0500 Subject: [PATCH 2/2] Rename `raw` to `rawsmallvec` and move it to its own file. --- src/lib.rs | 26 ++++++-------------------- src/rawsmallvec.rs | 13 +++++++++++++ 2 files changed, 19 insertions(+), 20 deletions(-) create mode 100644 src/rawsmallvec.rs diff --git a/src/lib.rs b/src/lib.rs index 5f5948df..5dba36a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,6 +62,7 @@ pub extern crate alloc; #[cfg(any(test, feature = "std"))] extern crate std; +mod rawsmallvec; #[cfg(test)] mod tests; @@ -95,6 +96,11 @@ use serde_core::{ #[cfg(feature = "std")] use std::io; +#[cfg(feature = "internals")] +pub use rawsmallvec::RawSmallVec; +#[cfg(not(feature = "internals"))] +use rawsmallvec::RawSmallVec; + /// Error type for APIs with fallible heap allocation #[derive(Debug)] pub enum CollectionAllocErr { @@ -114,26 +120,6 @@ impl core::fmt::Display for CollectionAllocErr { impl core::error::Error for CollectionAllocErr {} -mod raw { - use super::{ManuallyDrop, MaybeUninit, NonNull}; - - /// Either a stack array with `length <= N` or a heap array - /// whose pointer and capacity are stored here. - /// - /// We store a `NonNull` instead of a `*mut T` so that type is covariant - /// with respect to `T`, and since the heap pointer is never null. - #[repr(C)] - pub union RawSmallVec { - pub inline: ManuallyDrop>, - pub heap: (NonNull, usize), - } -} - -#[cfg(feature = "internals")] -pub use raw::RawSmallVec; -#[cfg(not(feature = "internals"))] -use raw::RawSmallVec; - #[inline] fn infallible(result: Result) -> T { match result { diff --git a/src/rawsmallvec.rs b/src/rawsmallvec.rs new file mode 100644 index 00000000..dadbf958 --- /dev/null +++ b/src/rawsmallvec.rs @@ -0,0 +1,13 @@ +use core::mem::{ManuallyDrop, MaybeUninit}; +use core::ptr::NonNull; + +/// Either a stack array with `length <= N` or a heap array +/// whose pointer and capacity are stored here. +/// +/// We store a `NonNull` instead of a `*mut T` so that type is covariant +/// with respect to `T`, and since the heap pointer is never null. +#[repr(C)] +pub union RawSmallVec { + pub inline: ManuallyDrop>, + pub heap: (NonNull, usize), +}