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
use core::iter::FusedIterator;
use crate::{Report, ReportRef, markers::Cloneable};
/// FIXME: Once rust-lang/rust#132922 gets resolved, we can make the `raw` field
/// an unsafe field and remove this module.
mod limit_field_access {
use core::marker::PhantomData;
use rootcause_internals::RawReport;
use crate::markers::{Dynamic, SendSync};
/// An iterator over references to reports in a [`ReportCollection`].
///
/// This iterator yields [`ReportRef`] instances, allowing you to iterate
/// over the reports in a collection without taking ownership.
///
/// # Examples
///
/// ```
/// use rootcause::{report, report_collection::ReportCollection};
///
/// let mut collection = ReportCollection::new();
/// collection.push(report!("Error 1").into_cloneable());
/// collection.push(report!("Error 2").into_cloneable());
///
/// // Iterate over references to reports
/// for report_ref in collection.iter() {
/// println!("Report: {}", report_ref);
/// }
/// ```
///
/// [`ReportRef`]: crate::ReportRef
/// [`ReportCollection`]: crate::report_collection::ReportCollection
#[must_use]
pub struct ReportCollectionIter<
'a,
Context: ?Sized + 'static = Dynamic,
ThreadSafety: 'static = SendSync,
> {
/// # Safety
///
/// The following safety invariants are guaranteed to be upheld as long
/// as this struct exists:
///
/// 1. Either the collection must be empty, `C` must either be a type
/// bounded by `Sized`, or C must be `Dynamic`.
/// 2. Either the collection must be empty or `T` must either be
/// `SendSync` or `Local`.
/// 3. If `C` is a `Sized` type: The contexts of the [`RawReport`]s are
/// all of type `C`.
/// 4. All references to these report or any sub-reports are compatible
/// with shared ownership. Specifically there are no references with
/// an assumption that the strong_count is `1`.
/// 5. If `T = SendSync`: All contexts and attachments in the
/// [`RawReport`]s and all sub-reports must be `Send+Sync`.
raw: core::slice::Iter<'a, RawReport>,
_context: PhantomData<Context>,
_thread_safety: PhantomData<ThreadSafety>,
}
impl<'a, C: ?Sized, T> ReportCollectionIter<'a, C, T> {
/// Creates a new `ReportCollectionIter` from an iterator of raw reports
///
/// # Safety
///
/// The caller must ensure:
///
/// 1. Either the collection must be empty, `C` must either be a type
/// bounded by `Sized`, or C must be `Dynamic`.
/// 2. Either the collection must be empty or `T` must either be
/// `SendSync` or `Local`.
/// 3. If `C` is a `Sized` type: The contexts of the [`RawReport`]s are
/// all of type `C`.
/// 4. All references to these report or any sub-reports are compatible
/// with shared ownership. Specifically there are no references with
/// an assumption that the strong_count is `1`.
/// 5. If `T = SendSync`: All contexts and attachments in the
/// [`RawReport`]s and all sub-reports must be `Send+Sync`.
pub(crate) unsafe fn from_raw(raw: &'a [RawReport]) -> Self {
// SAFETY: We must uphold the safety invariants of this type:
// 1. Guaranteed by the caller
// 2. Guaranteed by the caller
// 3. Guaranteed by the caller
// 4. Guaranteed by the caller
// 5. Guaranteed by the caller
Self {
raw: raw.iter(),
_context: PhantomData,
_thread_safety: PhantomData,
}
}
/// Returns a reference to the underlying raw report iterator
pub(crate) fn as_raw(&self) -> &core::slice::Iter<'a, RawReport> {
// SAFETY: We must uphold the safety invariants of this type:
// 1. Upheld as the type parameters do not change.
// 2. Upheld as the type parameters do not change.
// 3. No mutation occurs here, so the invariants are preserved
// 4. Upheld, as all references created here are compatible
// 5. No mutation occurs here, so the invariants are preserved
&self.raw
}
/// Returns a mutable reference to the underlying raw report iterator
///
/// # Safety
///
/// The caller must ensure:
///
/// 1. If `C` is a `Sized` type: No mutation is performed that would
/// invalidate the invariant that all contexts are of type `C`.
/// 2. No mutation is performed that would invalidate the shared
/// ownership invariant.
/// 3. If `T = SendSync`: No mutation is performed that invalidates the
/// invariant that all inner contexts and attachments are `Send +
/// Sync`.
pub(crate) unsafe fn as_raw_mut(&mut self) -> &mut core::slice::Iter<'a, RawReport> {
// SAFETY: We must uphold the safety invariants of this type:
// 1. Upheld as the type parameters do not change.
// 2. Upheld as the type parameters do not change.
// 3. Guaranteed by the caller
// 4. Upheld, as all references created here are compatible
// 5. Guaranteed by the caller
&mut self.raw
}
}
}
pub use limit_field_access::ReportCollectionIter;
impl<'a, C: ?Sized, T> Iterator for ReportCollectionIter<'a, C, T> {
type Item = ReportRef<'a, C, Cloneable, T>;
fn next(&mut self) -> Option<Self::Item> {
// SAFETY: We only remove items, we don't mutate them.
// 1. No mutation that breaks the context-type invariant
// 2. No mutation that breaks the shared-ownership invariant
// 3. No mutation that breaks the Send+Sync invariant for T=SendSync
let raw = unsafe { self.as_raw_mut() };
let item = raw.next()?.as_ref();
// SAFETY:
// 1. Guaranteed by the invariants of this type.
// 2. `O=Cloneable`, so this is trivially true.
// 3. Guaranteed by the invariants of this type.
// 4. Guaranteed by the invariants of this type.
// 5. Guaranteed by the invariants of this type.
// 6. Guaranteed by the invariants of this type.
// 7. Guaranteed by the invariants of this type.
let raw = unsafe { ReportRef::<C, Cloneable, T>::from_raw(item) };
Some(raw)
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.as_raw().size_hint()
}
}
impl<'a, C: ?Sized, T> DoubleEndedIterator for ReportCollectionIter<'a, C, T> {
fn next_back(&mut self) -> Option<Self::Item> {
// SAFETY: We only remove items, we don't mutate them.
// 1. No mutation that breaks the context-type invariant
// 2. No mutation that breaks the shared-ownership invariant
// 3. No mutation that breaks the Send+Sync invariant for T=SendSync
let raw = unsafe { self.as_raw_mut() };
let item = raw.next_back()?.as_ref();
// SAFETY:
// 1. Guaranteed by the invariants of this type.
// 2. `O=Cloneable`, so this is trivially true.
// 3. Guaranteed by the invariants of this type.
// 4. Guaranteed by the invariants of this type.
// 5. Guaranteed by the invariants of this type.
// 6. Guaranteed by the invariants of this type.
// 7. Guaranteed by the invariants of this type.
let raw = unsafe { ReportRef::<C, Cloneable, T>::from_raw(item) };
Some(raw)
}
}
impl<'a, C: ?Sized, T> ExactSizeIterator for ReportCollectionIter<'a, C, T> {
fn len(&self) -> usize {
self.as_raw().len()
}
}
impl<'a, C: ?Sized, T> FusedIterator for ReportCollectionIter<'a, C, T> {}
impl<'a, C: ?Sized, T> Unpin for ReportCollectionIter<'a, C, T> {}
/// FIXME: Once rust-lang/rust#132922 gets resolved, we can make the `raw` field
/// an unsafe field and remove this module.
mod limit_field_access2 {
use alloc::vec::Vec;
use core::marker::PhantomData;
use rootcause_internals::RawReport;
use crate::markers::{Dynamic, SendSync};
/// An owning iterator over reports in a [`ReportCollection`].
///
/// This iterator consumes a [`ReportCollection`] and yields owned
/// [`Report`] instances. Unlike [`ReportCollectionIter`], this iterator
/// takes ownership of the reports, allowing you to move them out of the
/// collection.
///
/// # Examples
///
/// ```
/// use rootcause::{report, report_collection::ReportCollection};
///
/// let mut collection = ReportCollection::new();
/// collection.push(report!("Error 1").into_cloneable());
/// collection.push(report!("Error 2").into_cloneable());
///
/// // Consume the collection and take ownership of reports
/// for report in collection {
/// println!("Owned report: {}", report);
/// }
/// ```
///
/// [`Report`]: crate::Report
/// [`ReportCollectionIter`]: crate::report_collection::ReportCollectionIter
/// [`ReportCollection`]: crate::report_collection::ReportCollection
#[must_use]
pub struct ReportCollectionIntoIter<
Context: ?Sized + 'static = Dynamic,
ThreadSafety: 'static = SendSync,
> {
/// # Safety
///
/// The following safety invariants are guaranteed to be upheld as long
/// as this struct exists:
///
/// 1. Either the collection must be empty, `C` must either be a type
/// bounded by `Sized`, or C must be `Dynamic`.
/// 2. Either the collection must be empty or `T` must either be
/// `SendSync` or `Local`.
/// 3. If `C` is a `Sized` type: The contexts of the [`RawReport`]s are
/// all of type `C`.
/// 4. All references to these report or any sub-reports are compatible
/// with shared ownership. Specifically there are no references with
/// an assumption that the strong_count is `1`.
/// 5. If `T = SendSync`: All contexts and attachments in the
/// [`RawReport`]s and all sub-reports must be `Send+Sync`.
raw: alloc::vec::IntoIter<RawReport>,
_context: PhantomData<fn(Context) -> Context>,
_thread_safety: PhantomData<ThreadSafety>,
}
impl<C: ?Sized, T> ReportCollectionIntoIter<C, T> {
/// Creates a new [`ReportCollectionIntoIter`] from a vector of raw
/// reports
///
/// # Safety
///
/// The caller must ensure:
///
/// 1. Either the collection must be empty, `C` must either be a type
/// bounded by `Sized`, or C must be `Dynamic`.
/// 2. Either the collection must be empty or `T` must either be
/// `SendSync` or `Local`.
/// 3. If `C` is a `Sized` type: The contexts of the [`RawReport`]s are
/// all of type `C`.
/// 4. All references to these report or any sub-reports are compatible
/// with shared ownership. Specifically there are no references with
/// an assumption that the strong_count is `1`.
/// 5. If `T = SendSync`: All contexts and attachments in the
/// [`RawReport`]s and all sub-reports must be `Send+Sync`.
pub(crate) unsafe fn from_raw(raw: Vec<RawReport>) -> Self {
// SAFETY: We must uphold the safety invariants of this type:
// 1. Guaranteed by the caller
// 2. Guaranteed by the caller
// 3. Guaranteed by the caller
// 4. Guaranteed by the caller
// 5. Guaranteed by the caller
Self {
raw: raw.into_iter(),
_context: PhantomData,
_thread_safety: PhantomData,
}
}
/// Returns a reference to the underlying raw report iterator
pub(crate) fn as_raw(&self) -> &alloc::vec::IntoIter<RawReport> {
// SAFETY: We must uphold the safety invariants of this type:
// 1. Upheld as the type parameters do not change.
// 2. Upheld as the type parameters do not change.
// 3. No mutation occurs here, so the invariants are preserved
// 4. No mutation occurs here, so the invariants are preserved
// 5. Upheld, as it is not possible to turn this into a `Report`, `ReportMut` or
// `ReportRef` with `T=SendSync`, as that would break the safety invariants
// of those types.
&self.raw
}
/// Returns a mutable reference to the underlying raw report iterator
///
/// # Safety
///
/// The caller must ensure:
///
/// 1. If `C` is a `Sized` type: No mutation is performed that would
/// invalidate the invariant that all contexts are of type `C`.
/// 2. No mutation is performed that would invalidate the shared
/// ownership invariant.
/// 3. If `T = SendSync`: No mutation is performed that invalidates the
/// invariant that all inner contexts and attachments are `Send +
/// Sync`.
#[must_use]
pub(crate) unsafe fn as_raw_mut(&mut self) -> &mut alloc::vec::IntoIter<RawReport> {
// SAFETY: We must uphold the safety invariants of this type:
// 1. Upheld as the type parameters do not change.
// 2. Upheld as the type parameters do not change.
// 3. Guaranteed by the caller
// 4. Guaranteed by the caller
// 5. Guaranteed by the caller
&mut self.raw
}
}
}
pub use limit_field_access2::ReportCollectionIntoIter;
impl<C: ?Sized, T> Iterator for ReportCollectionIntoIter<C, T> {
type Item = Report<C, Cloneable, T>;
fn next(&mut self) -> Option<Self::Item> {
// SAFETY: We only remove items, we don't mutate them.
// 1. No mutation that breaks the context-type invariant
// 2. No mutation that breaks the shared-ownership invariant
// 3. No mutation that breaks the Send+Sync invariant for T=SendSync
let raw = unsafe { self.as_raw_mut() };
let item = raw.next()?;
// SAFETY:
// 1. Guaranteed by the invariants of this type.
// 2. `O=Cloneable`, so this is trivially true.
// 3. Guaranteed by the invariants of this type.
// 4. Guaranteed by the invariants of this type.
// 5. `O=Cloneable`, so this is trivially true.
// 6. Guaranteed by the invariants of this type.
// 7. Guaranteed by the invariants of this type.
// 8. Guaranteed by the invariants of this type.
let raw = unsafe { Report::<C, Cloneable, T>::from_raw(item) };
Some(raw)
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.as_raw().size_hint()
}
}
impl<C: ?Sized, T> DoubleEndedIterator for ReportCollectionIntoIter<C, T> {
fn next_back(&mut self) -> Option<Self::Item> {
// SAFETY: We only remove items, we don't mutate them.
// 1. No mutation that breaks the context-type invariant
// 2. No mutation that breaks the shared-ownership invariant
// 3. No mutation that breaks the Send+Sync invariant for T=SendSync
let raw = unsafe { self.as_raw_mut() };
let item = raw.next_back()?;
// SAFETY:
// 1. Guaranteed by the invariants of this type.
// 2. `O=Cloneable`, so this is trivially true.
// 3. Guaranteed by the invariants of this type.
// 4. Guaranteed by the invariants of this type.
// 5. `O=Cloneable`, so this is trivially true.
// 6. Guaranteed by the invariants of this type.
// 7. Guaranteed by the invariants of this type.
// 8. Guaranteed by the invariants of this type.
let raw = unsafe { Report::<C, Cloneable, T>::from_raw(item) };
Some(raw)
}
}
impl<C: ?Sized, T> ExactSizeIterator for ReportCollectionIntoIter<C, T> {
fn len(&self) -> usize {
self.as_raw().len()
}
}
impl<C: ?Sized, T> FusedIterator for ReportCollectionIntoIter<C, T> {}
impl<C: ?Sized, T> Unpin for ReportCollectionIntoIter<C, T> {}