@@ -84,6 +84,32 @@ macro_rules! impl_ {
84
84
return head != tail;
85
85
}
86
86
87
+ /// Returns the item in the front of the queue without dequeuing, or `None` if the queue is empty.
88
+ ///
89
+ /// # Examples
90
+ /// ```
91
+ /// use heapless::spsc::Queue;
92
+ /// use heapless::consts::*;
93
+ ///
94
+ /// let mut queue: Queue<u8, U235, _> = Queue::u8();
95
+ /// let (mut producer, mut consumer) = queue.split();
96
+ /// assert_eq!(None, consumer.peek());
97
+ /// producer.enqueue(1);
98
+ /// assert_eq!(Some(&1), consumer.peek());
99
+ /// assert_eq!(Some(1), consumer.dequeue());
100
+ /// assert_eq!(None, consumer.peek());
101
+ /// ```
102
+ pub fn peek( & self ) -> Option <& T > {
103
+ let head = unsafe { self . rb. as_ref( ) . 0 . head. load_relaxed( ) } ;
104
+ let tail = unsafe { self . rb. as_ref( ) . 0 . tail. load_acquire( ) } ;
105
+
106
+ if head != tail {
107
+ Some ( unsafe { self . _peek( head) } )
108
+ } else {
109
+ None
110
+ }
111
+ }
112
+
87
113
/// Returns the item in the front of the queue, or `None` if the queue is empty
88
114
pub fn dequeue( & mut self ) -> Option <T > {
89
115
let head = unsafe { self . rb. as_ref( ) . 0 . head. load_relaxed( ) } ;
@@ -107,6 +133,15 @@ macro_rules! impl_ {
107
133
self . _dequeue( head) // ▲
108
134
}
109
135
136
+ unsafe fn _peek( & self , head: $uxx) -> & T {
137
+ let rb = self . rb. as_ref( ) ;
138
+
139
+ let cap = rb. capacity( ) ;
140
+
141
+ let item = ( rb. 0 . buffer. as_ptr( ) as * const T ) . add( usize :: from( head % cap) ) ;
142
+ & * item
143
+ }
144
+
110
145
unsafe fn _dequeue( & mut self , head: $uxx) -> T {
111
146
let rb = self . rb. as_ref( ) ;
112
147
0 commit comments