Skip to content
Open
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
51 changes: 33 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub use iterators::{
splice::Splice
};
mod locatedlength;
#[macro_use]
mod macros;
#[cfg(feature = "malloc_size_of")]
mod mallocsizeof;
Expand Down Expand Up @@ -1342,32 +1343,46 @@ impl<Item, const INLINE: usize, Heap: Allocator> SmallVec<Item, INLINE, Heap> {
self.length.add(length);
}
}
}

pub const fn new_in(allocator: Heap) -> SmallVec<Item, INLINE, Heap> {
Self {
length: LocatedLength::new(0, false),
raw: RawSmallVec::new(),
allocator
impl<Item, const INLINE: usize, Heap: Allocator> SmallVec<Item, INLINE, Heap> {
public! {
#[cfg(feature = "allocator-api")]
const fn new_in(allocator: Heap) -> SmallVec<Item, INLINE, Heap> {
Self {
length: LocatedLength::new(0, false),
raw: RawSmallVec::new(),
allocator
}
}
}

pub fn try_with_capacity_in(capacity: usize, allocator: Heap) -> Result<Self, SmallVecError> {
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<Self, SmallVecError> {
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::<true>();
// SAFETY: the allocation succeeded, so self.raw.heap is now active
this.length.set_location::<true>();
}
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)
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)*
};
}
Loading