chiark / gitweb /
ReadLimitedError: Make it generic over the error type
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 1 Feb 2025 11:19:55 +0000 (11:19 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 1 Feb 2025 20:14:58 +0000 (20:14 +0000)
We're going to have reqwest for the client and hyper for the server.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
server/sweb.rs
src/utils.rs

index dc962c17a4b17865312c75baef9cafe9b4346751..69afc903b0b6e50008919b627773865a663a8908 100644 (file)
@@ -91,7 +91,7 @@ pub async fn handle(
     ).await {
       Ok(all) => all,
       Err(ReadLimitedError::Truncated { sofar,.. }) => sofar,
-      Err(ReadLimitedError::Hyper(e)) => throw!(e),
+      Err(ReadLimitedError::Http(e)) => throw!(e),
     };
 
     let boundary_finder = memmem::Finder::new(&boundary);
index 823e2027cbd8cd3b7fb42bb12608a67753861a80..5dbb61610ed49ae7008912e0a534d4b5adf2f53b 100644 (file)
@@ -17,22 +17,22 @@ pub impl<T,E> Result<T,E> where AE: From<E> {
 }
 
 #[derive(Error,Debug)]
-pub enum ReadLimitedError {
+pub enum ReadLimitedError<H: std::error::Error + 'static> {
   #[error("maximum size {limit} exceeded")]
   Truncated { sofar: Box<[u8]>, limit: usize },
 
   #[error("HTTP error {0}")]
-  Hyper(#[from] hyper::Error),
+  Http(#[from] H),
 }
 
-impl ReadLimitedError {
+impl ReadLimitedError<hyper::Error> {
   pub fn discard_data(&mut self) { match self {
     ReadLimitedError::Truncated { sofar,.. } => { mem::take(sofar); },
     _ => { },
   } }
 }
 #[ext]
-pub impl<T> Result<T,ReadLimitedError> {
+pub impl<T> Result<T,ReadLimitedError<hyper::Error>> {
   fn discard_data(self) -> Self {
     self.map_err(|mut e| { e.discard_data(); e })
   }
@@ -52,12 +52,13 @@ pub impl io::Error {
   }
 }
 
-#[throws(ReadLimitedError)]
-pub async fn read_limited_bytes<S>(limit: usize, initial: Box<[u8]>,
+#[throws(ReadLimitedError<H>)]
+pub async fn read_limited_bytes<S, H>(limit: usize, initial: Box<[u8]>,
                                    capacity: usize,
                                    stream: &mut S) -> Box<[u8]>
-where S: futures::Stream<Item=Result<hyper::body::Bytes,hyper::Error>>
+where S: futures::Stream<Item=Result<hyper::body::Bytes, H>>
          + Debug + Unpin,
+      H: std::error::Error + 'static,
       // we also require that the Stream is cancellation-safe
 {
   let mut accum = initial.into_vec();