comparison rust/src/types.rs @ 617:87a78343140e rust

Duration::sum works now
author Matt Johnston <matt@ucc.asn.au>
date Tue, 21 Mar 2017 19:13:49 +0800
parents f153aec221be
children 8136a6b99866
comparison
equal deleted inserted replaced
616:a85c0c9bc1fa 617:87a78343140e
241 fn integrate(&self) -> Duration { 241 fn integrate(&self) -> Duration {
242 let durs = self.on_periods.iter().map(|p| { 242 let durs = self.on_periods.iter().map(|p| {
243 let end = p.end.unwrap_or_else(|| Instant::now()); 243 let end = p.end.unwrap_or_else(|| Instant::now());
244 end - p.start 244 end - p.start
245 }); 245 });
246 // TODO rust 1.16: impl Sum for Duration 246 durs.sum()
247 // durs.sum()
248 durs.fold(Duration::new(0,0), |acc, d| acc + d)
249 } 247 }
250 248
251 fn trim(&mut self) { 249 fn trim(&mut self) {
252 let begin = Instant::now() - self.limit; 250 let begin = Instant::now() - self.limit;
253 251
266 } 264 }
267 } 265 }
268 266
269 /// Takes a stream and returns a stream without errors. 267 /// Takes a stream and returns a stream without errors.
270 pub fn consume_errors<S>(s: S) -> Box<Stream<Item=S::Item, Error=S::Error>> 268 pub fn consume_errors<S>(s: S) -> Box<Stream<Item=S::Item, Error=S::Error>>
271 // XXX not sure why 'static is really necessary here?
272 where 269 where
273 S: Stream+Send+'static, 270 S: Stream+Send+'static,
274 <S as Stream>::Error: std::fmt::Display+Send+'static, 271 <S as Stream>::Error: std::fmt::Display+Send+'static,
275 <S as Stream>::Item: Send+'static, 272 <S as Stream>::Item: Send+'static,
276 { 273 {
277 s.then(|r| { 274 let f = s.then(|r| {
278 match r { 275 match r {
279 Ok(v) => Ok(Some(v)), 276 Ok(v) => Ok(Some(v)),
280 Err(e) => { 277 Err(e) => {
281 debug!("Stream error: {}", e); 278 debug!("Stream error: {}", e);
282 Ok(None) 279 Ok(None)
283 } 280 }
284 } 281 }
285 }) 282 })
286 .filter_map(|p| p).boxed() 283 .filter_map(|p| p);
287 } 284 Box::new(f)
285 }