@@ -9,9 +9,9 @@ use crate::collections::queue;
9
9
10
10
11
11
#[ flux_rs:: refined_by( ring_len: int, head: int, tail: int) ]
12
- #[ flux_rs:: invariant( ring_len > 0 ) ]
12
+ #[ flux_rs:: invariant( ring_len > 1 ) ]
13
13
pub struct RingBuffer < ' a , T : ' a > {
14
- #[ field( { & mut [ T ] [ ring_len] | ring_len > 0 } ) ]
14
+ #[ field( { & mut [ T ] [ ring_len] | ring_len > 1 } ) ]
15
15
ring : & ' a mut [ T ] ,
16
16
#[ field( { usize [ head] | head < ring_len} ) ]
17
17
head : usize ,
@@ -29,7 +29,7 @@ flux_rs::defs! {
29
29
30
30
31
31
impl < ' a , T : Copy > RingBuffer < ' a , T > {
32
- #[ flux_rs:: sig( fn ( { & mut [ T ] [ @ring_len] | ring_len > 0 } ) -> RingBuffer <T >[ ring_len, 0 , 0 ] ) ]
32
+ #[ flux_rs:: sig( fn ( { & mut [ T ] [ @ring_len] | ring_len > 1 } ) -> RingBuffer <T >[ ring_len, 0 , 0 ] ) ]
33
33
pub fn new ( ring : & ' a mut [ T ] ) -> RingBuffer < ' a , T > {
34
34
RingBuffer {
35
35
head : 0 ,
@@ -108,26 +108,18 @@ impl<T: Copy> queue::Queue<T> for RingBuffer<'_, T> {
108
108
// that hasn't been read
109
109
rg. head != rg. tail
110
110
&&
111
- (
112
- // either we're full and don't update
113
- // hd == rb_next(tl, ring_len) -> rg.tail == tl && rg.head == hd
114
- ( hd != rb_next( tl, ring_len) || rg. tail == tl && rg. head == hd)
115
- ||
116
- // or we updated which means the new value of tail is (tl + 1) % r
117
- // hd != rb_next(tl, ring_len) -> rg.tail = rb_next(tl, ring_len)
118
- ( hd == rb_next( tl, ring_len) || rg. tail == rb_next( tl, ring_len) )
119
- )
111
+ // either we're full and don't update
112
+ // hd == rb_next(tl, ring_len) -> rg.tail == tl && rg.head == hd
113
+ ( hd == rb_next( tl, ring_len) => rg. tail == tl && rg. head == hd)
114
+ &&
115
+ // or we updated which means the new value of tail is (tl + 1) % r
116
+ // hd != rb_next(tl, ring_len) -> rg.tail = rb_next(tl, ring_len)
117
+ ( hd != rb_next( tl, ring_len) => rg. tail == rb_next( tl, ring_len) )
120
118
}
121
119
) ]
122
120
fn enqueue ( & mut self , val : T ) -> bool {
123
121
if self . is_full ( ) {
124
122
// Incrementing tail will overwrite head
125
- //
126
- // at this point we know that
127
- // self.head == ((self.tail + 1) % self.ring_len())
128
- // so of course tail != head but for some reason
129
- // this is not provable
130
- flux_support:: assume ( self . tail != self . head ) ;
131
123
false
132
124
} else {
133
125
self . ring [ self . tail ] = val;
@@ -148,15 +140,11 @@ impl<T: Copy> queue::Queue<T> for RingBuffer<'_, T> {
148
140
&&
149
141
// or we have space so we just enqueue
150
142
( !full( old) => ( rg. tail == next_tl( old) && rg. head == old. head) )
151
-
152
143
}
153
144
) ]
154
145
fn push ( & mut self , val : T ) -> Option < T > {
155
146
let result = if self . is_full ( ) {
156
147
let val = self . ring [ self . head ] ;
157
- // at this point we should be
158
- // able to deduce that head != tail
159
- flux_support:: assume ( self . tail != self . head ) ;
160
148
self . head = ( self . head + 1 ) % self . ring_len ( ) ;
161
149
Some ( val)
162
150
} else {
@@ -172,11 +160,9 @@ impl<T: Copy> queue::Queue<T> for RingBuffer<'_, T> {
172
160
fn ( self : & strg RingBuffer <T >[ @ring_len, @hd, @tl] ) -> Option <T >
173
161
ensures self : RingBuffer <T >{
174
162
rg:
175
- // hd == tl -> (rg.head == hd && rg.tail == tl && rg.head == rg.tail)
176
- ( hd != tl || ( rg. head == hd && rg. tail == tl && rg. head == rg. tail) )
177
- ||
178
- // hd != tl -> (rg.head == rb_next(tl, ring_len))
179
- ( hd == tl || rg. head == rb_next( hd, ring_len) )
163
+ ( hd == tl => ( rg. head == hd && rg. tail == tl && rg. head == rg. tail) )
164
+ &&
165
+ ( hd != tl => rg. head == rb_next( hd, ring_len) )
180
166
}
181
167
) ]
182
168
fn dequeue ( & mut self ) -> Option < T > {
@@ -240,7 +226,6 @@ impl<T: Copy> queue::Queue<T> for RingBuffer<'_, T> {
240
226
fn ( self : & strg RingBuffer <T >[ @ring_len, @hd, @tl] , _)
241
227
ensures self : RingBuffer <T >{ rg: rg. tail < rg. ring_len}
242
228
) ]
243
- // NOTE: May want to strengthen this to talk about correctness
244
229
fn retain < F > ( & mut self , mut f : F )
245
230
where
246
231
F : FnMut ( & T ) -> bool ,
0 commit comments