Skip to content

Commit b777b0f

Browse files
committed
use cfg-if to simplify some things; add some docs
1 parent 00be0d8 commit b777b0f

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ readme = "README.md"
1414
[features]
1515
default = ["atomic_append"]
1616
atomic_append = []
17+
18+
[dependencies]
19+
cfg-if = "1.0.0"

src/lib.rs

+35-24
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
extern crate alloc;
44

5+
use cfg_if::cfg_if;
6+
#[cfg(feature = "atomic_append")]
7+
use core::sync::atomic::{AtomicUsize, Ordering};
58
use core::{
69
fmt::Debug,
710
mem::{self, ManuallyDrop},
@@ -10,9 +13,6 @@ use core::{
1013
slice::SliceIndex,
1114
};
1215

13-
#[cfg(feature = "atomic_append")]
14-
use core::sync::atomic::{AtomicUsize, Ordering};
15-
1616
struct HeaderVecHeader<H> {
1717
head: H,
1818
capacity: usize,
@@ -91,12 +91,13 @@ impl<H, T> HeaderVec<H, T> {
9191
#[cfg(feature = "atomic_append")]
9292
#[inline(always)]
9393
pub fn len_exact(&mut self) -> usize {
94-
*self.header_mut().len.get_mut()
95-
}
96-
#[cfg(not(feature = "atomic_append"))]
97-
#[inline(always)]
98-
pub fn len_exact(&mut self) -> usize {
99-
self.header_mut().len
94+
cfg_if! {
95+
if #[cfg(feature = "atomic_append")] {
96+
*self.header_mut().len.get_mut()
97+
} else {
98+
self.header_mut().len
99+
}
100+
}
100101
}
101102

102103
/// This gives the length of the `HeaderVec`. This is the non synchronized variant may
@@ -105,28 +106,29 @@ impl<H, T> HeaderVec<H, T> {
105106
#[cfg(feature = "atomic_append")]
106107
#[inline(always)]
107108
pub fn len(&self) -> usize {
108-
self.len_atomic_relaxed()
109-
}
110-
#[cfg(not(feature = "atomic_append"))]
111-
#[inline(always)]
112-
pub fn len(&self) -> usize {
113-
self.header().len
109+
cfg_if! {
110+
if #[cfg(feature = "atomic_append")] {
111+
self.len_atomic_relaxed()
112+
} else {
113+
self.header().len
114+
}
115+
}
114116
}
115117

116118
/// This gives the length of the `HeaderVec`. With `atomic_append` enabled this gives a
117119
/// exact result *after* another thread atomically appended to this `HeaderVec`. It still
118120
/// requires synchronization because the length may become invalidated when another thread
119121
/// atomically appends data to this `HeaderVec` while we still work with the result of
120122
/// this method.
121-
#[cfg(not(feature = "atomic_append"))]
122-
#[inline(always)]
123-
pub fn len_strict(&self) -> usize {
124-
self.header().len
125-
}
126-
#[cfg(feature = "atomic_append")]
127123
#[inline(always)]
128124
pub fn len_strict(&self) -> usize {
129-
self.len_atomic_acquire()
125+
cfg_if! {
126+
if #[cfg(feature = "atomic_append")] {
127+
self.len_atomic_acquire()
128+
} else {
129+
self.header().len
130+
}
131+
}
130132
}
131133

132134
/// Check whenever a `HeaderVec` is empty. This uses a `&mut self` reference and is
@@ -460,22 +462,31 @@ impl<H, T: Clone> HeaderVec<H, T> {
460462
/// The atomic append API is only enabled when the `atomic_append` feature flag is set (which
461463
/// is the default).
462464
impl<H, T> HeaderVec<H, T> {
465+
/// Get the length of the vector with `Ordering::Acquire`. This ensures that the length is
466+
/// properly synchronized after it got atomically updated.
467+
#[inline(always)]
468+
fn len_atomic(&self, order: Ordering) -> usize {
469+
self.header().len.load(order)
470+
}
471+
463472
/// Get the length of the vector with `Ordering::Acquire`. This ensures that the length is
464473
/// properly synchronized after it got atomically updated.
465474
#[inline(always)]
466475
fn len_atomic_acquire(&self) -> usize {
467-
self.header().len.load(Ordering::Acquire)
476+
self.len_atomic(Ordering::Acquire)
468477
}
469478

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

477486
/// Add `n` to the length of the vector atomically with `Ordering::Release`.
478487
///
488+
/// Returns the previous value of the length before the atomic add.
489+
///
479490
/// # Safety
480491
///
481492
/// Before incrementing the length of the vector, you must ensure that new elements are

0 commit comments

Comments
 (0)