Enum Either

Source
pub enum Either<A, B> {
    Left(A),
    Right(B),
}
Expand description

An Enum representing a value of one of two possible types.

Either<A, B> is particularly useful in service composition and layered architectures.

Either allows for conditional inclusion of layers in a service stack:


impl<T> SvcC<T> {
   fn layer<C>() -> impl FactoryLayer<C, T, Factory = Self> {
      layer_fn(|_: &C, inner| SvcC { inner })
   }

  fn opt_layer<C>(enabled: bool) -> Option<impl FactoryLayer<C, T, Factory = Self>> {
      if enabled {
          Some(layer_fn(|_: &C, inner| SvcC { inner }))
      } else {
          None
      }
   }
}

let stack = FactoryStack::new(config)
    .push(SvcAFactory::layer())
    .push(SvcBFactory::layer())
    .push(SvcC::opt_layer(true));  // Conditionally include SvcC

This pattern enables runtime control over the service composition, making it possible to dynamically include or exclude certain layers based on configuration or runtime conditions.

Variants§

§

Left(A)

§

Right(B)

Implementations§

Source§

impl<A, B> Either<A, B>

Source

pub fn as_pin_ref(self: Pin<&Self>) -> Either<Pin<&A>, Pin<&B>>

Convert Pin<&Either<A, B>> to Either<Pin<&A>, Pin<&B>>, pinned projections of the inner variants.

Source

pub fn as_pin_mut(self: Pin<&mut Self>) -> Either<Pin<&mut A>, Pin<&mut B>>

Convert Pin<&mut Either<A, B>> to Either<Pin<&mut A>, Pin<&mut B>>, pinned projections of the inner variants.

Source§

impl<T> Either<T, T>

Source

pub fn into_inner(self) -> T

Trait Implementations§

Source§

impl<A, B> AsyncMakeService for Either<A, B>

Source§

type Service = Either<<A as AsyncMakeService>::Service, <B as AsyncMakeService>::Service>

The type of service this factory creates.
Source§

type Error = Either<<A as AsyncMakeService>::Error, <B as AsyncMakeService>::Error>

The type of error that can occur during service creation.
Source§

async fn make_via_ref( &self, old: Option<&Self::Service>, ) -> Result<Self::Service, Self::Error>

Asynchronously creates a new service, optionally using an existing service as a reference. Read more
Source§

fn make(&self) -> impl Future<Output = Result<Self::Service, Self::Error>>

Asynchronously creates a new service without referencing an existing one. Read more
Source§

impl<A: Clone, B: Clone> Clone for Either<A, B>

Source§

fn clone(&self) -> Either<A, B>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<A: Debug, B: Debug> Debug for Either<A, B>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<A: Display, B: Display> Display for Either<A, B>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<A: Error, B: Error> Error for Either<A, B>

Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl<C, F, FLA, FLB> FactoryLayer<C, F> for Either<FLA, FLB>
where FLA: FactoryLayer<C, F>, FLB: FactoryLayer<C, F>,

Source§

type Factory = Either<<FLA as FactoryLayer<C, F>>::Factory, <FLB as FactoryLayer<C, F>>::Factory>

The type of factory this layer produces.
Source§

fn layer(&self, config: &C, inner: F) -> Self::Factory

Creates a new factory wrapper. Read more
Source§

impl<A, B> Future for Either<A, B>
where A: Future, B: Future<Output = A::Output>,

Source§

type Output = <A as Future>::Output

The type of value produced on completion.
Source§

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>

Attempts to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more
Source§

impl<A, B> MakeService for Either<A, B>
where A: MakeService, B: MakeService,

Source§

type Service = Either<<A as MakeService>::Service, <B as MakeService>::Service>

The type of service this factory creates.
Source§

type Error = Either<<A as MakeService>::Error, <B as MakeService>::Error>

The type of error that can occur during service creation.
Source§

fn make_via_ref( &self, old: Option<&Self::Service>, ) -> Result<Self::Service, Self::Error>

Creates a new service, optionally using an existing service as a reference. Read more
Source§

fn make(&self) -> Result<Self::Service, Self::Error>

Creates a new service without referencing an existing one. Read more
Source§

impl<A, B, R> Service<R> for Either<A, B>
where A: Service<R>, B: Service<R, Response = A::Response, Error = A::Error>,

Source§

type Response = <A as Service<R>>::Response

The type of response returned by this service.
Source§

type Error = <A as Service<R>>::Error

The type of error that this service can produce.
Source§

fn call( &self, req: R, ) -> impl Future<Output = Result<Self::Response, Self::Error>>

Asynchronously process the request and return the response. Read more

Auto Trait Implementations§

§

impl<A, B> Freeze for Either<A, B>
where A: Freeze, B: Freeze,

§

impl<A, B> RefUnwindSafe for Either<A, B>

§

impl<A, B> Send for Either<A, B>
where A: Send, B: Send,

§

impl<A, B> Sync for Either<A, B>
where A: Sync, B: Sync,

§

impl<A, B> Unpin for Either<A, B>
where A: Unpin, B: Unpin,

§

impl<A, B> UnwindSafe for Either<A, B>
where A: UnwindSafe, B: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T, Request, Response, E> BoxService<Request, Response, E> for T
where T: Service<Request, Response = Response, Error = E> + 'static, Request: 'static,

Source§

fn into_boxed(self) -> BoxedService<Request, Response, E>

Source§

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

Source§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<F> IntoFuture for F
where F: Future,

Source§

type Output = <F as Future>::Output

The output that the future will produce on completion.
Source§

type IntoFuture = F

Which kind of future are we turning this into?
Source§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. Read more
Source§

impl<T> Param<T> for T
where T: Clone,

Source§

fn param(&self) -> T

Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

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

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.