Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: clippy #143

Merged
merged 2 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions viz-core/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,16 @@ pub trait ResponseExt: private::Sealed + Sized {
where
T: serde::Serialize,
{
serde_json::to_vec(&body)
.map(|buf| Self::with(Full::new(buf.into()), mime::APPLICATION_JSON.as_ref()))
use bytes::{BufMut, BytesMut};

let mut buf = BytesMut::with_capacity(128).writer();
serde_json::to_writer(&mut buf, &body)
.map(|()| {
Response::with(
Full::new(buf.into_inner().freeze()),
mime::APPLICATION_JSON.as_ref(),
)
})
.map_err(crate::types::PayloadError::Json)
}

Expand Down
1 change: 1 addition & 0 deletions viz-core/tests/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![allow(clippy::unused_async)]
#![allow(clippy::similar_names)]
#![allow(clippy::wildcard_imports)]
#![allow(dependency_on_unit_never_type_fallback)]

use http_body_util::Full;
use viz_core::handler::CatchError;
Expand Down
107 changes: 107 additions & 0 deletions viz-core/tests/response.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#![feature(test)]

extern crate test;
use test::Bencher;

use futures_util::{stream, Stream, StreamExt};
use headers::{ContentDisposition, ContentType, HeaderMapExt};
use http_body_util::{BodyExt, Full};
Expand Down Expand Up @@ -129,3 +134,105 @@ async fn response_ext() -> Result<()> {
fn response_ext_panic() {
Response::redirect_with_status("/oauth", StatusCode::OK);
}

#[cfg(all(feature = "json", not(miri)))]
#[bench]
fn response_json_with_to_vec(b: &mut Bencher) {
use mime;
use viz_core::types::PayloadError;

#[derive(Serialize)]
struct Message {
message: &'static str,
}

b.iter(|| {
let body = Message {
message: "Hello, World!",
};

let body = serde_json::to_vec(&body).map_err(PayloadError::Json)?;
Ok::<Response, Error>(Response::with(
Full::from(body),
mime::APPLICATION_JSON.as_ref(),
))
});
}

#[cfg(all(feature = "json", not(miri)))]
#[bench]
fn response_json_with_map_to_vec(b: &mut Bencher) {
use mime;
use viz_core::types::PayloadError;

#[derive(Serialize)]
struct Message {
message: &'static str,
}

b.iter(|| {
let body = Message {
message: "Hello, World!",
};

serde_json::to_vec(&body)
.map(|buf| Response::with(Full::new(buf.into()), mime::APPLICATION_JSON.as_ref()))
.map_err(PayloadError::Json)
});
}

#[cfg(all(feature = "json", not(miri)))]
#[bench]
fn response_json_with_to_writer(b: &mut Bencher) {
use bytes::{BufMut, BytesMut};
use mime;
use viz_core::types::PayloadError;

#[derive(Serialize)]
struct Message {
message: &'static str,
}

b.iter(|| {
let body = Message {
message: "Hello, World!",
};

let mut buf = BytesMut::with_capacity(128).writer();
let () = serde_json::to_writer(&mut buf, &body).map_err(PayloadError::Json)?;

Ok::<Response, Error>(Response::with(
Full::new(buf.into_inner().freeze()),
mime::APPLICATION_JSON.as_ref(),
))
});
}

#[cfg(all(feature = "json", not(miri)))]
#[bench]
fn response_json_with_map_to_writer(b: &mut Bencher) {
use bytes::{BufMut, BytesMut};
use mime;
use viz_core::types::PayloadError;

#[derive(Serialize)]
struct Message {
message: &'static str,
}

b.iter(|| {
let body = Message {
message: "Hello, World!",
};

let mut buf = BytesMut::with_capacity(128).writer();
serde_json::to_writer(&mut buf, &body)
.map(|()| {
Response::with(
Full::new(buf.into_inner().freeze()),
mime::APPLICATION_JSON.as_ref(),
)
})
.map_err(PayloadError::Json)
});
}
Loading