Skip to content

Commit

Permalink
More body woes
Browse files Browse the repository at this point in the history
  • Loading branch information
algesten committed Jul 6, 2024
1 parent 0a27da6 commit a4233f3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
31 changes: 27 additions & 4 deletions src/body.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
// TODO(martin): Is a better name SendBody to mirror RecvBody? Or even invert
// BodySend, BodyRecv to get alphabetical sorting.

use std::fs::File;
use std::io::Read;

pub struct Body<'a> {
inner: BodyInner<'a>,

Check warning on line 8 in src/body.rs

View workflow job for this annotation

GitHub Actions / build_versions (stable)

fields `inner` and `ended` are never read

Check warning on line 8 in src/body.rs

View workflow job for this annotation

GitHub Actions / build_versions (stable)

fields `inner` and `ended` are never read

Check failure on line 8 in src/body.rs

View workflow job for this annotation

GitHub Actions / Test

fields `inner` and `ended` are never read

Check warning on line 8 in src/body.rs

View workflow job for this annotation

GitHub Actions / build_versions (1.61.0)

field is never read: `inner`

Check warning on line 8 in src/body.rs

View workflow job for this annotation

GitHub Actions / build_versions (beta)

fields `inner` and `ended` are never read

Check failure on line 8 in src/body.rs

View workflow job for this annotation

GitHub Actions / Lint

fields `inner` and `ended` are never read

Check warning on line 8 in src/body.rs

View workflow job for this annotation

GitHub Actions / build_versions (beta)

fields `inner` and `ended` are never read
ended: bool,

Check warning on line 9 in src/body.rs

View workflow job for this annotation

GitHub Actions / build_versions (1.61.0)

field is never read: `ended`
}

impl<'a> Body<'a> {
pub fn empty() -> Body<'static> {
Body {
inner: BodyInner::ByteSlice(&[]),
ended: false,
}
}
}

pub trait IntoBody {
pub trait AsBody {
fn as_body(&self) -> Body;
}

Expand All @@ -24,16 +27,36 @@ pub(crate) enum BodyInner<'a> {
Reader(&'a mut dyn Read),

Check warning on line 27 in src/body.rs

View workflow job for this annotation

GitHub Actions / build_versions (stable)

variant `Reader` is never constructed

Check warning on line 27 in src/body.rs

View workflow job for this annotation

GitHub Actions / build_versions (stable)

variant `Reader` is never constructed

Check failure on line 27 in src/body.rs

View workflow job for this annotation

GitHub Actions / Test

variant `Reader` is never constructed

Check warning on line 27 in src/body.rs

View workflow job for this annotation

GitHub Actions / build_versions (1.61.0)

variant is never constructed: `Reader`

Check warning on line 27 in src/body.rs

View workflow job for this annotation

GitHub Actions / build_versions (beta)

variant `Reader` is never constructed

Check failure on line 27 in src/body.rs

View workflow job for this annotation

GitHub Actions / Lint

variant `Reader` is never constructed

Check warning on line 27 in src/body.rs

View workflow job for this annotation

GitHub Actions / build_versions (beta)

variant `Reader` is never constructed
}

impl IntoBody for &[u8] {
macro_rules! impl_into_body {
($t:ty) => {
impl AsBody for $t {
fn as_body(&self) -> Body {
BodyInner::ByteSlice(self.as_ref()).into()
}
}
};
}

impl AsBody for &mut File {
fn as_body(&self) -> Body {
BodyInner::ByteSlice(self).into()
unreachable!()
}
}

impl_into_body!(&[u8]);
impl_into_body!(&str);
impl_into_body!(String);
impl_into_body!(Vec<u8>);
impl_into_body!(&String);
impl_into_body!(&Vec<u8>);

pub struct RecvBody;

impl<'a> From<BodyInner<'a>> for Body<'a> {
fn from(inner: BodyInner<'a>) -> Self {
Body { inner }
Body {
inner,
ended: false,
}
}
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use std::convert::TryFrom;
use std::sync::atomic::{AtomicBool, Ordering};

use body::{IntoBody, RecvBody};
use body::{AsBody, RecvBody};
/// Re-exported http-crate.
pub use http;

Expand All @@ -26,7 +26,7 @@ pub use agent::{Agent, AgentConfig};
pub use body::Body;
pub use error::Error;

pub fn run(request: &Request<impl IntoBody>) -> Result<Response<RecvBody>, Error> {
pub fn run(request: &Request<impl AsBody>) -> Result<Response<RecvBody>, Error> {
let mut agent = Agent::new_default();
let body = request.body().as_body();
agent.run(request, body)
Expand Down
2 changes: 1 addition & 1 deletion src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::convert::TryFrom;

use http::{HeaderName, HeaderValue, Method, Request, Response, Uri};

use crate::body::{IntoBody, RecvBody};
use crate::body::{AsBody, RecvBody};
use crate::{Agent, Body, Error};

#[derive(Debug)]
Expand Down

0 comments on commit a4233f3

Please sign in to comment.