pub struct DynamicService<In, Out>(/* private fields */);Available on crate features
dynamic-service only.Expand description
Type-erased wrapper for Service that hides the concrete type.
Use DynamicService when working with complex service compositions where the
concrete type becomes unwieldy, or when storing services of different types in
collections.
Type erasure adds some overhead, but it’s typically negligible compared to the actual service work (network calls, database queries, etc.).
§Examples
use layered::{DynamicService, DynamicServiceExt, Execute, Service};
async fn example() {
// Create a concrete service that doubles an integer and uses a closure.
let service = Execute::new(|v: i32| async move { v * 2 });
// Create a type-erased service
let service: DynamicService<i32, i32> = service.into_dynamic();
// Use it like any other service
let result = service.execute(42).await;
println!("Result: {}", result);
}Trait Implementations§
Source§impl<In, Out> Clone for DynamicService<In, Out>
impl<In, Out> Clone for DynamicService<In, Out>
Source§impl<In, Out> Debug for DynamicService<In, Out>
impl<In, Out> Debug for DynamicService<In, Out>
Auto Trait Implementations§
impl<In, Out> Freeze for DynamicService<In, Out>
impl<In, Out> !RefUnwindSafe for DynamicService<In, Out>
impl<In, Out> Send for DynamicService<In, Out>
impl<In, Out> Sync for DynamicService<In, Out>
impl<In, Out> Unpin for DynamicService<In, Out>
impl<In, Out> !UnwindSafe for DynamicService<In, Out>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<In, Out, T> DynamicServiceExt<In, Out> for T
impl<In, Out, T> DynamicServiceExt<In, Out> for T
Source§fn into_dynamic(self) -> DynamicService<In, Out>
fn into_dynamic(self) -> DynamicService<In, Out>
Available on crate features
dynamic-service only.Converts this service into a type-erased
DynamicService.