1#[cfg_attr(feature = "std", allow(unused_imports))]
4use alloc::{vec, vec::Vec};
5#[cfg(nightly)]
6use core::marker::PhantomData;
7use core::{
8 fmt,
9 iter::FusedIterator,
10 slice::{Iter, IterMut},
11};
12
13use crate::Frame;
14
15fn next<I: Iterator<Item = T>, T>(iter: &mut Vec<I>) -> Option<T> {
36 let out;
37 loop {
38 let last = iter.last_mut()?;
39
40 if let Some(next) = last.next() {
41 out = next;
42 break;
43 }
44
45 iter.pop();
47 }
48
49 Some(out)
50}
51
52#[must_use]
80#[derive(Clone)]
81pub struct Frames<'r> {
82 stack: Vec<Iter<'r, Frame>>,
83}
84
85impl<'r> Frames<'r> {
86 pub(crate) fn new(frames: &'r [Frame]) -> Self {
87 Self {
88 stack: vec![frames.iter()],
89 }
90 }
91}
92
93impl<'r> Iterator for Frames<'r> {
94 type Item = &'r Frame;
95
96 fn next(&mut self) -> Option<Self::Item> {
97 let frame = next(&mut self.stack)?;
98
99 self.stack.push(frame.sources().iter());
100 Some(frame)
101 }
102}
103
104impl<'r> FusedIterator for Frames<'r> {}
105
106impl fmt::Debug for Frames<'_> {
107 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
108 fmt.debug_list().entries(self.clone()).finish()
109 }
110}
111
112#[must_use]
119pub struct FramesMut<'r> {
120 stack: Vec<IterMut<'r, Frame>>,
121}
122
123impl<'r> FramesMut<'r> {
124 pub(crate) fn new(frames: &'r mut [Frame]) -> Self {
125 Self {
126 stack: vec![frames.iter_mut()],
127 }
128 }
129}
130
131impl<'r> Iterator for FramesMut<'r> {
132 type Item = &'r mut Frame;
133
134 fn next(&mut self) -> Option<Self::Item> {
135 let frame = next(&mut self.stack)?;
136 let frame: *mut Frame = frame;
137
138 unsafe {
146 self.stack.push((*frame).sources_mut().iter_mut());
147
148 Some(&mut *frame)
149 }
150 }
151}
152
153impl<'r> FusedIterator for FramesMut<'r> {}
154
155#[must_use]
162#[cfg(nightly)]
163pub struct RequestRef<'r, T: ?Sized> {
164 frames: Frames<'r>,
165 _marker: PhantomData<&'r T>,
166}
167
168#[cfg(nightly)]
169impl<'r, T: ?Sized> RequestRef<'r, T> {
170 pub(super) fn new(frames: &'r [Frame]) -> Self {
171 Self {
172 frames: Frames::new(frames),
173 _marker: PhantomData,
174 }
175 }
176}
177
178#[cfg(nightly)]
179impl<'r, T> Iterator for RequestRef<'r, T>
180where
181 T: ?Sized + 'static,
182{
183 type Item = &'r T;
184
185 fn next(&mut self) -> Option<Self::Item> {
186 self.frames.by_ref().find_map(Frame::request_ref)
187 }
188}
189
190#[cfg(nightly)]
191impl<'r, T> FusedIterator for RequestRef<'r, T> where T: ?Sized + 'static {}
192
193#[cfg(nightly)]
194impl<T: ?Sized> Clone for RequestRef<'_, T> {
195 fn clone(&self) -> Self {
196 Self {
197 frames: self.frames.clone(),
198 _marker: PhantomData,
199 }
200 }
201}
202
203#[cfg(nightly)]
204impl<'r, T> fmt::Debug for RequestRef<'r, T>
205where
206 T: ?Sized + fmt::Debug + 'static,
207{
208 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
209 fmt.debug_list().entries(self.clone()).finish()
210 }
211}
212
213#[must_use]
220#[cfg(nightly)]
221pub struct RequestValue<'r, T> {
222 frames: Frames<'r>,
223 _marker: PhantomData<T>,
224}
225
226#[cfg(nightly)]
227impl<'r, T> RequestValue<'r, T> {
228 pub(super) fn new(frames: &'r [Frame]) -> Self {
229 Self {
230 frames: Frames::new(frames),
231 _marker: PhantomData,
232 }
233 }
234}
235
236#[cfg(nightly)]
237impl<'r, T> Iterator for RequestValue<'r, T>
238where
239 T: 'static,
240{
241 type Item = T;
242
243 fn next(&mut self) -> Option<Self::Item> {
244 self.frames.find_map(Frame::request_value)
245 }
246}
247
248#[cfg(nightly)]
249impl<'r, T> FusedIterator for RequestValue<'r, T> where T: 'static {}
250
251#[cfg(nightly)]
252impl<T> Clone for RequestValue<'_, T> {
253 fn clone(&self) -> Self {
254 Self {
255 frames: self.frames.clone(),
256 _marker: PhantomData,
257 }
258 }
259}
260
261#[cfg(nightly)]
262impl<'r, T> fmt::Debug for RequestValue<'r, T>
263where
264 T: fmt::Debug + 'static,
265{
266 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
267 fmt.debug_list().entries(self.clone()).finish()
268 }
269}