@@ -137,42 +137,29 @@ pub enum EventArray {
137
137
}
138
138
139
139
impl EventArray {
140
- /// Call the given update function over each `LogEvent` in this array.
141
- pub fn for_each_log ( & mut self , update : impl FnMut ( & mut LogEvent ) ) {
142
- if let Self :: Logs ( logs) = self {
143
- logs. iter_mut ( ) . for_each ( update) ;
144
- }
145
- }
146
-
147
- /// Call the given update function over each `Metric` in this array.
148
- pub fn for_each_metric ( & mut self , update : impl FnMut ( & mut Metric ) ) {
149
- if let Self :: Metrics ( metrics) = self {
150
- metrics. iter_mut ( ) . for_each ( update) ;
151
- }
152
- }
153
-
154
- /// Run the given update function over each `Trace` in this array.
155
- pub fn for_each_trace ( & mut self , update : impl FnMut ( & mut TraceEvent ) ) {
156
- if let Self :: Traces ( traces) = self {
157
- traces. iter_mut ( ) . for_each ( update) ;
140
+ /// Iterate over references to this array's events.
141
+ pub fn iter_events ( & self ) -> impl Iterator < Item = EventRef > {
142
+ match self {
143
+ Self :: Logs ( array) => EventArrayIter :: Logs ( array. iter ( ) ) ,
144
+ Self :: Metrics ( array) => EventArrayIter :: Metrics ( array. iter ( ) ) ,
145
+ Self :: Traces ( array) => EventArrayIter :: Traces ( array. iter ( ) ) ,
158
146
}
159
147
}
160
148
161
- /// Call the given update function over each event in this array.
162
- pub fn for_each_event ( & mut self , mut update : impl FnMut ( EventMutRef < ' _ > ) ) {
149
+ /// Iterate over mutable references to this array's events .
150
+ pub fn iter_events_mut ( & mut self ) -> impl Iterator < Item = EventMutRef > {
163
151
match self {
164
- Self :: Logs ( array) => array. iter_mut ( ) . for_each ( |log| update ( log . into ( ) ) ) ,
165
- Self :: Metrics ( array) => array. iter_mut ( ) . for_each ( |metric| update ( metric . into ( ) ) ) ,
166
- Self :: Traces ( array) => array. iter_mut ( ) . for_each ( |trace| update ( trace . into ( ) ) ) ,
152
+ Self :: Logs ( array) => EventArrayIterMut :: Logs ( array. iter_mut ( ) ) ,
153
+ Self :: Metrics ( array) => EventArrayIterMut :: Metrics ( array. iter_mut ( ) ) ,
154
+ Self :: Traces ( array) => EventArrayIterMut :: Traces ( array. iter_mut ( ) ) ,
167
155
}
168
156
}
169
157
170
- /// Iterate over this array's events .
171
- pub fn iter_events ( & self ) -> impl Iterator < Item = EventRef > {
158
+ /// Iterate over references to the logs in this array.
159
+ pub fn iter_logs_mut ( & mut self ) -> impl Iterator < Item = & mut LogEvent > {
172
160
match self {
173
- Self :: Logs ( array) => EventArrayIter :: Logs ( array. iter ( ) ) ,
174
- Self :: Metrics ( array) => EventArrayIter :: Metrics ( array. iter ( ) ) ,
175
- Self :: Traces ( array) => EventArrayIter :: Traces ( array. iter ( ) ) ,
161
+ Self :: Logs ( array) => TypedArrayIterMut ( Some ( array. iter_mut ( ) ) ) ,
162
+ _ => TypedArrayIterMut ( None ) ,
176
163
}
177
164
}
178
165
}
@@ -348,6 +335,29 @@ impl<'a> Iterator for EventArrayIter<'a> {
348
335
}
349
336
}
350
337
338
+ /// The iterator type for `EventArray::iter_events_mut`.
339
+ #[ derive( Debug ) ]
340
+ pub enum EventArrayIterMut < ' a > {
341
+ /// An iterator over type `LogEvent`.
342
+ Logs ( slice:: IterMut < ' a , LogEvent > ) ,
343
+ /// An iterator over type `Metric`.
344
+ Metrics ( slice:: IterMut < ' a , Metric > ) ,
345
+ /// An iterator over type `Trace`.
346
+ Traces ( slice:: IterMut < ' a , TraceEvent > ) ,
347
+ }
348
+
349
+ impl < ' a > Iterator for EventArrayIterMut < ' a > {
350
+ type Item = EventMutRef < ' a > ;
351
+
352
+ fn next ( & mut self ) -> Option < Self :: Item > {
353
+ match self {
354
+ Self :: Logs ( i) => i. next ( ) . map ( EventMutRef :: from) ,
355
+ Self :: Metrics ( i) => i. next ( ) . map ( EventMutRef :: from) ,
356
+ Self :: Traces ( i) => i. next ( ) . map ( EventMutRef :: from) ,
357
+ }
358
+ }
359
+ }
360
+
351
361
/// The iterator type for `EventArray::into_events`.
352
362
#[ derive( Debug ) ]
353
363
pub enum EventArrayIntoIter {
@@ -371,6 +381,15 @@ impl Iterator for EventArrayIntoIter {
371
381
}
372
382
}
373
383
384
+ struct TypedArrayIterMut < ' a , T > ( Option < slice:: IterMut < ' a , T > > ) ;
385
+
386
+ impl < ' a , T > Iterator for TypedArrayIterMut < ' a , T > {
387
+ type Item = & ' a mut T ;
388
+ fn next ( & mut self ) -> Option < Self :: Item > {
389
+ self . 0 . as_mut ( ) . and_then ( Iterator :: next)
390
+ }
391
+ }
392
+
374
393
/// Intermediate buffer for conversion of a sequence of individual
375
394
/// `Event`s into a sequence of `EventArray`s by coalescing contiguous
376
395
/// events of the same type into one array. This is used by
0 commit comments