Vendor dependencies

This commit is contained in:
2026-08-01 16:11:49 +03:00
parent 7f139a0241
commit 6b5e7f0f8b
29706 changed files with 9575646 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
use super::Layer;
use std::fmt;
/// A no-op middleware.
///
/// When wrapping a [`Service`], the [`Identity`] layer returns the provided
/// service without modifying it.
///
/// [`Service`]: https://docs.rs/tower-service/latest/tower_service/trait.Service.html
#[derive(Default, Clone)]
pub struct Identity {
_p: (),
}
impl Identity {
/// Create a new [`Identity`] value
pub const fn new() -> Identity {
Identity { _p: () }
}
}
/// Decorates a [`Service`], transforming either the request or the response.
///
/// [`Service`]: https://docs.rs/tower-service/latest/tower_service/trait.Service.html
impl<S> Layer<S> for Identity {
type Service = S;
fn layer(&self, inner: S) -> Self::Service {
inner
}
}
impl fmt::Debug for Identity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Identity").finish()
}
}
+114
View File
@@ -0,0 +1,114 @@
use super::Layer;
use std::fmt;
/// Returns a new [`LayerFn`] that implements [`Layer`] by calling the
/// given function.
///
/// The [`Layer::layer`] method takes a type implementing [`Service`] and
/// returns a different type implementing [`Service`]. In many cases, this can
/// be implemented by a function or a closure. The [`LayerFn`] helper allows
/// writing simple [`Layer`] implementations without needing the boilerplate of
/// a new struct implementing [`Layer`].
///
/// # Example
/// ```rust
/// # use tower::Service;
/// # use std::task::{Poll, Context};
/// # use tower_layer::{Layer, layer_fn};
/// # use std::fmt;
/// # use std::convert::Infallible;
/// #
/// // A middleware that logs requests before forwarding them to another service
/// pub struct LogService<S> {
/// target: &'static str,
/// service: S,
/// }
///
/// impl<S, Request> Service<Request> for LogService<S>
/// where
/// S: Service<Request>,
/// Request: fmt::Debug,
/// {
/// type Response = S::Response;
/// type Error = S::Error;
/// type Future = S::Future;
///
/// fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
/// self.service.poll_ready(cx)
/// }
///
/// fn call(&mut self, request: Request) -> Self::Future {
/// // Log the request
/// println!("request = {:?}, target = {:?}", request, self.target);
///
/// self.service.call(request)
/// }
/// }
///
/// // A `Layer` that wraps services in `LogService`
/// let log_layer = layer_fn(|service| {
/// LogService {
/// service,
/// target: "tower-docs",
/// }
/// });
///
/// // An example service. This one uppercases strings
/// let uppercase_service = tower::service_fn(|request: String| async move {
/// Ok::<_, Infallible>(request.to_uppercase())
/// });
///
/// // Wrap our service in a `LogService` so requests are logged.
/// let wrapped_service = log_layer.layer(uppercase_service);
/// ```
///
/// [`Service`]: https://docs.rs/tower-service/latest/tower_service/trait.Service.html
/// [`Layer::layer`]: crate::Layer::layer
pub fn layer_fn<T>(f: T) -> LayerFn<T> {
LayerFn { f }
}
/// A `Layer` implemented by a closure. See the docs for [`layer_fn`] for more details.
#[derive(Clone, Copy)]
pub struct LayerFn<F> {
f: F,
}
impl<F, S, Out> Layer<S> for LayerFn<F>
where
F: Fn(S) -> Out,
{
type Service = Out;
fn layer(&self, inner: S) -> Self::Service {
(self.f)(inner)
}
}
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>()))
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[allow(dead_code)]
#[test]
fn layer_fn_has_useful_debug_impl() {
struct WrappedService<S> {
inner: S,
}
let layer = layer_fn(|svc| WrappedService { inner: svc });
let _svc = layer.layer("foo");
assert_eq!(
"LayerFn { f: tower_layer::layer_fn::tests::layer_fn_has_useful_debug_impl::{{closure}} }".to_string(),
format!("{:?}", layer),
);
}
}
+112
View File
@@ -0,0 +1,112 @@
#![warn(
missing_debug_implementations,
missing_docs,
rust_2018_idioms,
unreachable_pub
)]
#![forbid(unsafe_code)]
// `rustdoc::broken_intra_doc_links` is checked on CI
//! Layer traits and extensions.
//!
//! A layer decorates an service and provides additional functionality. It
//! allows other services to be composed with the service that implements layer.
//!
//! A middleware implements the [`Layer`] and [`Service`] trait.
//!
//! [`Service`]: https://docs.rs/tower/*/tower/trait.Service.html
mod identity;
mod layer_fn;
mod stack;
mod tuple;
pub use self::{
identity::Identity,
layer_fn::{layer_fn, LayerFn},
stack::Stack,
};
/// Decorates a [`Service`], transforming either the request or the response.
///
/// Often, many of the pieces needed for writing network applications can be
/// reused across multiple services. The `Layer` trait can be used to write
/// reusable components that can be applied to very different kinds of services;
/// for example, it can be applied to services operating on different protocols,
/// and to both the client and server side of a network transaction.
///
/// # Log
///
/// Take request logging as an example:
///
/// ```rust
/// # use tower_service::Service;
/// # use std::task::{Poll, Context};
/// # use tower_layer::Layer;
/// # use std::fmt;
///
/// pub struct LogLayer {
/// target: &'static str,
/// }
///
/// impl<S> Layer<S> for LogLayer {
/// type Service = LogService<S>;
///
/// fn layer(&self, service: S) -> Self::Service {
/// LogService {
/// target: self.target,
/// service
/// }
/// }
/// }
///
/// // This service implements the Log behavior
/// pub struct LogService<S> {
/// target: &'static str,
/// service: S,
/// }
///
/// impl<S, Request> Service<Request> for LogService<S>
/// where
/// S: Service<Request>,
/// Request: fmt::Debug,
/// {
/// type Response = S::Response;
/// type Error = S::Error;
/// type Future = S::Future;
///
/// fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
/// self.service.poll_ready(cx)
/// }
///
/// fn call(&mut self, request: Request) -> Self::Future {
/// // Insert log statement here or other functionality
/// println!("request = {:?}, target = {:?}", request, self.target);
/// self.service.call(request)
/// }
/// }
/// ```
///
/// The above log implementation is decoupled from the underlying protocol and
/// is also decoupled from client or server concerns. In other words, the same
/// log middleware could be used in either a client or a server.
///
/// [`Service`]: https://docs.rs/tower/*/tower/trait.Service.html
pub trait Layer<S> {
/// The wrapped service
type Service;
/// Wrap the given service with the middleware, returning a new service
/// that has been decorated with the middleware.
fn layer(&self, inner: S) -> Self::Service;
}
impl<'a, T, S> Layer<S> for &'a T
where
T: ?Sized + Layer<S>,
{
type Service = T::Service;
fn layer(&self, inner: S) -> Self::Service {
(**self).layer(inner)
}
}
+62
View File
@@ -0,0 +1,62 @@
use super::Layer;
use std::fmt;
/// Two middlewares chained together.
#[derive(Clone)]
pub struct Stack<Inner, Outer> {
inner: Inner,
outer: Outer,
}
impl<Inner, Outer> Stack<Inner, Outer> {
/// Create a new `Stack`.
pub const fn new(inner: Inner, outer: Outer) -> Self {
Stack { inner, outer }
}
}
impl<S, Inner, Outer> Layer<S> for Stack<Inner, Outer>
where
Inner: Layer<S>,
Outer: Layer<Inner::Service>,
{
type Service = Outer::Service;
fn layer(&self, service: S) -> Self::Service {
let inner = self.inner.layer(service);
self.outer.layer(inner)
}
}
impl<Inner, Outer> fmt::Debug for Stack<Inner, Outer>
where
Inner: fmt::Debug,
Outer: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// The generated output of nested `Stack`s is very noisy and makes
// it harder to understand what is in a `ServiceBuilder`.
//
// Instead, this output is designed assuming that a `Stack` is
// usually quite nested, and inside a `ServiceBuilder`. Therefore,
// this skips using `f.debug_struct()`, since each one would force
// a new layer of indentation.
//
// - In compact mode, a nested stack ends up just looking like a flat
// list of layers.
//
// - In pretty mode, while a newline is inserted between each layer,
// the `DebugStruct` used in the `ServiceBuilder` will inject padding
// to that each line is at the same indentation level.
//
// Also, the order of [outer, inner] is important, since it reflects
// the order that the layers were added to the stack.
if f.alternate() {
// pretty
write!(f, "{:#?},\n{:#?}", self.outer, self.inner)
} else {
write!(f, "{:?}, {:?}", self.outer, self.inner)
}
}
}
+330
View File
@@ -0,0 +1,330 @@
use crate::Layer;
impl<S> Layer<S> for () {
type Service = S;
fn layer(&self, service: S) -> Self::Service {
service
}
}
impl<S, L1> Layer<S> for (L1,)
where
L1: Layer<S>,
{
type Service = L1::Service;
fn layer(&self, service: S) -> Self::Service {
let (l1,) = self;
l1.layer(service)
}
}
impl<S, L1, L2> Layer<S> for (L1, L2)
where
L1: Layer<L2::Service>,
L2: Layer<S>,
{
type Service = L1::Service;
fn layer(&self, service: S) -> Self::Service {
let (l1, l2) = self;
l1.layer(l2.layer(service))
}
}
impl<S, L1, L2, L3> Layer<S> for (L1, L2, L3)
where
L1: Layer<L2::Service>,
L2: Layer<L3::Service>,
L3: Layer<S>,
{
type Service = L1::Service;
fn layer(&self, service: S) -> Self::Service {
let (l1, l2, l3) = self;
l1.layer((l2, l3).layer(service))
}
}
impl<S, L1, L2, L3, L4> Layer<S> for (L1, L2, L3, L4)
where
L1: Layer<L2::Service>,
L2: Layer<L3::Service>,
L3: Layer<L4::Service>,
L4: Layer<S>,
{
type Service = L1::Service;
fn layer(&self, service: S) -> Self::Service {
let (l1, l2, l3, l4) = self;
l1.layer((l2, l3, l4).layer(service))
}
}
impl<S, L1, L2, L3, L4, L5> Layer<S> for (L1, L2, L3, L4, L5)
where
L1: Layer<L2::Service>,
L2: Layer<L3::Service>,
L3: Layer<L4::Service>,
L4: Layer<L5::Service>,
L5: Layer<S>,
{
type Service = L1::Service;
fn layer(&self, service: S) -> Self::Service {
let (l1, l2, l3, l4, l5) = self;
l1.layer((l2, l3, l4, l5).layer(service))
}
}
impl<S, L1, L2, L3, L4, L5, L6> Layer<S> for (L1, L2, L3, L4, L5, L6)
where
L1: Layer<L2::Service>,
L2: Layer<L3::Service>,
L3: Layer<L4::Service>,
L4: Layer<L5::Service>,
L5: Layer<L6::Service>,
L6: Layer<S>,
{
type Service = L1::Service;
fn layer(&self, service: S) -> Self::Service {
let (l1, l2, l3, l4, l5, l6) = self;
l1.layer((l2, l3, l4, l5, l6).layer(service))
}
}
impl<S, L1, L2, L3, L4, L5, L6, L7> Layer<S> for (L1, L2, L3, L4, L5, L6, L7)
where
L1: Layer<L2::Service>,
L2: Layer<L3::Service>,
L3: Layer<L4::Service>,
L4: Layer<L5::Service>,
L5: Layer<L6::Service>,
L6: Layer<L7::Service>,
L7: Layer<S>,
{
type Service = L1::Service;
fn layer(&self, service: S) -> Self::Service {
let (l1, l2, l3, l4, l5, l6, l7) = self;
l1.layer((l2, l3, l4, l5, l6, l7).layer(service))
}
}
impl<S, L1, L2, L3, L4, L5, L6, L7, L8> Layer<S> for (L1, L2, L3, L4, L5, L6, L7, L8)
where
L1: Layer<L2::Service>,
L2: Layer<L3::Service>,
L3: Layer<L4::Service>,
L4: Layer<L5::Service>,
L5: Layer<L6::Service>,
L6: Layer<L7::Service>,
L7: Layer<L8::Service>,
L8: Layer<S>,
{
type Service = L1::Service;
fn layer(&self, service: S) -> Self::Service {
let (l1, l2, l3, l4, l5, l6, l7, l8) = self;
l1.layer((l2, l3, l4, l5, l6, l7, l8).layer(service))
}
}
impl<S, L1, L2, L3, L4, L5, L6, L7, L8, L9> Layer<S> for (L1, L2, L3, L4, L5, L6, L7, L8, L9)
where
L1: Layer<L2::Service>,
L2: Layer<L3::Service>,
L3: Layer<L4::Service>,
L4: Layer<L5::Service>,
L5: Layer<L6::Service>,
L6: Layer<L7::Service>,
L7: Layer<L8::Service>,
L8: Layer<L9::Service>,
L9: Layer<S>,
{
type Service = L1::Service;
fn layer(&self, service: S) -> Self::Service {
let (l1, l2, l3, l4, l5, l6, l7, l8, l9) = self;
l1.layer((l2, l3, l4, l5, l6, l7, l8, l9).layer(service))
}
}
impl<S, L1, L2, L3, L4, L5, L6, L7, L8, L9, L10> Layer<S>
for (L1, L2, L3, L4, L5, L6, L7, L8, L9, L10)
where
L1: Layer<L2::Service>,
L2: Layer<L3::Service>,
L3: Layer<L4::Service>,
L4: Layer<L5::Service>,
L5: Layer<L6::Service>,
L6: Layer<L7::Service>,
L7: Layer<L8::Service>,
L8: Layer<L9::Service>,
L9: Layer<L10::Service>,
L10: Layer<S>,
{
type Service = L1::Service;
fn layer(&self, service: S) -> Self::Service {
let (l1, l2, l3, l4, l5, l6, l7, l8, l9, l10) = self;
l1.layer((l2, l3, l4, l5, l6, l7, l8, l9, l10).layer(service))
}
}
impl<S, L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11> Layer<S>
for (L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11)
where
L1: Layer<L2::Service>,
L2: Layer<L3::Service>,
L3: Layer<L4::Service>,
L4: Layer<L5::Service>,
L5: Layer<L6::Service>,
L6: Layer<L7::Service>,
L7: Layer<L8::Service>,
L8: Layer<L9::Service>,
L9: Layer<L10::Service>,
L10: Layer<L11::Service>,
L11: Layer<S>,
{
type Service = L1::Service;
fn layer(&self, service: S) -> Self::Service {
let (l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11) = self;
l1.layer((l2, l3, l4, l5, l6, l7, l8, l9, l10, l11).layer(service))
}
}
impl<S, L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11, L12> Layer<S>
for (L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11, L12)
where
L1: Layer<L2::Service>,
L2: Layer<L3::Service>,
L3: Layer<L4::Service>,
L4: Layer<L5::Service>,
L5: Layer<L6::Service>,
L6: Layer<L7::Service>,
L7: Layer<L8::Service>,
L8: Layer<L9::Service>,
L9: Layer<L10::Service>,
L10: Layer<L11::Service>,
L11: Layer<L12::Service>,
L12: Layer<S>,
{
type Service = L1::Service;
fn layer(&self, service: S) -> Self::Service {
let (l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12) = self;
l1.layer((l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12).layer(service))
}
}
impl<S, L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11, L12, L13> Layer<S>
for (L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11, L12, L13)
where
L1: Layer<L2::Service>,
L2: Layer<L3::Service>,
L3: Layer<L4::Service>,
L4: Layer<L5::Service>,
L5: Layer<L6::Service>,
L6: Layer<L7::Service>,
L7: Layer<L8::Service>,
L8: Layer<L9::Service>,
L9: Layer<L10::Service>,
L10: Layer<L11::Service>,
L11: Layer<L12::Service>,
L12: Layer<L13::Service>,
L13: Layer<S>,
{
type Service = L1::Service;
fn layer(&self, service: S) -> Self::Service {
let (l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13) = self;
l1.layer((l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13).layer(service))
}
}
impl<S, L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11, L12, L13, L14> Layer<S>
for (L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11, L12, L13, L14)
where
L1: Layer<L2::Service>,
L2: Layer<L3::Service>,
L3: Layer<L4::Service>,
L4: Layer<L5::Service>,
L5: Layer<L6::Service>,
L6: Layer<L7::Service>,
L7: Layer<L8::Service>,
L8: Layer<L9::Service>,
L9: Layer<L10::Service>,
L10: Layer<L11::Service>,
L11: Layer<L12::Service>,
L12: Layer<L13::Service>,
L13: Layer<L14::Service>,
L14: Layer<S>,
{
type Service = L1::Service;
fn layer(&self, service: S) -> Self::Service {
let (l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14) = self;
l1.layer((l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14).layer(service))
}
}
#[rustfmt::skip]
impl<S, L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11, L12, L13, L14, L15> Layer<S>
for (L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11, L12, L13, L14, L15)
where
L1: Layer<L2::Service>,
L2: Layer<L3::Service>,
L3: Layer<L4::Service>,
L4: Layer<L5::Service>,
L5: Layer<L6::Service>,
L6: Layer<L7::Service>,
L7: Layer<L8::Service>,
L8: Layer<L9::Service>,
L9: Layer<L10::Service>,
L10: Layer<L11::Service>,
L11: Layer<L12::Service>,
L12: Layer<L13::Service>,
L13: Layer<L14::Service>,
L14: Layer<L15::Service>,
L15: Layer<S>,
{
type Service = L1::Service;
fn layer(&self, service: S) -> Self::Service {
let (l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15) = self;
l1.layer((l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15).layer(service))
}
}
#[rustfmt::skip]
impl<S, L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11, L12, L13, L14, L15, L16> Layer<S>
for (L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11, L12, L13, L14, L15, L16)
where
L1: Layer<L2::Service>,
L2: Layer<L3::Service>,
L3: Layer<L4::Service>,
L4: Layer<L5::Service>,
L5: Layer<L6::Service>,
L6: Layer<L7::Service>,
L7: Layer<L8::Service>,
L8: Layer<L9::Service>,
L9: Layer<L10::Service>,
L10: Layer<L11::Service>,
L11: Layer<L12::Service>,
L12: Layer<L13::Service>,
L13: Layer<L14::Service>,
L14: Layer<L15::Service>,
L15: Layer<L16::Service>,
L16: Layer<S>,
{
type Service = L1::Service;
fn layer(&self, service: S) -> Self::Service {
let (l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15, l16) = self;
l1.layer((l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15, l16).layer(service))
}
}