axum/
util.rs

1use axum_core::response::{IntoResponse, Response};
2use pin_project_lite::pin_project;
3use std::{
4    future::Future,
5    ops::Deref,
6    pin::Pin,
7    sync::Arc,
8    task::{ready, Context, Poll},
9};
10use tower::Service;
11
12#[derive(Clone, Debug, PartialEq, Eq, Hash)]
13pub(crate) struct PercentDecodedStr(Arc<str>);
14
15impl PercentDecodedStr {
16    pub(crate) fn new<S>(s: S) -> Option<Self>
17    where
18        S: AsRef<str>,
19    {
20        percent_encoding::percent_decode(s.as_ref().as_bytes())
21            .decode_utf8()
22            .ok()
23            .map(|decoded| Self(decoded.as_ref().into()))
24    }
25
26    pub(crate) fn as_str(&self) -> &str {
27        &self.0
28    }
29}
30
31impl Deref for PercentDecodedStr {
32    type Target = str;
33
34    #[inline]
35    fn deref(&self) -> &Self::Target {
36        self.as_str()
37    }
38}
39
40pin_project! {
41    #[project = EitherProj]
42    pub(crate) enum Either<A, B> {
43        A { #[pin] inner: A },
44        B { #[pin] inner: B },
45    }
46}
47
48#[derive(Clone)]
49pub(crate) struct MapIntoResponse<S> {
50    inner: S,
51}
52
53impl<S> MapIntoResponse<S> {
54    pub(crate) fn new(inner: S) -> Self {
55        Self { inner }
56    }
57}
58
59impl<B, S> Service<http::Request<B>> for MapIntoResponse<S>
60where
61    S: Service<http::Request<B>>,
62    S::Response: IntoResponse,
63{
64    type Response = Response;
65    type Error = S::Error;
66    type Future = MapIntoResponseFuture<S::Future>;
67
68    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
69        self.inner.poll_ready(cx)
70    }
71
72    fn call(&mut self, req: http::Request<B>) -> Self::Future {
73        MapIntoResponseFuture {
74            inner: self.inner.call(req),
75        }
76    }
77}
78
79pin_project! {
80    pub(crate) struct MapIntoResponseFuture<F> {
81        #[pin]
82        inner: F,
83    }
84}
85
86impl<F, T, E> Future for MapIntoResponseFuture<F>
87where
88    F: Future<Output = Result<T, E>>,
89    T: IntoResponse,
90{
91    type Output = Result<Response, E>;
92
93    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
94        let res = ready!(self.project().inner.poll(cx)?);
95        Poll::Ready(Ok(res.into_response()))
96    }
97}
98
99pub(crate) fn try_downcast<T, K>(k: K) -> Result<T, K>
100where
101    T: 'static,
102    K: Send + 'static,
103{
104    let mut k = Some(k);
105    if let Some(k) = <dyn std::any::Any>::downcast_mut::<Option<T>>(&mut k) {
106        Ok(k.take().unwrap())
107    } else {
108        Err(k.unwrap())
109    }
110}
111
112#[test]
113fn test_try_downcast() {
114    assert_eq!(try_downcast::<i32, _>(5_u32), Err(5_u32));
115    assert_eq!(try_downcast::<i32, _>(5_i32), Ok(5_i32));
116}