Make combinators implement Debug in more cases (#552)

Co-authored-by: Eliza Weisman <eliza@buoyant.io>
This commit is contained in:
David Pedersen
2021-01-27 23:47:04 +01:00
committed by GitHub
parent 02b6731350
commit 0226ef0f4c
9 changed files with 99 additions and 12 deletions

View File

@@ -88,7 +88,7 @@ where
impl<F> fmt::Debug for LayerFn<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("LayerFn")
.field("f", &format_args!("<{}>", std::any::type_name::<F>()))
.field("f", &format_args!("{}", std::any::type_name::<F>()))
.finish()
}
}
@@ -107,7 +107,7 @@ mod tests {
let _svc = layer.layer("foo");
assert_eq!(
"LayerFn { f: <tower_layer::layer_fn::tests::layer_fn_has_useful_debug_impl::{{closure}}> }".to_string(),
"LayerFn { f: tower_layer::layer_fn::tests::layer_fn_has_useful_debug_impl::{{closure}} }".to_string(),
format!("{:?}", layer),
);
}

View File

@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
# Unreleased
- **util**: Add `ServiceExt::map_future`. ([#542])
- **util**: Make combinators implement `Debug` in more cases
- `tracing` is now only pulled in for the features that need it.
[#542]: https://github.com/tower-rs/tower/pull/542

View File

@@ -1,21 +1,33 @@
use futures_core::TryFuture;
use futures_util::{future, TryFutureExt};
use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use futures_core::TryFuture;
use futures_util::{future, TryFutureExt};
use tower_layer::Layer;
use tower_service::Service;
/// Service returned by the [`and_then`] combinator.
///
/// [`and_then`]: crate::util::ServiceExt::and_then
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct AndThen<S, F> {
inner: S,
f: F,
}
impl<S, F> fmt::Debug for AndThen<S, F>
where
S: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AndThen")
.field("inner", &self.inner)
.field("f", &format_args!("{}", std::any::type_name::<F>()))
.finish()
}
}
/// Response future from [`AndThen`] services.
///
/// [`AndThen`]: crate::util::AndThen

View File

@@ -1,4 +1,5 @@
use futures_util::{future, TryFutureExt};
use std::fmt;
use std::task::{Context, Poll};
use tower_layer::Layer;
use tower_service::Service;
@@ -6,12 +7,24 @@ use tower_service::Service;
/// Service returned by the [`map_err`] combinator.
///
/// [`map_err`]: crate::util::ServiceExt::map_err
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct MapErr<S, F> {
inner: S,
f: F,
}
impl<S, F> fmt::Debug for MapErr<S, F>
where
S: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MapErr")
.field("inner", &self.inner)
.field("f", &format_args!("{}", std::any::type_name::<F>()))
.finish()
}
}
/// A [`Layer`] that produces [`MapErr`] services.
///
/// [`Layer`]: tower_layer::Layer

View File

@@ -1,3 +1,4 @@
use std::fmt;
use std::task::{Context, Poll};
use tower_layer::Layer;
use tower_service::Service;
@@ -5,12 +6,24 @@ use tower_service::Service;
/// Service returned by the [`MapRequest`] combinator.
///
/// [`MapRequest`]: crate::util::ServiceExt::map_request
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct MapRequest<S, F> {
inner: S,
f: F,
}
impl<S, F> fmt::Debug for MapRequest<S, F>
where
S: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MapRequest")
.field("inner", &self.inner)
.field("f", &format_args!("{}", std::any::type_name::<F>()))
.finish()
}
}
impl<S, F> MapRequest<S, F> {
/// Creates a new [`MapRequest`] service.
pub fn new(inner: S, f: F) -> Self {

View File

@@ -1,4 +1,5 @@
use futures_util::{future::MapOk, TryFutureExt};
use std::fmt;
use std::task::{Context, Poll};
use tower_layer::Layer;
use tower_service::Service;
@@ -6,12 +7,24 @@ use tower_service::Service;
/// Service returned by the [`map_response`] combinator.
///
/// [`map_response`]: crate::util::ServiceExt::map_response
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct MapResponse<S, F> {
inner: S,
f: F,
}
impl<S, F> fmt::Debug for MapResponse<S, F>
where
S: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MapResponse")
.field("inner", &self.inner)
.field("f", &format_args!("{}", std::any::type_name::<F>()))
.finish()
}
}
/// A [`Layer`] that produces a [`MapResponse`] service.
///
/// [`Layer`]: tower_layer::Layer

View File

@@ -1,4 +1,5 @@
use futures_util::{future::Map, FutureExt};
use std::fmt;
use std::task::{Context, Poll};
use tower_layer::Layer;
use tower_service::Service;
@@ -6,12 +7,24 @@ use tower_service::Service;
/// Service returned by the [`map_result`] combinator.
///
/// [`map_result`]: crate::util::ServiceExt::map_result
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct MapResult<S, F> {
inner: S,
f: F,
}
impl<S, F> fmt::Debug for MapResult<S, F>
where
S: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MapResult")
.field("inner", &self.inner)
.field("f", &format_args!("{}", std::any::type_name::<F>()))
.finish()
}
}
/// A [`Layer`] that produces a [`MapResult`] service.
///
/// [`Layer`]: tower_layer::Layer

View File

@@ -1,3 +1,4 @@
use std::fmt;
use std::future::Future;
use std::task::{Context, Poll};
use tower_service::Service;
@@ -8,11 +9,19 @@ pub fn service_fn<T>(f: T) -> ServiceFn<T> {
}
/// A [`Service`] implemented by a closure.
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone)]
pub struct ServiceFn<T> {
f: T,
}
impl<T> fmt::Debug for ServiceFn<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ServiceFn")
.field("f", &format_args!("{}", std::any::type_name::<T>()))
.finish()
}
}
impl<T, F, Request, R, E> Service<Request> for ServiceFn<T>
where
T: FnMut(Request) -> F,

View File

@@ -1,5 +1,6 @@
use futures_util::{future, FutureExt};
use std::{
fmt,
future::Future,
task::{Context, Poll},
};
@@ -9,12 +10,24 @@ use tower_service::Service;
/// [`Service`] returned by the [`then`] combinator.
///
/// [`then`]: crate::util::ServiceExt::then
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct Then<S, F> {
inner: S,
f: F,
}
impl<S, F> fmt::Debug for Then<S, F>
where
S: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Then")
.field("inner", &self.inner)
.field("f", &format_args!("{}", std::any::type_name::<F>()))
.finish()
}
}
/// A [`Layer`] that produces a [`Then`] service.
///
/// [`Layer`]: tower_layer::Layer