2
2
3
3
extern crate alloc;
4
4
5
+ use cfg_if:: cfg_if;
6
+ #[ cfg( feature = "atomic_append" ) ]
7
+ use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
5
8
use core:: {
6
9
fmt:: Debug ,
7
10
mem:: { self , ManuallyDrop } ,
@@ -10,9 +13,6 @@ use core::{
10
13
slice:: SliceIndex ,
11
14
} ;
12
15
13
- #[ cfg( feature = "atomic_append" ) ]
14
- use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
15
-
16
16
struct HeaderVecHeader < H > {
17
17
head : H ,
18
18
capacity : usize ,
@@ -91,12 +91,13 @@ impl<H, T> HeaderVec<H, T> {
91
91
#[ cfg( feature = "atomic_append" ) ]
92
92
#[ inline( always) ]
93
93
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
+ }
100
101
}
101
102
102
103
/// This gives the length of the `HeaderVec`. This is the non synchronized variant may
@@ -105,28 +106,29 @@ impl<H, T> HeaderVec<H, T> {
105
106
#[ cfg( feature = "atomic_append" ) ]
106
107
#[ inline( always) ]
107
108
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
+ }
114
116
}
115
117
116
118
/// This gives the length of the `HeaderVec`. With `atomic_append` enabled this gives a
117
119
/// exact result *after* another thread atomically appended to this `HeaderVec`. It still
118
120
/// requires synchronization because the length may become invalidated when another thread
119
121
/// atomically appends data to this `HeaderVec` while we still work with the result of
120
122
/// 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" ) ]
127
123
#[ inline( always) ]
128
124
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
+ }
130
132
}
131
133
132
134
/// 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> {
460
462
/// The atomic append API is only enabled when the `atomic_append` feature flag is set (which
461
463
/// is the default).
462
464
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
+
463
472
/// Get the length of the vector with `Ordering::Acquire`. This ensures that the length is
464
473
/// properly synchronized after it got atomically updated.
465
474
#[ inline( always) ]
466
475
fn len_atomic_acquire ( & self ) -> usize {
467
- self . header ( ) . len . load ( Ordering :: Acquire )
476
+ self . len_atomic ( Ordering :: Acquire )
468
477
}
469
478
470
479
/// Get the length of the vector with `Ordering::Relaxed`. This is useful for when you don't
471
480
/// need exact synchronization semantic.
472
481
#[ inline( always) ]
473
482
fn len_atomic_relaxed ( & self ) -> usize {
474
- self . header ( ) . len . load ( Ordering :: Relaxed )
483
+ self . len_atomic ( Ordering :: Relaxed )
475
484
}
476
485
477
486
/// Add `n` to the length of the vector atomically with `Ordering::Release`.
478
487
///
488
+ /// Returns the previous value of the length before the atomic add.
489
+ ///
479
490
/// # Safety
480
491
///
481
492
/// Before incrementing the length of the vector, you must ensure that new elements are
0 commit comments