1use alloc::{vec, vec::Vec};
4#[cfg(nightly)]
5use core::marker::PhantomData;
6use core::{fmt, iter::FusedIterator, slice::Iter};
7
8use crate::Frame;
9
10pub(crate) fn next<I: Iterator<Item = T>, T>(iter: &mut Vec<I>) -> Option<T> {
34 let out;
35 loop {
36 let last = iter.last_mut()?;
37
38 if let Some(next) = last.next() {
39 out = next;
40 break;
41 }
42
43 iter.pop();
45 }
46
47 Some(out)
48}
49
50#[must_use]
78#[derive(Clone)]
79pub struct Frames<'r> {
80 stack: Vec<Iter<'r, Frame>>,
81}
82
83impl<'r> Frames<'r> {
84 pub(crate) fn new(frames: &'r [Frame]) -> Self {
85 Self {
86 stack: vec![frames.iter()],
87 }
88 }
89}
90
91impl<'r> Iterator for Frames<'r> {
92 type Item = &'r Frame;
93
94 fn next(&mut self) -> Option<Self::Item> {
95 let frame = next(&mut self.stack)?;
96
97 self.stack.push(frame.sources().iter());
98 Some(frame)
99 }
100}
101
102impl FusedIterator for Frames<'_> {}
103
104impl fmt::Debug for Frames<'_> {
105 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
106 fmt.debug_list().entries(self.clone()).finish()
107 }
108}
109
110#[must_use]
117#[cfg(nightly)]
118pub struct RequestRef<'r, T: ?Sized> {
119 frames: Frames<'r>,
120 _marker: PhantomData<&'r T>,
121}
122
123#[cfg(nightly)]
124impl<'r, T: ?Sized> RequestRef<'r, T> {
125 pub(super) fn new(frames: &'r [Frame]) -> Self {
126 Self {
127 frames: Frames::new(frames),
128 _marker: PhantomData,
129 }
130 }
131}
132
133#[cfg(nightly)]
134impl<'r, T> Iterator for RequestRef<'r, T>
135where
136 T: ?Sized + 'static,
137{
138 type Item = &'r T;
139
140 fn next(&mut self) -> Option<Self::Item> {
141 self.frames.by_ref().find_map(Frame::request_ref)
142 }
143}
144
145#[cfg(nightly)]
146impl<T> FusedIterator for RequestRef<'_, T> where T: ?Sized + 'static {}
147
148#[cfg(nightly)]
149impl<T: ?Sized> Clone for RequestRef<'_, T> {
150 fn clone(&self) -> Self {
151 Self {
152 frames: self.frames.clone(),
153 _marker: PhantomData,
154 }
155 }
156}
157
158#[cfg(nightly)]
159impl<T> fmt::Debug for RequestRef<'_, T>
160where
161 T: ?Sized + fmt::Debug + 'static,
162{
163 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
164 fmt.debug_list().entries(self.clone()).finish()
165 }
166}
167
168#[must_use]
175#[cfg(nightly)]
176pub struct RequestValue<'r, T> {
177 frames: Frames<'r>,
178 _marker: PhantomData<T>,
179}
180
181#[cfg(nightly)]
182impl<'r, T> RequestValue<'r, T> {
183 pub(super) fn new(frames: &'r [Frame]) -> Self {
184 Self {
185 frames: Frames::new(frames),
186 _marker: PhantomData,
187 }
188 }
189}
190
191#[cfg(nightly)]
192impl<T> Iterator for RequestValue<'_, T>
193where
194 T: 'static,
195{
196 type Item = T;
197
198 fn next(&mut self) -> Option<Self::Item> {
199 self.frames.find_map(Frame::request_value)
200 }
201}
202
203#[cfg(nightly)]
204impl<T> FusedIterator for RequestValue<'_, T> where T: 'static {}
205
206#[cfg(nightly)]
207impl<T> Clone for RequestValue<'_, T> {
208 fn clone(&self) -> Self {
209 Self {
210 frames: self.frames.clone(),
211 _marker: PhantomData,
212 }
213 }
214}
215
216#[cfg(nightly)]
217impl<T> fmt::Debug for RequestValue<'_, T>
218where
219 T: fmt::Debug + 'static,
220{
221 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
222 fmt.debug_list().entries(self.clone()).finish()
223 }
224}