Skip to content

Commit

Permalink
Support for Hyper v1/Axum 0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
abdolence committed Dec 8, 2023
1 parent a81bf93 commit 6f3d358
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 18 deletions.
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ name = "axum_streams"
path = "src/lib.rs"

[dependencies]
axum = { version = "0.6" }
axum = { version = "0.7" }
bytes = "1"
futures-util = { version = "0.3", default-features = false, features = ["alloc"] }
http = "0.2"
http = "1"
http-body = "1"
mime = "0.3"
tokio = "1"
serde = { version = "1", features = ["serde_derive"], optional = true }
Expand All @@ -44,10 +45,10 @@ text = []

[dev-dependencies]
futures = "0.3"
hyper = "0.14"
hyper = "1"
reqwest = { version = "0.11", default-features = false, features = ["json", "stream", "multipart"] }
tower = { version = "0.4", default-features = false, features = ["util", "make"] }
tower-http = { version = "0.4", features = ["util", "map-response-body"] }
tower-http = { version = "0.5", features = ["util", "map-response-body"] }
tower-layer = "0.3"
tower-service = "0.3"
tokio = { version = "1", features = ["full"] }
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ and want to avoid huge memory allocation.
Cargo.toml:
```toml
[dependencies]
axum-streams = { version = "0.10", features=["json", "csv", "protobuf", "text"] }
axum-streams = { version = "0.11", features=["json", "csv", "protobuf", "text"] }
```

## Compatibility matrix

| axum | axum-streams |
|------|--------------|
| 0.7 | v0.11 |
| 0.6 | v0.9-v0.10 |
| 0.5 | 0.7 |

Expand Down
17 changes: 6 additions & 11 deletions src/stream_body_as.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use crate::stream_format::StreamingFormat;
use axum::body::HttpBody;
use axum::response::{IntoResponse, Response};
use futures::Stream;
use futures_util::stream::BoxStream;
use http::HeaderMap;
use std::fmt::Formatter;
use std::pin::Pin;
use std::task::{Context, Poll};
use axum::body::{Body, HttpBody};
use http_body::Frame;

pub struct StreamBodyAs<'a> {
stream: BoxStream<'a, Result<axum::body::Bytes, axum::Error>>,
stream: BoxStream<'a, Result<Frame<axum::body::Bytes>, axum::Error>>,
trailers: Option<HeaderMap>,
}

Expand Down Expand Up @@ -46,7 +47,7 @@ impl IntoResponse for StreamBodyAs<'static> {
HeaderMap::new()
};

let mut response = Response::new(axum::body::boxed(self));
let mut response: Response<Body> = Response::new(Body::new(self));
*response.headers_mut() = headers;
response
}
Expand All @@ -56,17 +57,11 @@ impl<'a> HttpBody for StreamBodyAs<'a> {
type Data = axum::body::Bytes;
type Error = axum::Error;

fn poll_data(
fn poll_frame(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
Pin::new(&mut self.stream).poll_next(cx)
}

fn poll_trailers(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Result<Option<HeaderMap>, Self::Error>> {
Poll::Ready(Ok(self.trailers.clone()))
}
}
2 changes: 1 addition & 1 deletion src/stream_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub trait StreamingFormat<T> {
fn to_bytes_stream<'a, 'b>(
&'a self,
stream: BoxStream<'b, T>,
) -> BoxStream<'b, Result<axum::body::Bytes, axum::Error>>;
) -> BoxStream<'b, Result<http_body::Frame<axum::body::Bytes>, axum::Error>>;

fn http_response_trailers(&self) -> Option<HeaderMap>;
}
3 changes: 2 additions & 1 deletion src/text_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use futures::Stream;
use futures_util::stream::BoxStream;
use futures_util::StreamExt;
use http::HeaderMap;
use http_body::Frame;

pub struct TextStreamFormat;

Expand All @@ -16,7 +17,7 @@ impl StreamingFormat<String> for TextStreamFormat {
fn to_bytes_stream<'a, 'b>(
&'a self,
stream: BoxStream<'b, String>,
) -> BoxStream<'b, Result<axum::body::Bytes, axum::Error>> {
) -> BoxStream<'b, Result<Frame<axum::body::Bytes>, axum::Error>> {
fn write_text_record(obj: String) -> Result<Vec<u8>, axum::Error> {
let obj_vec = obj.as_bytes().to_vec();
Ok(obj_vec)
Expand Down

0 comments on commit 6f3d358

Please sign in to comment.