Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ std = []
specialization = []
may_dangle = []
serde = ["dep:serde_core"]
internals = []

[dependencies]
bytes = { version = "1", optional = true, default-features = false }
Expand Down
18 changes: 6 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub extern crate alloc;
#[cfg(any(test, feature = "std"))]
extern crate std;

mod rawsmallvec;
#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -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 {
Expand All @@ -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<T>` 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<T, const N: usize> {
inline: ManuallyDrop<MaybeUninit<[T; N]>>,
heap: (NonNull<T>, usize),
}

#[inline]
fn infallible<T>(result: Result<T, CollectionAllocErr>) -> T {
match result {
Expand Down
13 changes: 13 additions & 0 deletions src/rawsmallvec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use core::mem::{ManuallyDrop, MaybeUninit};
use core::ptr::NonNull;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure splitting to its own file is really beneficial but...


/// Either a stack array with `length <= N` or a heap array
/// whose pointer and capacity are stored here.
///
/// We store a `NonNull<T>` 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<T, const N: usize> {
pub inline: ManuallyDrop<MaybeUninit<[T; N]>>,
pub heap: (NonNull<T>, usize),
}
Loading