@@ -121,10 +121,21 @@ pub trait Consumer: Observer {
121
121
}
122
122
}
123
123
124
- /// Removes items from the ring buffer and writes them into an uninit slice .
124
+ /// Returns the reference to the eldest item without removing it from the buffer .
125
125
///
126
- /// Returns count of items been removed.
127
- fn pop_slice_uninit ( & mut self , elems : & mut [ MaybeUninit < Self :: Item > ] ) -> usize {
126
+ /// Returns `None` if the ring buffer is empty.
127
+ fn try_peek ( & self ) -> Option < & Self :: Item > {
128
+ if !self . is_empty ( ) {
129
+ Some ( unsafe { self . occupied_slices ( ) . 0 . get_unchecked ( 0 ) . assume_init_ref ( ) } )
130
+ } else {
131
+ None
132
+ }
133
+ }
134
+
135
+ /// Copies items from the ring buffer to an uninit slice without removing them from the ring buffer.
136
+ ///
137
+ /// Returns a number of items being copied.
138
+ fn peek_slice_uninit ( & self , elems : & mut [ MaybeUninit < Self :: Item > ] ) -> usize {
128
139
let ( left, right) = self . occupied_slices ( ) ;
129
140
let count = if elems. len ( ) < left. len ( ) {
130
141
move_uninit_slice ( elems, unsafe { left. get_unchecked ( ..elems. len ( ) ) } ) ;
@@ -141,6 +152,24 @@ pub trait Consumer: Observer {
141
152
right. len ( )
142
153
}
143
154
} ;
155
+ count
156
+ }
157
+
158
+ /// Copies items from the ring buffer to a slice without removing them from the ring buffer.
159
+ ///
160
+ /// Returns a number of items being copied.
161
+ fn peek_slice ( & self , elems : & mut [ Self :: Item ] ) -> usize
162
+ where
163
+ Self :: Item : Copy ,
164
+ {
165
+ self . peek_slice_uninit ( unsafe { slice_as_uninit_mut ( elems) } )
166
+ }
167
+
168
+ /// Removes items from the ring buffer and writes them into an uninit slice.
169
+ ///
170
+ /// Returns count of items been removed.
171
+ fn pop_slice_uninit ( & mut self , elems : & mut [ MaybeUninit < Self :: Item > ] ) -> usize {
172
+ let count = self . peek_slice_uninit ( elems) ;
144
173
unsafe { self . advance_read_index ( count) } ;
145
174
count
146
175
}
0 commit comments