pub trait LocalContext<Msg>: Sized {
// Required methods
fn from_local<'async_trait>( ) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>
where Self: 'async_trait;
fn msg<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<Msg>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
}Expand description
A trait of the Context type, required when using run_local_handler to execute the message handler.
All Context types must implement the implement the LocalContext::from_local and LocalContext::msg methods for their corresponding Msg type
in order to use the Context with run_local_handler.
§Example
use anyhow::Result;
use serde::Deserialize;
use cobalt_aws::lambda::LocalContext;
/// Shared context we build up before invoking the local runner.
#[derive(Debug)]
pub struct Context {
pub greeting: String,
}
#[derive(Debug, Deserialize)]
pub struct Message {
pub target: String,
}
#[async_trait]
impl LocalContext<Message> for Context {
/// Initialise a shared context object from which will be
/// passed to all instances of the message handler.
async fn from_local() -> Result<Self> {
Ok(Context {
greeting: "Hello".to_string(),
})
}
/// Construct a message object to be processed by the message handler.
async fn msg(&self) -> Result<Message> {
Ok(Message {
target: "World".to_string(),
})
}
}Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.