pub trait ServerlessComputeFunction {
type Event;
type Response;
type Error;
// Required method
fn call(
&self,
event: Self::Event,
context: Context,
) -> Result<Self::Response, Self::Error>;
}
Expand description
Main trait for a serverless compute function.
§Note
Depending on whether the concrete type is unwind-safe, user should choose between start
and start_uncatched
to start the runtime. This trait doesn’t imply RefUnwindSafe
.
§Implement the Trait
Using a closure should cover most of the cases. If a struct/enum is used, the trait can be implemented as:
use tencent_scf::{start, Context, ServerlessComputeFunction};
struct MyFunction {
some_attribute: String,
}
impl ServerlessComputeFunction for MyFunction {
// Type for input event
type Event = String;
// Type for output response
type Response = String;
// Type for possible error
type Error = std::convert::Infallible;
// Implement the execution of the function
fn call(
&self,
event: Self::Event,
context: Context,
) -> Result<Self::Response, Self::Error> {
// We just concatenate the strings
Ok(event + &self.some_attribute)
}
}
start(MyFunction {
some_attribute: "suffix".to_string(),
});