pub struct LayerFactory(/* private fields */);Expand description
Cloneable handle to a ProcessorLayer.
Debug is redacted to LayerFactory(...) so pipeline snapshots stay
printable regardless of the wrapped layer’s Debug (same style as
OpaqueProcessor).
Equality is Arc pointer identity, not structural: two factories built
from equal-but-distinct layer values compare unequal. Builder tests rely on
this to assert factory ORDER in nesting assertions while the layer itself
stays private.
Implementations§
Source§impl LayerFactory
impl LayerFactory
Sourcepub fn new(layer: impl ProcessorLayer + 'static) -> Self
pub fn new(layer: impl ProcessorLayer + 'static) -> Self
Build a factory from any ProcessorLayer implementation.
§Example
A TimeoutLayer turns a slow producer into a timeout-classified error:
use std::time::Duration;
use camel_api::{BoxProcessor, CamelError, Exchange, LayerFactory, Message, ProcessorFn};
use tower::timeout::TimeoutLayer;
let producer: BoxProcessor = BoxProcessor::new(ProcessorFn::new(|_ex: Exchange| async {
tokio::time::sleep(Duration::from_secs(5)).await;
Ok(Exchange::new(Message::new("late")))
}));
let factory = LayerFactory::new(TimeoutLayer::new(Duration::from_millis(50)));
let wrapped = factory.wrap(producer).expect("timeout layer is Clone + Send");
// The 50ms timeout must fire well inside this 1s guard.
let result = tokio::time::timeout(
Duration::from_secs(1),
tower::ServiceExt::oneshot(wrapped, Exchange::new(Message::new("x"))),
)
.await
.expect("the timeout must fire before the 1s guard");
let err = result.expect_err("the 50ms timeout must fire before the 5s producer finishes");
assert!(
err.to_string().contains("middleware timeout elapsed"),
"expected a timeout-classified CamelError, got: {err}"
);
// Walk the public `source()` chain: the opaque payload returns the
// original error directly from `source()`, so the concrete `Elapsed` is
// the first chain node (reading the payload directly would bypass the
// consumer path).
let mut current = std::error::Error::source(&err);
let mut reached_elapsed = false;
for _ in 0..3 {
let Some(node) = current else { break };
if node.downcast_ref::<tower::timeout::error::Elapsed>().is_some() {
reached_elapsed = true;
break;
}
current = std::error::Error::source(node);
}
assert!(
reached_elapsed,
"source chain must reach tower Elapsed: {err:?}"
);Sourcepub fn wrap(&self, inner: BoxProcessor) -> Result<BoxProcessor, CamelError>
pub fn wrap(&self, inner: BoxProcessor) -> Result<BoxProcessor, CamelError>
Wrap inner with the layer held by this factory.
Trait Implementations§
Source§impl Clone for LayerFactory
impl Clone for LayerFactory
Source§impl Debug for LayerFactory
impl Debug for LayerFactory
Auto Trait Implementations§
impl !RefUnwindSafe for LayerFactory
impl !UnwindSafe for LayerFactory
impl Forget for LayerFactory
impl Freeze for LayerFactory
impl Move for LayerFactory
impl Send for LayerFactory
impl Sync for LayerFactory
impl Unpin for LayerFactory
impl UnsafeUnpin for LayerFactory
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