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

Remove unnecessary generic parameter from Set{Request,Response}HeaderLayer #148

Merged
merged 1 commit into from
Nov 15, 2021
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
2 changes: 1 addition & 1 deletion examples/tonic-key-value-store/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ async fn make_client(
// Decompress response bodies
.layer(DecompressionLayer::new())
// Set a `User-Agent` header
.layer(SetRequestHeaderLayer::<_, Request<BoxBody>>::overriding(
.layer(SetRequestHeaderLayer::overriding(
header::USER_AGENT,
HeaderValue::from_static("tonic-key-value-store"),
))
Expand Down
4 changes: 2 additions & 2 deletions examples/warp-key-value-store/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bytes::Bytes;
use hyper::{
body::HttpBody,
header::{self, HeaderValue},
Body, Request, Response, Server, StatusCode,
Body, Response, Server, StatusCode,
};
use std::collections::HashMap;
use std::net::SocketAddr;
Expand Down Expand Up @@ -89,7 +89,7 @@ async fn serve_forever(listener: TcpListener) -> Result<(), hyper::Error> {
content_length_from_response,
))
// Set a `Content-Type` if there isn't one already.
.layer(SetResponseHeaderLayer::<_, Request<Body>>::if_not_present(
.layer(SetResponseHeaderLayer::if_not_present(
header::CONTENT_TYPE,
HeaderValue::from_static("application/octet-stream"),
))
Expand Down
3 changes: 3 additions & 0 deletions tower-http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `ServeDir` and `ServeFile`: Ability to serve precompressed files ([#156])
- `Trace`: Add `DefaultMakeSpan::level` to make log level of tracing spans easily configurable ([#124])
- `SetRequestHeaderLayer`, `SetResponseHeaderLayer`: Remove unnecessary generic parameter ([#148])

This removes the need (and possibility) to specify a body type for these layers.

[#124]: https://github.com/tower-rs/tower-http/pull/124
[#156]: https://github.com/tower-rs/tower-http/pull/156
Expand Down
2 changes: 1 addition & 1 deletion tower-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
//! StatusInRangeAsFailures::new(400..=599).into_make_classifier()
//! ))
//! // Set a `User-Agent` header on all requests.
//! .layer(SetRequestHeaderLayer::<_, Body>::overriding(
//! .layer(SetRequestHeaderLayer::overriding(
//! USER_AGENT,
//! HeaderValue::from_static("tower-http demo")
//! ))
Expand Down
23 changes: 6 additions & 17 deletions tower-http/src/set_header/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@
//! .layer(
//! // Layer that sets `User-Agent: my very cool app` on requests.
//! //
//! // We have to add `::<_, Body>` since Rust cannot infer the body type when
//! // we don't use a closure to produce the header value.
//! //
//! // `if_not_present` will only insert the header if it does not already
//! // have a value.
//! SetRequestHeaderLayer::<_, Body>::if_not_present(
//! SetRequestHeaderLayer::if_not_present(
//! header::USER_AGENT,
//! HeaderValue::from_static("my very cool app"),
//! )
Expand Down Expand Up @@ -87,7 +84,6 @@

use super::{InsertHeaderMode, MakeHeaderValue};
use http::{header::HeaderName, Request};
use std::marker::PhantomData;
use std::{
fmt,
task::{Context, Poll},
Expand All @@ -98,15 +94,13 @@ use tower_service::Service;
/// Layer that applies [`SetRequestHeader`] which adds a request header.
///
/// See [`SetRequestHeader`] for more details.
pub struct SetRequestHeaderLayer<M, T> {
pub struct SetRequestHeaderLayer<M> {
header_name: HeaderName,
make: M,
mode: InsertHeaderMode,
// Covariant over T, no dropping of T
_marker: PhantomData<fn() -> T>,
}

impl<M, T> fmt::Debug for SetRequestHeaderLayer<M, T> {
impl<M> fmt::Debug for SetRequestHeaderLayer<M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SetRequestHeaderLayer")
.field("header_name", &self.header_name)
Expand All @@ -116,10 +110,7 @@ impl<M, T> fmt::Debug for SetRequestHeaderLayer<M, T> {
}
}

impl<M, T> SetRequestHeaderLayer<M, T>
where
M: MakeHeaderValue<T>,
{
impl<M> SetRequestHeaderLayer<M> {
/// Create a new [`SetRequestHeaderLayer`].
///
/// If a previous value exists for the same header, it is removed and replaced with the new
Expand Down Expand Up @@ -148,12 +139,11 @@ where
make,
header_name,
mode,
_marker: PhantomData,
}
}
}

impl<T, S, M> Layer<S> for SetRequestHeaderLayer<M, T>
impl<S, M> Layer<S> for SetRequestHeaderLayer<M>
where
M: Clone,
{
Expand All @@ -169,7 +159,7 @@ where
}
}

impl<M, T> Clone for SetRequestHeaderLayer<M, T>
impl<M> Clone for SetRequestHeaderLayer<M>
where
M: Clone,
{
Expand All @@ -178,7 +168,6 @@ where
make: self.make.clone(),
header_name: self.header_name.clone(),
mode: self.mode,
_marker: PhantomData,
}
}
}
Expand Down
24 changes: 7 additions & 17 deletions tower-http/src/set_header/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@
//! .layer(
//! // Layer that sets `Content-Type: text/html` on responses.
//! //
//! // We have to add `::<_, Body>` since Rust cannot infer the body type when
//! // we don't use a closure to produce the header value.
//! //
//! // `if_not_present` will only insert the header if it does not already
//! // have a value.
//! SetResponseHeaderLayer::<_, Body>::if_not_present(
//! SetResponseHeaderLayer::if_not_present(
//! header::CONTENT_TYPE,
//! HeaderValue::from_static("text/html"),
//! )
Expand Down Expand Up @@ -101,25 +98,23 @@ use http::{header::HeaderName, Response};
use pin_project::pin_project;
use std::{
fmt,
future::Future,
pin::Pin,
task::{Context, Poll},
};
use std::{future::Future, marker::PhantomData};
use tower_layer::Layer;
use tower_service::Service;

/// Layer that applies [`SetResponseHeader`] which adds a response header.
///
/// See [`SetResponseHeader`] for more details.
pub struct SetResponseHeaderLayer<M, T> {
pub struct SetResponseHeaderLayer<M> {
header_name: HeaderName,
make: M,
mode: InsertHeaderMode,
// Covariant over T, no dropping of T
_marker: PhantomData<fn() -> T>,
}

impl<M, T> fmt::Debug for SetResponseHeaderLayer<M, T> {
impl<M> fmt::Debug for SetResponseHeaderLayer<M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SetResponseHeaderLayer")
.field("header_name", &self.header_name)
Expand All @@ -129,10 +124,7 @@ impl<M, T> fmt::Debug for SetResponseHeaderLayer<M, T> {
}
}

impl<M, T> SetResponseHeaderLayer<M, T>
where
M: MakeHeaderValue<T>,
{
impl<M> SetResponseHeaderLayer<M> {
/// Create a new [`SetResponseHeaderLayer`].
///
/// If a previous value exists for the same header, it is removed and replaced with the new
Expand Down Expand Up @@ -161,12 +153,11 @@ where
make,
header_name,
mode,
_marker: PhantomData,
}
}
}

impl<T, S, M> Layer<S> for SetResponseHeaderLayer<M, T>
impl<S, M> Layer<S> for SetResponseHeaderLayer<M>
where
M: Clone,
{
Expand All @@ -182,7 +173,7 @@ where
}
}

impl<M, T> Clone for SetResponseHeaderLayer<M, T>
impl<M> Clone for SetResponseHeaderLayer<M>
where
M: Clone,
{
Expand All @@ -191,7 +182,6 @@ where
make: self.make.clone(),
header_name: self.header_name.clone(),
mode: self.mode,
_marker: PhantomData,
}
}
}
Expand Down