Struct Shared

pub struct Shared<S> { /* private fields */ }

A MakeService that produces services by cloning an inner service.

Example

# use std::task::{Context, Poll};
# use std::pin::Pin;
# use std::convert::Infallible;
use std::future::{Ready, ready};
use tower::make::{MakeService, Shared};
use tower::buffer::Buffer;
use tower::Service;

// An example connection type
struct Connection {}

// An example request type
struct Request {}

// An example response type
struct Response {}

// Some service that doesn't implement `Clone`
struct MyService;

impl Service<Request> for MyService {
    type Response = Response;
    type Error = Infallible;
    type Future = Ready<Result<Response, Infallible>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: Request) -> Self::Future {
        ready(Ok(Response {}))
    }
}

// Example function that runs a service by accepting new connections and using
// `Make` to create new services that might be bound to the connection.
//
// This is similar to what you might find in hyper.
async fn serve_make_service<Make>(make: Make)
where
    Make: MakeService<Connection, Request>
{
    // ...
}

# async {
// Our service
let svc = MyService;

// Make it `Clone` by putting a channel in front
let buffered = Buffer::new(svc, 1024);

// Convert it into a `MakeService`
let make = Shared::new(buffered);

// Run the service and just ignore the `Connection`s as `MyService` doesn't need them
serve_make_service(make).await;
# };

Implementations

impl<S> Shared<S>

const fn new(service: S) -> Self

Create a new Shared from a service.

Trait Implementations

impl<S, T> Service<T> for Shared<S> where S: Clone,

type Response = S;
type Error = never;
type Future = SharedFuture<S>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>
fn call(&mut self, _target: T) -> Self::Future

impl<S: Clone> Clone for Shared<S>

fn clone(&self) -> Self

impl<S: Copy> Copy for Shared<S>

impl<S: Debug> Debug for Shared<S>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Auto Trait Implementations

impl<S> Freeze for Shared<S> where S: Freeze,

impl<S> RefUnwindSafe for Shared<S> where S: RefUnwindSafe,

impl<S> Send for Shared<S> where S: Send,

impl<S> Sync for Shared<S> where S: Sync,

impl<S> Unpin for Shared<S> where S: Unpin,

impl<S> UnsafeUnpin for Shared<S> where S: UnsafeUnpin,

impl<S> UnwindSafe for Shared<S> where S: UnwindSafe,

Blanket Implementations

impl<C, Target> MakeConnection<Target> for Shared<S> where C: Service<Target>, <C as Service<Target>>::Response: AsyncRead + AsyncWrite,

type Connection = <C as Service<Target>>::Response;
type Error = <C as Service<Target>>::Error;
type Future = <C as Service<Target>>::Future;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), <C as MakeConnection<Target>>::Error>>
fn make_connection(&mut self, target: Target) -> <C as MakeConnection<Target>>::Future

impl<M, S, Target, Request> MakeService<Target, Request> for Shared<S> where M: Service<Target, Response = S>, S: Service<Request>,

type Response = <S as Service<Request>>::Response;
type Error = <S as Service<Request>>::Error;
type Service = S;
type MakeError = <M as Service<Target>>::Error;
type Future = <M as Service<Target>>::Future;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), <M as MakeService<Target, Request>>::MakeError>>
fn make_service(&mut self, target: Target) -> <M as MakeService<Target, Request>>::Future

impl<T> Any for Shared<S> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Shared<S> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Shared<S> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for Shared<S> where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for Shared<S>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Instrument for Shared<S>

impl<T> ToOwned for Shared<S> where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T> WithSubscriber for Shared<S>

impl<T, Request> ServiceExt<Request> for Shared<S> where T: Service<Request> + ?Sized,

impl<T, U> Into<U> for Shared<S> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for Shared<S> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Shared<S> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>