aws_smithy_http_server/routing/into_make_service.rs
1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6// This code was copied and then modified from Tokio's Axum.
7
8/* Copyright (c) 2021 Tower Contributors
9 *
10 * Permission is hereby granted, free of charge, to any
11 * person obtaining a copy of this software and associated
12 * documentation files (the "Software"), to deal in the
13 * Software without restriction, including without
14 * limitation the rights to use, copy, modify, merge,
15 * publish, distribute, sublicense, and/or sell copies of
16 * the Software, and to permit persons to whom the Software
17 * is furnished to do so, subject to the following
18 * conditions:
19 *
20 * The above copyright notice and this permission notice
21 * shall be included in all copies or substantial portions
22 * of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
25 * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
26 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
27 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
28 * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
29 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
31 * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 * DEALINGS IN THE SOFTWARE.
33 */
34
35use std::{
36 convert::Infallible,
37 future::ready,
38 task::{Context, Poll},
39};
40use tower::Service;
41
42/// A [`MakeService`] that produces router services.
43///
44/// [`MakeService`]: tower::make::MakeService
45#[derive(Debug, Clone)]
46pub struct IntoMakeService<S> {
47 service: S,
48}
49
50impl<S> IntoMakeService<S> {
51 pub fn new(service: S) -> Self {
52 Self { service }
53 }
54}
55
56impl<S, T> Service<T> for IntoMakeService<S>
57where
58 S: Clone,
59{
60 type Response = S;
61 type Error = Infallible;
62 type Future = MakeRouteServiceFuture<S>;
63
64 #[inline]
65 fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
66 Poll::Ready(Ok(()))
67 }
68
69 fn call(&mut self, _target: T) -> Self::Future {
70 MakeRouteServiceFuture::new(ready(Ok(self.service.clone())))
71 }
72}
73
74opaque_future! {
75 /// Response future for [`IntoMakeService`] services.
76 pub type MakeRouteServiceFuture<S> =
77 std::future::Ready<Result<S, Infallible>>;
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 #[test]
85 fn traits() {
86 use crate::test_helpers::*;
87
88 assert_send::<IntoMakeService<()>>();
89 assert_sync::<IntoMakeService<()>>();
90 }
91}