From 38204f5fbacb4ef5dc30a407609bd3ae50aef717 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Tue, 29 Jun 2021 20:06:10 +0200 Subject: [PATCH] time: fix Timeout size_hint (#3902) --- tokio-stream/src/stream_ext/timeout.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tokio-stream/src/stream_ext/timeout.rs b/tokio-stream/src/stream_ext/timeout.rs index de17dc009..98d7cd5c0 100644 --- a/tokio-stream/src/stream_ext/timeout.rs +++ b/tokio-stream/src/stream_ext/timeout.rs @@ -69,7 +69,18 @@ impl Stream for Timeout { } fn size_hint(&self) -> (usize, Option) { - self.stream.size_hint() + let (lower, upper) = self.stream.size_hint(); + + // The timeout stream may insert an error before and after each message + // from the underlying stream, but no more than one error between each + // message. Hence the upper bound is computed as 2x+1. + + // Using a helper function to enable use of question mark operator. + fn twice_plus_one(value: Option) -> Option { + value?.checked_mul(2)?.checked_add(1) + } + + (lower, twice_plus_one(upper)) } }