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..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,18 +120,6 @@ 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), -} - #[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), +}