chiark / gitweb /
read_limited_bytes: take a `capacity` argument
[hippotat.git] / src / utils.rs
index 4b345d0c4cf8cbd8993a5fd4ce778477be5d0662..c210a7d3ce2ab7a0801653cbfb40060f9465058b 100644 (file)
@@ -40,12 +40,15 @@ impl<T> Result<T,ReadLimitedError> {
 
 #[throws(ReadLimitedError)]
 pub async fn read_limited_bytes<S>(limit: usize, initial: Box<[u8]>,
+                                   capacity: usize,
                                    stream: &mut S) -> Box<[u8]>
 where S: futures::Stream<Item=Result<hyper::body::Bytes,hyper::Error>>
          + Debug + Unpin,
       // we also require that the Stream is cancellation-safe
 {
   let mut accum = initial.into_vec();
+  let capacity = min(limit, capacity);
+  if capacity > accum.len() { accum.reserve(capacity - accum.len()); }
   while let Some(item) = stream.next().await {
     let b = item?;
     accum.extend(b);