Skip to content

Commit

Permalink
use cfg-if to simplify some things; add some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
vadixidav committed Jan 16, 2025
1 parent 00be0d8 commit b777b0f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ readme = "README.md"
[features]
default = ["atomic_append"]
atomic_append = []

[dependencies]
cfg-if = "1.0.0"
59 changes: 35 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

extern crate alloc;

use cfg_if::cfg_if;
#[cfg(feature = "atomic_append")]
use core::sync::atomic::{AtomicUsize, Ordering};
use core::{
fmt::Debug,
mem::{self, ManuallyDrop},
Expand All @@ -10,9 +13,6 @@ use core::{
slice::SliceIndex,
};

#[cfg(feature = "atomic_append")]
use core::sync::atomic::{AtomicUsize, Ordering};

struct HeaderVecHeader<H> {
head: H,
capacity: usize,
Expand Down Expand Up @@ -91,12 +91,13 @@ impl<H, T> HeaderVec<H, T> {
#[cfg(feature = "atomic_append")]
#[inline(always)]
pub fn len_exact(&mut self) -> usize {
*self.header_mut().len.get_mut()
}
#[cfg(not(feature = "atomic_append"))]
#[inline(always)]
pub fn len_exact(&mut self) -> usize {
self.header_mut().len
cfg_if! {
if #[cfg(feature = "atomic_append")] {
*self.header_mut().len.get_mut()
} else {
self.header_mut().len
}
}
}

/// This gives the length of the `HeaderVec`. This is the non synchronized variant may
Expand All @@ -105,28 +106,29 @@ impl<H, T> HeaderVec<H, T> {
#[cfg(feature = "atomic_append")]
#[inline(always)]
pub fn len(&self) -> usize {
self.len_atomic_relaxed()
}
#[cfg(not(feature = "atomic_append"))]
#[inline(always)]
pub fn len(&self) -> usize {
self.header().len
cfg_if! {
if #[cfg(feature = "atomic_append")] {
self.len_atomic_relaxed()
} else {
self.header().len
}
}
}

/// This gives the length of the `HeaderVec`. With `atomic_append` enabled this gives a
/// exact result *after* another thread atomically appended to this `HeaderVec`. It still
/// requires synchronization because the length may become invalidated when another thread
/// atomically appends data to this `HeaderVec` while we still work with the result of
/// this method.
#[cfg(not(feature = "atomic_append"))]
#[inline(always)]
pub fn len_strict(&self) -> usize {
self.header().len
}
#[cfg(feature = "atomic_append")]
#[inline(always)]
pub fn len_strict(&self) -> usize {
self.len_atomic_acquire()
cfg_if! {
if #[cfg(feature = "atomic_append")] {
self.len_atomic_acquire()
} else {
self.header().len
}
}
}

/// Check whenever a `HeaderVec` is empty. This uses a `&mut self` reference and is
Expand Down Expand Up @@ -460,22 +462,31 @@ impl<H, T: Clone> HeaderVec<H, T> {
/// The atomic append API is only enabled when the `atomic_append` feature flag is set (which
/// is the default).
impl<H, T> HeaderVec<H, T> {
/// Get the length of the vector with `Ordering::Acquire`. This ensures that the length is
/// properly synchronized after it got atomically updated.
#[inline(always)]
fn len_atomic(&self, order: Ordering) -> usize {
self.header().len.load(order)
}

/// Get the length of the vector with `Ordering::Acquire`. This ensures that the length is
/// properly synchronized after it got atomically updated.
#[inline(always)]
fn len_atomic_acquire(&self) -> usize {
self.header().len.load(Ordering::Acquire)
self.len_atomic(Ordering::Acquire)
}

/// Get the length of the vector with `Ordering::Relaxed`. This is useful for when you don't
/// need exact synchronization semantic.
#[inline(always)]
fn len_atomic_relaxed(&self) -> usize {
self.header().len.load(Ordering::Relaxed)
self.len_atomic(Ordering::Relaxed)
}

/// Add `n` to the length of the vector atomically with `Ordering::Release`.
///
/// Returns the previous value of the length before the atomic add.
///
/// # Safety
///
/// Before incrementing the length of the vector, you must ensure that new elements are
Expand Down

0 comments on commit b777b0f

Please sign in to comment.