Make Request::send more general.

Removes `+ 'static` constraint from the `impl Read` parameter.
For this, lifetime parameters are added to `Payload` and `SizedReader`.
This commit is contained in:
Frank Steffahn
2020-10-25 21:51:04 +01:00
parent 22e3839340
commit c8cd130770
2 changed files with 13 additions and 13 deletions

View File

@@ -15,16 +15,16 @@ use super::SerdeValue;
/// The different kinds of bodies to send. /// The different kinds of bodies to send.
/// ///
/// *Internal API* /// *Internal API*
pub(crate) enum Payload { pub(crate) enum Payload<'a> {
Empty, Empty,
Text(String, String), Text(String, String),
#[cfg(feature = "json")] #[cfg(feature = "json")]
JSON(SerdeValue), JSON(SerdeValue),
Reader(Box<dyn Read + 'static>), Reader(Box<dyn Read + 'a>),
Bytes(Vec<u8>), Bytes(Vec<u8>),
} }
impl fmt::Debug for Payload { impl fmt::Debug for Payload<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
Payload::Empty => write!(f, "Empty"), Payload::Empty => write!(f, "Empty"),
@@ -37,8 +37,8 @@ impl fmt::Debug for Payload {
} }
} }
impl Default for Payload { impl Default for Payload<'_> {
fn default() -> Payload { fn default() -> Self {
Payload::Empty Payload::Empty
} }
} }
@@ -56,25 +56,25 @@ pub(crate) enum BodySize {
/// Payloads are turned into this type where we can hold both a size and the reader. /// Payloads are turned into this type where we can hold both a size and the reader.
/// ///
/// *Internal API* /// *Internal API*
pub(crate) struct SizedReader { pub(crate) struct SizedReader<'a> {
pub size: BodySize, pub size: BodySize,
pub reader: Box<dyn Read + 'static>, pub reader: Box<dyn Read + 'a>,
} }
impl fmt::Debug for SizedReader { impl fmt::Debug for SizedReader<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "SizedReader[size={:?},reader]", self.size) write!(f, "SizedReader[size={:?},reader]", self.size)
} }
} }
impl SizedReader { impl<'a> SizedReader<'a> {
fn new(size: BodySize, reader: Box<dyn Read + 'static>) -> Self { fn new(size: BodySize, reader: Box<dyn Read + 'a>) -> Self {
SizedReader { size, reader } SizedReader { size, reader }
} }
} }
impl Payload { impl<'a> Payload<'a> {
pub fn into_read(self) -> SizedReader { pub fn into_read(self) -> SizedReader<'a> {
match self { match self {
Payload::Empty => SizedReader::new(BodySize::Empty, Box::new(empty())), Payload::Empty => SizedReader::new(BodySize::Empty, Box::new(empty())),
Payload::Text(text, _charset) => { Payload::Text(text, _charset) => {

View File

@@ -227,7 +227,7 @@ impl Request {
/// .set("Content-Type", "text/plain") /// .set("Content-Type", "text/plain")
/// .send(read); /// .send(read);
/// ``` /// ```
pub fn send(&mut self, reader: impl Read + 'static) -> Response { pub fn send(&mut self, reader: impl Read) -> Response {
self.do_call(Payload::Reader(Box::new(reader))) self.do_call(Payload::Reader(Box::new(reader)))
} }