@@ -16,13 +16,14 @@ use sync_wrapper::SyncWrapper;
1616/// This type is `Sync` via [`SyncWrapper`], allowing it to be sent across thread boundaries
1717/// as required by the upload service.
1818#[ derive( Debug ) ]
19- pub struct CountingStream < S > {
19+ pub struct BoundedStream < S > {
20+ pub lower_bound : usize ,
21+ pub upper_bound : usize ,
2022 inner : Option < SyncWrapper < S > > ,
21- expected_length : Option < usize > ,
2223 byte_counter : ByteCounter ,
2324}
2425
25- /// A shared counter that can be read after the [`CountingStream `] has been moved.
26+ /// A shared counter that can be read after the [`BoundedStream `] has been moved.
2627#[ derive( Clone , Debug ) ]
2728pub struct ByteCounter ( Arc < AtomicUsize > ) ;
2829
@@ -40,28 +41,24 @@ impl ByteCounter {
4041 }
4142}
4243
43- impl < S > CountingStream < S > {
44- /// Creates a new [`CountingStream `] wrapping the given stream with the expected total length.
45- pub fn new ( stream : S , expected_length : Option < usize > ) -> Self {
44+ impl < S > BoundedStream < S > {
45+ /// Creates a new [`BoundedStream `] wrapping the given stream with the expected total length.
46+ pub fn new ( stream : S , lower_bound : usize , upper_bound : usize ) -> Self {
4647 Self {
4748 inner : Some ( SyncWrapper :: new ( stream) ) ,
48- expected_length,
49+ lower_bound,
50+ upper_bound,
4951 byte_counter : ByteCounter :: new ( ) ,
5052 }
5153 }
5254
53- /// Returns the expected total length of the stream.
54- pub fn expected_length ( & self ) -> Option < usize > {
55- self . expected_length
56- }
57-
5855 /// Returns a shared handle to read the byte count after the stream is consumed.
5956 pub fn byte_counter ( & self ) -> ByteCounter {
6057 self . byte_counter . clone ( )
6158 }
6259}
6360
64- impl < S , E > Stream for CountingStream < S >
61+ impl < S , E > Stream for BoundedStream < S >
6562where
6663 S : Stream < Item = Result < Bytes , E > > + Send + Unpin ,
6764 E : Into < io:: Error > ,
@@ -78,15 +75,13 @@ where
7875 match inner. poll_next ( cx) {
7976 Poll :: Ready ( Some ( Ok ( bytes) ) ) => {
8077 let bytes_received = this. byte_counter . add ( bytes. len ( ) ) ;
81- if let Some ( expected_length) = this. expected_length
82- && bytes_received > expected_length
83- {
78+ if bytes_received > this. upper_bound {
8479 this. inner = None ;
8580 Poll :: Ready ( Some ( Err ( io:: Error :: new (
8681 io:: ErrorKind :: FileTooLarge ,
8782 format ! (
88- "stream exceeded expected length : received {} > {}" ,
89- bytes_received, expected_length
83+ "stream exceeded upper bound : received {} > {}" ,
84+ bytes_received, this . upper_bound
9085 ) ,
9186 ) ) ) )
9287 } else {
@@ -96,15 +91,13 @@ where
9691 Poll :: Ready ( Some ( Err ( e) ) ) => Poll :: Ready ( Some ( Err ( e. into ( ) ) ) ) ,
9792 Poll :: Ready ( None ) => {
9893 let bytes_received = this. byte_counter . get ( ) ;
99- if let Some ( expected_length) = this. expected_length
100- && bytes_received < expected_length
101- {
94+ if bytes_received < this. lower_bound {
10295 this. inner = None ;
10396 Poll :: Ready ( Some ( Err ( io:: Error :: new (
10497 io:: ErrorKind :: UnexpectedEof ,
10598 format ! (
106- "stream shorter than expected length : received {} < {}" ,
107- bytes_received, expected_length
99+ "stream shorter than lower bound : received {} < {}" ,
100+ bytes_received, this . lower_bound
108101 ) ,
109102 ) ) ) )
110103 } else {
0 commit comments