pub trait ProcessorLayer:
Send
+ Sync
+ 'static {
// Required method
fn wrap(&self, inner: BoxProcessor) -> Result<BoxProcessor, CamelError>;
}Expand description
Object-safe factory that wraps a BoxProcessor in a Tower layer.
Implementations are provided by the blanket impl below, which requires the
layered service to be Clone + Send + Sync + 'static with a Send future
and an error convertible into BoxError.
That blanket impl also forecloses hand-written ProcessorLayer impls for
concrete tower layers in this crate — such an impl overlaps the blanket one
(E0119), so a manual impl needs a newtype wrapper around the tower layer.
§Compile-time rejection
A layer whose service does not satisfy those bounds fails to compile at the
wrap call site; there is no runtime fallback. A direct
tower::limit::RateLimit service is the canonical example: it is not
Clone, so wrapping it fails the Clone bound (E0277, compile-probe
evidence in openspec/changes/builder-layer-middleware/probe/PROBE.md,
finding 3). Stateful layers such as RateLimit must be shared through
tower::buffer::Buffer before they can be used here.
§Example
use std::time::Duration;
use camel_api::{BoxProcessor, Exchange, LayerFactory, Message, ProcessorFn};
use tower::timeout::TimeoutLayer;
let inner: BoxProcessor =
BoxProcessor::new(ProcessorFn::new(|ex: Exchange| async move { Ok(ex) }));
let factory = LayerFactory::new(TimeoutLayer::new(Duration::from_millis(50)));
let wrapped = factory.wrap(inner).expect("timeout layer is Clone + Send");
let exchange = Exchange::new(Message::new("payload"));
let response = tower::ServiceExt::oneshot(wrapped, exchange)
.await
.expect("a fast call passes through the timeout layer");
assert_eq!(response.input.body.as_text(), Some("payload"));Required Methods§
Sourcefn wrap(&self, inner: BoxProcessor) -> Result<BoxProcessor, CamelError>
fn wrap(&self, inner: BoxProcessor) -> Result<BoxProcessor, CamelError>
Wrap inner and re-erase the layered service as a BoxProcessor.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".