@@ -88,24 +88,24 @@ impl<T> Buffer<T> {
88
88
}
89
89
90
90
/// Returns the capacity of the buffer.
91
- pub fn cap ( & self ) -> usize {
91
+ pub fn len ( & self ) -> usize {
92
92
self . inner . len ( )
93
93
}
94
94
95
95
/// Returns a pointer to the slot at the specified `index`.
96
- pub unsafe fn at ( & self , index : usize ) -> * mut Slot < T > {
96
+ pub fn get ( & self , index : usize ) -> * const Slot < T > {
97
97
// `array.size()` is always a power of two.
98
- self . inner . get_unchecked ( index & ( self . cap ( ) - 1 ) ) as * const _ as * mut _
98
+ unsafe { self . inner . get_unchecked ( index & ( self . len ( ) - 1 ) ) . as_ptr ( ) }
99
99
}
100
100
101
101
/// Reads a value from the specified `index`.
102
102
///
103
103
/// Returns `Some(v)` if `v` is at `index`; or `None` if there's no valid value for `index`.
104
104
pub unsafe fn read ( & self , index : usize ) -> Option < mem:: ManuallyDrop < T > > {
105
- let slot = self . at ( index) ;
105
+ let slot = & * self . get ( index) ;
106
106
107
107
// Reads the index with `Acquire`.
108
- let i = ( * slot) . index . load ( Ordering :: Acquire ) ;
108
+ let i = slot. index . load ( Ordering :: Acquire ) ;
109
109
110
110
// If the index in the buffer mismatches with the queried index, there's no valid value.
111
111
if index != i {
@@ -120,41 +120,32 @@ impl<T> Buffer<T> {
120
120
///
121
121
/// Returns the value at `index` regardless or whether it's valid or not.
122
122
pub unsafe fn read_unchecked ( & self , index : usize ) -> mem:: ManuallyDrop < T > {
123
- let slot = self . at ( index) ;
123
+ let slot = & * self . get ( index) ;
124
+ slot. data . get ( ) . read_volatile ( )
125
+ }
124
126
125
- // Returns the value.
126
- ( * slot) . data . get ( ) . read_volatile ( )
127
+ /// Reads the index from the specified slot.
128
+ pub unsafe fn read_index ( & self , index : usize , ord : Ordering ) -> usize {
129
+ let slot = & * self . get ( index) ;
130
+ slot. index . load ( ord)
127
131
}
128
132
129
133
/// Writes `value` into the specified `index`.
130
134
pub unsafe fn write ( & self , index : usize , value : T ) {
131
- let slot = self . at ( index) ;
135
+ let slot = & * self . get ( index) ;
132
136
133
137
// Writes the value.
134
- ( * slot)
135
- . data
138
+ slot. data
136
139
. get ( )
137
140
. write_volatile ( mem:: ManuallyDrop :: new ( value) ) ;
138
141
139
142
// Writes the index with `Release`.
140
- ( * slot) . index . store ( index, Ordering :: Release ) ;
141
- }
142
-
143
- /// Reads the index from the specified slot.
144
- pub unsafe fn read_index ( & self , offset : usize , ord : Ordering ) -> usize {
145
- let slot = self . at ( offset) ;
146
- ( * slot) . index . load ( ord)
143
+ slot. index . store ( index, Ordering :: Release ) ;
147
144
}
148
145
149
146
/// Writes the specified `index` in the slot.
150
147
pub unsafe fn write_index ( & self , index : usize , ord : Ordering ) {
151
- let slot = self . at ( index) ;
152
- ( * slot) . index . store ( index, ord) ;
153
- }
154
-
155
- /// Reads the value from the specified slot.
156
- pub unsafe fn read_value ( & self , offset : usize ) -> mem:: ManuallyDrop < T > {
157
- let slot = self . at ( offset) ;
158
- ( * slot) . data . get ( ) . read_volatile ( )
148
+ let slot = & * self . get ( index) ;
149
+ slot. index . store ( index, ord) ;
159
150
}
160
151
}
0 commit comments