Skip to content

Commit

Permalink
get rid of pin-project
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Dec 1, 2024
1 parent fd4b942 commit 622f529
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/kitsune-http-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ hyper-rustls = { version = "0.27.3", default-features = false, features = [
] }
kitsune-core.workspace = true
kitsune-type.workspace = true
pin-project = "1.1.7"
pin-project-lite.workspace = true
serde.workspace = true
simdutf8.workspace = true
sonic-rs.workspace = true
Expand Down
58 changes: 41 additions & 17 deletions crates/kitsune-http-client/src/body.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use bytes::Bytes;
use futures_util::{stream::BoxStream, StreamExt, TryStream, TryStreamExt};
use futures_util::{StreamExt, TryStream, TryStreamExt};
use http_body::Frame;
use http_body_util::StreamBody;
use pin_project::pin_project;
use std::{
borrow::Cow,
fmt::{self, Debug},
Expand All @@ -11,19 +10,40 @@ use std::{
};
use tower::BoxError;

/// Body on a budget
#[derive(Default)]
#[pin_project(project = BodyProj)]
pub enum Body {
/// Empty body
#[default]
Empty,
mod body_def {
#![allow(missing_docs)]

use super::{BoxError, Bytes, Frame, StreamBody};
use futures_util::stream::BoxStream;
use pin_project_lite::pin_project;

pin_project! {
#[project = BodyProj]
// Body on a budget
pub enum Body {
// Empty body
Empty,

// Body consisting of a single chunk
Full { data: Option<Bytes> },

// Body backed by a `StreamBody`
Stream {
#[pin]
stream: StreamBody<BoxStream<'static, Result<Frame<Bytes>, BoxError>>>
},
}
}
}

/// Body consisting of a single chunk
Full(Option<Bytes>),
pub use self::body_def::Body;
use self::body_def::BodyProj;

/// Body backed by a `StreamBody`
Stream(#[pin] StreamBody<BoxStream<'static, Result<Frame<Bytes>, BoxError>>>),
impl Default for Body {
#[inline]
fn default() -> Self {
Self::empty()
}
}

impl Body {
Expand All @@ -40,7 +60,9 @@ impl Body {
where
D: Into<Bytes>,
{
Self::Full(Some(data.into()))
Self::Full {
data: Some(data.into()),
}
}

/// Stream body
Expand All @@ -56,7 +78,9 @@ impl Body {
.map_err(Into::into)
.boxed();

Self::Stream(StreamBody::new(stream))
Self::Stream {
stream: StreamBody::new(stream),
}

Check warning on line 83 in crates/kitsune-http-client/src/body.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-http-client/src/body.rs#L81-L83

Added lines #L81 - L83 were not covered by tests
}
}

Expand Down Expand Up @@ -107,8 +131,8 @@ impl http_body::Body for Body {
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
match self.project() {
BodyProj::Empty => Poll::Ready(None),
BodyProj::Full(data) => Poll::Ready(data.take().map(|data| Ok(Frame::data(data)))),
BodyProj::Stream(stream) => stream.poll_frame(cx),
BodyProj::Full { data } => Poll::Ready(data.take().map(|data| Ok(Frame::data(data)))),
BodyProj::Stream { stream } => stream.poll_frame(cx),

Check warning on line 135 in crates/kitsune-http-client/src/body.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-http-client/src/body.rs#L135

Added line #L135 was not covered by tests
}
}
}

0 comments on commit 622f529

Please sign in to comment.