Skip to content

Commit

Permalink
mendes: implement FromContext for Body
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Jul 22, 2024
1 parent d1fd4ed commit 86b9f17
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions mendes/src/body.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::future::Future;
use std::pin::Pin;
use std::str::FromStr;
use std::sync::Arc;
use std::task::ready;
use std::task::Poll;
use std::{io, mem, str};
Expand All @@ -14,6 +15,7 @@ use async_compression::tokio::bufread::GzipEncoder;
use bytes::{Buf, Bytes, BytesMut};
#[cfg(any(feature = "brotli", feature = "deflate", feature = "gzip"))]
use http::header::{ACCEPT_ENCODING, CONTENT_ENCODING};
use http::request::Parts;
#[cfg(any(feature = "brotli", feature = "deflate", feature = "gzip"))]
use http::HeaderMap;
#[cfg(any(feature = "brotli", feature = "deflate", feature = "gzip"))]
Expand All @@ -25,6 +27,8 @@ use tokio::io::{AsyncBufRead, AsyncRead, ReadBuf};
#[cfg(any(feature = "brotli", feature = "deflate", feature = "gzip"))]
use tokio_util::io::poll_read_buf;

use crate::application::{Application, FromContext, PathState};

#[pin_project]
pub struct Body {
#[pin]
Expand Down Expand Up @@ -64,6 +68,20 @@ impl Body {
}
}

impl<'a, A: Application<RequestBody = Body>> FromContext<'a, A> for Body {
fn from_context(
_: &'a Arc<A>,
_: &'a Parts,
_: &mut PathState,
body: &mut Option<Body>,
) -> Result<Self, A::Error> {
match body.take() {
Some(body) => Ok(body),
None => panic!("attempted to retrieve body twice"),
}
}
}

impl http_body::Body for Body {
type Data = Bytes;
type Error = io::Error;
Expand Down

0 comments on commit 86b9f17

Please sign in to comment.