|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +//! Opaque iterator types for [`Array`]. |
| 5 | +//! |
| 6 | +//! These newtypes wrap the storage backend's iterators so the backend can be |
| 7 | +//! swapped without changing any iterator type signatures observed by callers. |
| 8 | +
|
| 9 | +use alloc::vec; |
| 10 | +use core::iter::FusedIterator; |
| 11 | +use core::slice; |
| 12 | + |
| 13 | +use super::Array; |
| 14 | +use crate::value::Value; |
| 15 | + |
| 16 | +/// Owned iterator over `Value` elements. |
| 17 | +#[derive(Debug)] |
| 18 | +pub struct IntoIter { |
| 19 | + pub(super) inner: vec::IntoIter<Value>, |
| 20 | +} |
| 21 | + |
| 22 | +impl Iterator for IntoIter { |
| 23 | + type Item = Value; |
| 24 | + |
| 25 | + #[inline] |
| 26 | + fn next(&mut self) -> Option<Self::Item> { |
| 27 | + self.inner.next() |
| 28 | + } |
| 29 | + |
| 30 | + #[inline] |
| 31 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 32 | + self.inner.size_hint() |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl DoubleEndedIterator for IntoIter { |
| 37 | + #[inline] |
| 38 | + fn next_back(&mut self) -> Option<Self::Item> { |
| 39 | + self.inner.next_back() |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl ExactSizeIterator for IntoIter { |
| 44 | + #[inline] |
| 45 | + fn len(&self) -> usize { |
| 46 | + self.inner.len() |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl FusedIterator for IntoIter {} |
| 51 | + |
| 52 | +/// Borrowed iterator over `&Value` elements. |
| 53 | +#[derive(Debug, Clone)] |
| 54 | +pub struct Iter<'a> { |
| 55 | + pub(super) inner: slice::Iter<'a, Value>, |
| 56 | +} |
| 57 | + |
| 58 | +impl<'a> Iterator for Iter<'a> { |
| 59 | + type Item = &'a Value; |
| 60 | + |
| 61 | + #[inline] |
| 62 | + fn next(&mut self) -> Option<Self::Item> { |
| 63 | + self.inner.next() |
| 64 | + } |
| 65 | + |
| 66 | + #[inline] |
| 67 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 68 | + self.inner.size_hint() |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl<'a> DoubleEndedIterator for Iter<'a> { |
| 73 | + #[inline] |
| 74 | + fn next_back(&mut self) -> Option<Self::Item> { |
| 75 | + self.inner.next_back() |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl<'a> ExactSizeIterator for Iter<'a> { |
| 80 | + #[inline] |
| 81 | + fn len(&self) -> usize { |
| 82 | + self.inner.len() |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl<'a> FusedIterator for Iter<'a> {} |
| 87 | + |
| 88 | +/// Mutable borrowed iterator over `&mut Value` elements. |
| 89 | +#[derive(Debug)] |
| 90 | +pub struct IterMut<'a> { |
| 91 | + pub(super) inner: slice::IterMut<'a, Value>, |
| 92 | +} |
| 93 | + |
| 94 | +impl<'a> Iterator for IterMut<'a> { |
| 95 | + type Item = &'a mut Value; |
| 96 | + |
| 97 | + #[inline] |
| 98 | + fn next(&mut self) -> Option<Self::Item> { |
| 99 | + self.inner.next() |
| 100 | + } |
| 101 | + |
| 102 | + #[inline] |
| 103 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 104 | + self.inner.size_hint() |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +impl<'a> DoubleEndedIterator for IterMut<'a> { |
| 109 | + #[inline] |
| 110 | + fn next_back(&mut self) -> Option<Self::Item> { |
| 111 | + self.inner.next_back() |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +impl<'a> ExactSizeIterator for IterMut<'a> { |
| 116 | + #[inline] |
| 117 | + fn len(&self) -> usize { |
| 118 | + self.inner.len() |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +impl<'a> FusedIterator for IterMut<'a> {} |
| 123 | + |
| 124 | +/// Opaque resumable cursor over an [`Array`]'s elements. |
| 125 | +/// |
| 126 | +/// Self-owned: holds no borrow on the `Array`, so it can be stored as a field |
| 127 | +/// of a long-lived state struct (e.g. an RVM iteration frame). |
| 128 | +#[cfg(feature = "rvm")] |
| 129 | +#[derive(Debug, Clone, Default)] |
| 130 | +pub struct Cursor { |
| 131 | + pub(super) next: usize, |
| 132 | +} |
| 133 | + |
| 134 | +impl IntoIterator for Array { |
| 135 | + type Item = Value; |
| 136 | + type IntoIter = IntoIter; |
| 137 | + |
| 138 | + #[inline] |
| 139 | + fn into_iter(self) -> Self::IntoIter { |
| 140 | + IntoIter { |
| 141 | + inner: self.inner.into_iter(), |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +impl<'a> IntoIterator for &'a Array { |
| 147 | + type Item = &'a Value; |
| 148 | + type IntoIter = Iter<'a>; |
| 149 | + |
| 150 | + #[inline] |
| 151 | + fn into_iter(self) -> Self::IntoIter { |
| 152 | + Iter { |
| 153 | + inner: self.inner.iter(), |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +impl<'a> IntoIterator for &'a mut Array { |
| 159 | + type Item = &'a mut Value; |
| 160 | + type IntoIter = IterMut<'a>; |
| 161 | + |
| 162 | + #[inline] |
| 163 | + fn into_iter(self) -> Self::IntoIter { |
| 164 | + IterMut { |
| 165 | + inner: self.inner.iter_mut(), |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments