-
Notifications
You must be signed in to change notification settings - Fork 27
/
deque.rs
723 lines (602 loc) · 23.8 KB
/
deque.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
use std::{any::type_name, convert::TryInto, marker::PhantomData};
use cosmwasm_std::{
from_json, storage_keys::namespace_with_key, to_json_vec, StdError, StdResult, Storage,
};
use serde::{de::DeserializeOwned, Serialize};
use crate::namespace::Namespace;
// metadata keys need to have different length than the position type (4 bytes) to prevent collisions
const TAIL_KEY: &[u8] = b"t";
const HEAD_KEY: &[u8] = b"h";
/// A deque stores multiple items at the given key. It provides efficient FIFO and LIFO access,
/// as well as direct index access.
///
/// It has a maximum capacity of `u32::MAX - 1`. Make sure to never exceed that number when using this type.
/// If you do, the methods won't work as intended anymore.
pub struct Deque<T> {
// prefix of the deque items
namespace: Namespace,
// see https://doc.rust-lang.org/std/marker/struct.PhantomData.html#unused-type-parameters for why this is needed
item_type: PhantomData<T>,
}
impl<T> Deque<T> {
/// Creates a new [`Deque`] with the given storage key. This is a constant function only suitable
/// when you have a prefix in the form of a static string slice.
pub const fn new(prefix: &'static str) -> Self {
Self {
namespace: Namespace::from_static_str(prefix),
item_type: PhantomData,
}
}
/// Creates a new [`Deque`] with the given storage key. Use this if you might need to handle
/// a dynamic string. Otherwise, you should probably prefer [`Deque::new`].
pub fn new_dyn(prefix: impl Into<Namespace>) -> Self {
Self {
namespace: prefix.into(),
item_type: PhantomData,
}
}
}
impl<T: Serialize + DeserializeOwned> Deque<T> {
/// Adds the given value to the end of the deque
pub fn push_back(&self, storage: &mut dyn Storage, value: &T) -> StdResult<()> {
// save value
let pos = self.tail(storage)?;
self.set_unchecked(storage, pos, value)?;
// update tail
self.set_tail(storage, pos.wrapping_add(1));
Ok(())
}
/// Adds the given value to the front of the deque
pub fn push_front(&self, storage: &mut dyn Storage, value: &T) -> StdResult<()> {
// need to subtract first, because head potentially points to existing element
let pos = self.head(storage)?.wrapping_sub(1);
self.set_unchecked(storage, pos, value)?;
// update head
self.set_head(storage, pos);
Ok(())
}
/// Removes the last element of the deque and returns it
pub fn pop_back(&self, storage: &mut dyn Storage) -> StdResult<Option<T>> {
// get position
let pos = self.tail(storage)?.wrapping_sub(1);
let value = self.get_unchecked(storage, pos)?;
if value.is_some() {
self.remove_unchecked(storage, pos);
// only update tail if a value was popped
self.set_tail(storage, pos);
}
Ok(value)
}
/// Removes the first element of the deque and returns it
pub fn pop_front(&self, storage: &mut dyn Storage) -> StdResult<Option<T>> {
// get position
let pos = self.head(storage)?;
let value = self.get_unchecked(storage, pos)?;
if value.is_some() {
self.remove_unchecked(storage, pos);
// only update head if a value was popped
self.set_head(storage, pos.wrapping_add(1));
}
Ok(value)
}
/// Returns the first element of the deque without removing it
pub fn front(&self, storage: &dyn Storage) -> StdResult<Option<T>> {
let pos = self.head(storage)?;
self.get_unchecked(storage, pos)
}
/// Returns the last element of the deque without removing it
pub fn back(&self, storage: &dyn Storage) -> StdResult<Option<T>> {
let pos = self.tail(storage)?.wrapping_sub(1);
self.get_unchecked(storage, pos)
}
/// Gets the length of the deque.
#[allow(clippy::len_without_is_empty)]
pub fn len(&self, storage: &dyn Storage) -> StdResult<u32> {
Ok(calc_len(self.head(storage)?, self.tail(storage)?))
}
/// Returns `true` if the deque contains no elements.
pub fn is_empty(&self, storage: &dyn Storage) -> StdResult<bool> {
Ok(self.len(storage)? == 0)
}
/// Gets the head position from storage.
///
/// Unless the deque is empty, this points to the first element.
#[inline]
fn head(&self, storage: &dyn Storage) -> StdResult<u32> {
self.read_meta_key(storage, HEAD_KEY)
}
/// Gets the tail position from storage.
///
/// This points to the first empty position after the last element.
#[inline]
fn tail(&self, storage: &dyn Storage) -> StdResult<u32> {
self.read_meta_key(storage, TAIL_KEY)
}
#[inline]
fn set_head(&self, storage: &mut dyn Storage, value: u32) {
self.set_meta_key(storage, HEAD_KEY, value);
}
#[inline]
fn set_tail(&self, storage: &mut dyn Storage, value: u32) {
self.set_meta_key(storage, TAIL_KEY, value);
}
/// Helper method for `tail` and `head` methods to handle reading the value from storage
fn read_meta_key(&self, storage: &dyn Storage, key: &[u8]) -> StdResult<u32> {
let full_key = namespace_with_key(&[self.namespace.as_slice()], key);
storage
.get(&full_key)
.map(|vec| {
Ok(u32::from_be_bytes(
vec.as_slice()
.try_into()
.map_err(|e| StdError::parse_err("u32", e))?,
))
})
.unwrap_or(Ok(0))
}
/// Helper method for `set_tail` and `set_head` methods to write to storage
#[inline]
fn set_meta_key(&self, storage: &mut dyn Storage, key: &[u8], value: u32) {
let full_key = namespace_with_key(&[self.namespace.as_slice()], key);
storage.set(&full_key, &value.to_be_bytes());
}
/// Returns the value at the given position in the queue or `None` if the index is out of bounds
pub fn get(&self, storage: &dyn Storage, pos: u32) -> StdResult<Option<T>> {
let head = self.head(storage)?;
let tail = self.tail(storage)?;
if pos >= calc_len(head, tail) {
// out of bounds
return Ok(None);
}
let pos = head.wrapping_add(pos);
self.get_unchecked(storage, pos)
.and_then(|v| v.ok_or_else(|| StdError::not_found(format!("deque position {}", pos))))
.map(Some)
}
/// Sets the value at the given position in the queue. Returns [`StdError::NotFound`] if index is out of bounds
pub fn set(&self, storage: &mut dyn Storage, pos: u32, value: &T) -> StdResult<()> {
let head = self.head(storage)?;
let tail = self.tail(storage)?;
if pos >= calc_len(head, tail) {
// out of bounds
return Err(StdError::not_found(format!("deque position {}", pos)));
}
self.set_unchecked(storage, pos, value)
}
/// Tries to get the value at the given position
/// Used internally
fn get_unchecked(&self, storage: &dyn Storage, pos: u32) -> StdResult<Option<T>> {
let prefixed_key = namespace_with_key(&[self.namespace.as_slice()], &pos.to_be_bytes());
let value = storage.get(&prefixed_key);
value.map(|v| from_json(v)).transpose()
}
/// Removes the value at the given position
/// Used internally
fn remove_unchecked(&self, storage: &mut dyn Storage, pos: u32) {
let prefixed_key = namespace_with_key(&[self.namespace.as_slice()], &pos.to_be_bytes());
storage.remove(&prefixed_key);
}
/// Tries to set the value at the given position
/// Used internally when pushing
fn set_unchecked(&self, storage: &mut dyn Storage, pos: u32, value: &T) -> StdResult<()> {
let prefixed_key = namespace_with_key(&[self.namespace.as_slice()], &pos.to_be_bytes());
storage.set(&prefixed_key, &to_json_vec(value)?);
Ok(())
}
}
// used internally to avoid additional storage loads
#[inline]
fn calc_len(head: u32, tail: u32) -> u32 {
tail.wrapping_sub(head)
}
impl<T: Serialize + DeserializeOwned> Deque<T> {
pub fn iter<'a>(&'a self, storage: &'a dyn Storage) -> StdResult<DequeIter<'a, T>> {
Ok(DequeIter {
deque: self,
storage,
start: self.head(storage)?,
end: self.tail(storage)?,
})
}
}
pub struct DequeIter<'a, T>
where
T: Serialize + DeserializeOwned,
{
deque: &'a Deque<T>,
storage: &'a dyn Storage,
start: u32,
end: u32,
}
impl<'a, T> Iterator for DequeIter<'a, T>
where
T: Serialize + DeserializeOwned,
{
type Item = StdResult<T>;
fn next(&mut self) -> Option<Self::Item> {
if self.start == self.end {
return None;
}
let item = self
.deque
.get_unchecked(self.storage, self.start)
.and_then(|item| item.ok_or_else(|| StdError::not_found(type_name::<T>())));
self.start = self.start.wrapping_add(1);
Some(item)
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = calc_len(self.start, self.end) as usize;
(len, Some(len))
}
// The default implementation calls `next` repeatedly, which is very costly in our case.
// It is used when skipping over items, so this allows cheap skipping.
//
// Once `advance_by` is stabilized, we can implement that instead (`nth` calls it internally).
fn nth(&mut self, n: usize) -> Option<Self::Item> {
// make sure that we don't skip past the end
if calc_len(self.start, self.end) < n as u32 {
// mark as empty
self.start = self.end;
} else {
self.start = self.start.wrapping_add(n as u32);
}
self.next()
}
}
impl<'a, T> DoubleEndedIterator for DequeIter<'a, T>
where
T: Serialize + DeserializeOwned,
{
fn next_back(&mut self) -> Option<Self::Item> {
if self.start == self.end {
return None;
}
let item = self
.deque
.get_unchecked(self.storage, self.end.wrapping_sub(1)) // end points to position after last element
.and_then(|item| item.ok_or_else(|| StdError::not_found(type_name::<T>())));
self.end = self.end.wrapping_sub(1);
Some(item)
}
// see [`DequeIter::nth`]
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
// make sure that we don't skip past the start
if calc_len(self.start, self.end) < n as u32 {
// mark as empty
self.end = self.start;
} else {
self.end = self.end.wrapping_sub(n as u32);
}
self.next_back()
}
}
#[cfg(test)]
mod tests {
use crate::deque::Deque;
use cosmwasm_std::testing::MockStorage;
use cosmwasm_std::{StdError, StdResult};
use serde::{Deserialize, Serialize};
#[test]
fn owned_key() {
let mut store = MockStorage::new();
for i in 1..4 {
let key = format!("key{}", i);
let item = Deque::new_dyn(key);
for i in 0..i {
item.push_back(&mut store, &i).unwrap();
}
}
assert_eq!(
Deque::<u32>::new("key1")
.iter(&store)
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![0]
);
assert_eq!(
Deque::<u32>::new("key2")
.iter(&store)
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![0, 1]
);
assert_eq!(
Deque::<u32>::new("key3")
.iter(&store)
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![0, 1, 2]
);
}
#[test]
fn push_and_pop() {
const PEOPLE: Deque<String> = Deque::new("people");
let mut store = MockStorage::new();
// push some entries
PEOPLE.push_back(&mut store, &"jack".to_owned()).unwrap();
PEOPLE.push_back(&mut store, &"john".to_owned()).unwrap();
PEOPLE.push_back(&mut store, &"joanne".to_owned()).unwrap();
// pop them, should be in correct order
assert_eq!("jack", PEOPLE.pop_front(&mut store).unwrap().unwrap());
assert_eq!("john", PEOPLE.pop_front(&mut store).unwrap().unwrap());
// push again in-between
PEOPLE.push_back(&mut store, &"jason".to_owned()).unwrap();
// pop last person from first batch
assert_eq!("joanne", PEOPLE.pop_front(&mut store).unwrap().unwrap());
// pop the entry pushed in-between
assert_eq!("jason", PEOPLE.pop_front(&mut store).unwrap().unwrap());
// nothing after that
assert_eq!(None, PEOPLE.pop_front(&mut store).unwrap());
// now push to the front
PEOPLE.push_front(&mut store, &"pascal".to_owned()).unwrap();
PEOPLE.push_front(&mut store, &"peter".to_owned()).unwrap();
PEOPLE.push_front(&mut store, &"paul".to_owned()).unwrap();
assert_eq!("pascal", PEOPLE.pop_back(&mut store).unwrap().unwrap());
assert_eq!("paul", PEOPLE.pop_front(&mut store).unwrap().unwrap());
assert_eq!("peter", PEOPLE.pop_back(&mut store).unwrap().unwrap());
}
#[test]
fn length() {
let deque: Deque<u32> = Deque::new("test");
let mut store = MockStorage::new();
assert_eq!(deque.len(&store).unwrap(), 0);
assert!(deque.is_empty(&store).unwrap());
// push some entries
deque.push_front(&mut store, &1234).unwrap();
deque.push_back(&mut store, &2345).unwrap();
deque.push_front(&mut store, &3456).unwrap();
deque.push_back(&mut store, &4567).unwrap();
assert_eq!(deque.len(&store).unwrap(), 4);
assert!(!deque.is_empty(&store).unwrap());
// pop some
deque.pop_front(&mut store).unwrap();
deque.pop_back(&mut store).unwrap();
deque.pop_front(&mut store).unwrap();
assert_eq!(deque.len(&store).unwrap(), 1);
assert!(!deque.is_empty(&store).unwrap());
// pop the last one
deque.pop_front(&mut store).unwrap();
assert_eq!(deque.len(&store).unwrap(), 0);
assert!(deque.is_empty(&store).unwrap());
// should stay 0 after that
assert_eq!(deque.pop_back(&mut store).unwrap(), None);
assert_eq!(
deque.len(&store).unwrap(),
0,
"popping from empty deque should keep length 0"
);
assert!(deque.is_empty(&store).unwrap());
}
#[test]
fn iterator() {
let deque: Deque<u32> = Deque::new("test");
let mut store = MockStorage::new();
// push some items
deque.push_back(&mut store, &1).unwrap();
deque.push_back(&mut store, &2).unwrap();
deque.push_back(&mut store, &3).unwrap();
deque.push_back(&mut store, &4).unwrap();
let items: StdResult<Vec<_>> = deque.iter(&store).unwrap().collect();
assert_eq!(items.unwrap(), [1, 2, 3, 4]);
// nth should work correctly
let mut iter = deque.iter(&store).unwrap();
assert_eq!(iter.nth(6), None);
assert_eq!(iter.start, iter.end, "iter should detect skipping too far");
assert_eq!(iter.next(), None);
let mut iter = deque.iter(&store).unwrap();
assert_eq!(iter.nth(1).unwrap().unwrap(), 2);
assert_eq!(iter.next().unwrap().unwrap(), 3);
}
#[test]
fn reverse_iterator() {
let deque: Deque<u32> = Deque::new("test");
let mut store = MockStorage::new();
// push some items
deque.push_back(&mut store, &1).unwrap();
deque.push_back(&mut store, &2).unwrap();
deque.push_back(&mut store, &3).unwrap();
deque.push_back(&mut store, &4).unwrap();
let items: StdResult<Vec<_>> = deque.iter(&store).unwrap().rev().collect();
assert_eq!(items.unwrap(), [4, 3, 2, 1]);
// nth should work correctly
let mut iter = deque.iter(&store).unwrap();
assert_eq!(iter.nth_back(6), None);
assert_eq!(iter.start, iter.end, "iter should detect skipping too far");
assert_eq!(iter.next_back(), None);
let mut iter = deque.iter(&store).unwrap().rev();
assert_eq!(iter.nth(1).unwrap().unwrap(), 3);
assert_eq!(iter.next().unwrap().unwrap(), 2);
// mixed
let mut iter = deque.iter(&store).unwrap();
assert_eq!(iter.next().unwrap().unwrap(), 1);
assert_eq!(iter.next_back().unwrap().unwrap(), 4);
assert_eq!(iter.next_back().unwrap().unwrap(), 3);
assert_eq!(iter.next().unwrap().unwrap(), 2);
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
}
#[test]
fn wrapping() {
let deque: Deque<u32> = Deque::new("test");
let mut store = MockStorage::new();
// simulate deque that was pushed and popped `u32::MAX` times
deque.set_head(&mut store, u32::MAX);
deque.set_tail(&mut store, u32::MAX);
// should be empty
assert_eq!(deque.pop_front(&mut store).unwrap(), None);
assert_eq!(deque.len(&store).unwrap(), 0);
// pushing should still work
deque.push_back(&mut store, &1).unwrap();
assert_eq!(
deque.len(&store).unwrap(),
1,
"length should calculate correctly, even when wrapping"
);
assert_eq!(
deque.pop_front(&mut store).unwrap(),
Some(1),
"popping should work, even when wrapping"
);
}
#[test]
fn wrapping_iterator() {
let deque: Deque<u32> = Deque::new("test");
let mut store = MockStorage::new();
deque.set_head(&mut store, u32::MAX);
deque.set_tail(&mut store, u32::MAX);
deque.push_back(&mut store, &1).unwrap();
deque.push_back(&mut store, &2).unwrap();
deque.push_back(&mut store, &3).unwrap();
deque.push_back(&mut store, &4).unwrap();
deque.push_back(&mut store, &5).unwrap();
let mut iter = deque.iter(&store).unwrap();
assert_eq!(iter.next().unwrap().unwrap(), 1);
assert_eq!(iter.next().unwrap().unwrap(), 2);
assert_eq!(iter.next_back().unwrap().unwrap(), 5);
assert_eq!(iter.nth(1).unwrap().unwrap(), 4);
assert_eq!(iter.nth(1), None);
assert_eq!(iter.start, iter.end);
}
#[test]
fn front_back() {
let deque: Deque<u64> = Deque::new("test");
let mut store = MockStorage::new();
assert_eq!(deque.back(&store).unwrap(), None);
deque.push_back(&mut store, &1).unwrap();
assert_eq!(deque.back(&store).unwrap(), Some(1));
assert_eq!(deque.front(&store).unwrap(), Some(1));
deque.push_back(&mut store, &2).unwrap();
assert_eq!(deque.back(&store).unwrap(), Some(2));
assert_eq!(deque.front(&store).unwrap(), Some(1));
deque.push_front(&mut store, &3).unwrap();
assert_eq!(deque.back(&store).unwrap(), Some(2));
assert_eq!(deque.front(&store).unwrap(), Some(3));
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
struct Data {
pub name: String,
pub age: i32,
}
const DATA: Deque<Data> = Deque::new("data");
#[test]
fn readme_works() -> StdResult<()> {
let mut store = MockStorage::new();
// read methods return a wrapped Option<T>, so None if the deque is empty
let empty = DATA.front(&store)?;
assert_eq!(None, empty);
// some example entries
let p1 = Data {
name: "admin".to_string(),
age: 1234,
};
let p2 = Data {
name: "user".to_string(),
age: 123,
};
// use it like a queue by pushing and popping at opposite ends
DATA.push_back(&mut store, &p1)?;
DATA.push_back(&mut store, &p2)?;
let admin = DATA.pop_front(&mut store)?;
assert_eq!(admin.as_ref(), Some(&p1));
let user = DATA.pop_front(&mut store)?;
assert_eq!(user.as_ref(), Some(&p2));
// or push and pop at the same end to use it as a stack
DATA.push_back(&mut store, &p1)?;
DATA.push_back(&mut store, &p2)?;
let user = DATA.pop_back(&mut store)?;
assert_eq!(user.as_ref(), Some(&p2));
let admin = DATA.pop_back(&mut store)?;
assert_eq!(admin.as_ref(), Some(&p1));
// you can also iterate over it
DATA.push_front(&mut store, &p1)?;
DATA.push_front(&mut store, &p2)?;
let all: StdResult<Vec<_>> = DATA.iter(&store)?.collect();
assert_eq!(all?, [p2.clone(), p1.clone()]);
// or access an index directly
assert_eq!(DATA.get(&store, 0)?, Some(p2));
assert_eq!(DATA.get(&store, 1)?, Some(p1));
assert_eq!(DATA.get(&store, 3)?, None);
Ok(())
}
#[test]
fn iterator_errors_when_item_missing() {
let mut store = MockStorage::new();
let deque = Deque::new("error_test");
deque.push_back(&mut store, &1u32).unwrap();
// manually remove it
deque.remove_unchecked(&mut store, 0);
let mut iter = deque.iter(&store).unwrap();
assert!(
matches!(iter.next(), Some(Err(StdError::NotFound { .. }))),
"iterator should error when item is missing"
);
let mut iter = deque.iter(&store).unwrap().rev();
assert!(
matches!(iter.next(), Some(Err(StdError::NotFound { .. }))),
"reverse iterator should error when item is missing"
);
}
#[test]
fn get() {
let mut store = MockStorage::new();
let deque = Deque::new("test");
deque.push_back(&mut store, &1u32).unwrap();
deque.push_back(&mut store, &2).unwrap();
assert_eq!(deque.get(&store, 0).unwrap(), Some(1));
assert_eq!(deque.get(&store, 1).unwrap(), Some(2));
assert_eq!(
deque.get(&store, 2).unwrap(),
None,
"out of bounds access should return None"
);
// manually remove storage item
deque.remove_unchecked(&mut store, 1);
assert!(
matches!(deque.get(&store, 1), Err(StdError::NotFound { .. })),
"missing deque item should error"
);
// start fresh
let deque = Deque::new("test2");
deque.push_back(&mut store, &0u32).unwrap();
deque.push_back(&mut store, &1).unwrap();
// push to front to move the head index
deque.push_front(&mut store, &u32::MAX).unwrap();
deque.push_front(&mut store, &(u32::MAX - 1)).unwrap();
assert_eq!(deque.get(&store, 0).unwrap().unwrap(), u32::MAX - 1);
assert_eq!(deque.get(&store, 1).unwrap().unwrap(), u32::MAX);
assert_eq!(deque.get(&store, 2).unwrap().unwrap(), 0);
assert_eq!(deque.get(&store, 3).unwrap().unwrap(), 1);
assert_eq!(
deque.get(&store, 5).unwrap(),
None,
"out of bounds access should return None"
);
}
#[test]
fn set() {
let mut store = MockStorage::new();
let deque = Deque::new("test");
deque.push_back(&mut store, &1u32).unwrap();
deque.push_back(&mut store, &2).unwrap();
deque.set(&mut store, 1, &3).unwrap();
assert_eq!(deque.get(&store, 1).unwrap(), Some(3));
assert_eq!(deque.back(&store).unwrap(), Some(3));
assert_eq!(
deque.get(&store, 2).unwrap(),
None,
"out of bounds access should return None"
);
assert!(
matches!(deque.set(&mut store, 2, &3), Err(StdError::NotFound { .. })),
"setting value at an out of bounds index should error"
);
assert_eq!(deque.pop_back(&mut store), Ok(Some(3)));
assert!(
matches!(deque.set(&mut store, 1, &3), Err(StdError::NotFound { .. })),
"setting value at an out of bounds index should error"
);
}
}