pub struct Any;
Expand description

A passthrough marker message type.

This is a special message type which will enable your worker to accept any typed message, by ignoring the type information in the payload.

This is especially useful for implementing middleware workers which need access to the route information of a message, without understanding its payload.

Examples

use ockam::{hex, Any, Context, Result, Routed, Worker};

pub struct Logger;

#[ockam::worker]
impl Worker for Logger {
    type Context = Context;
    type Message = Any;

    /// This Worker will take any incoming message, print out the payload
    /// and then forward it to the next hop in its onward route.
    async fn handle_message(&mut self, ctx: &mut Context, msg: Routed<Any>) -> Result<()> {
        let mut local_msg = msg.into_local_message();
        let transport_msg = local_msg.transport_mut();
        transport_msg.onward_route.step()?;
        transport_msg.return_route.modify().prepend(ctx.address());

        let payload = transport_msg.payload.clone();

        if let Ok(str) = String::from_utf8(payload.clone()) {
            println!("Address: {}, Received string: {}", ctx.address(), str);
        } else {
            println!("Address: {}, Received binary: {}", ctx.address(), hex::encode(&payload));
        }

        ctx.forward(local_msg).await
    }
}

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Decode a slice.
Formats the value using the given formatter. Read more
Encode the type into an Encoded type.
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Try cloning a object and return an Err in case of failure.
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more