Skip to content

Commit

Permalink
ci: clippy (#143)
Browse files Browse the repository at this point in the history
* fix: clippy

* bench: response with json
  • Loading branch information
fundon authored Jun 23, 2024
1 parent 6cfd8fd commit d1a1009
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 2 deletions.
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)
});
}

0 comments on commit d1a1009

Please sign in to comment.