Crate tencent_scf[][src]

A rust runtime for Tencent Cloud Serverless Compute Function

This library provides a basic custom runtime for a rust application on Tencent Cloud as Serverless Compute Function. For complete setup, see Setup.

start vs start_uncatched

There are two varaints of start function to start the runtime, to sum up:

  • start catches panic from the provided serverless compute function, but this requires the function to be RefUnwindSafe. Most pure function should satisfy this.
  • start_uncatched allows the serverless compute function to panic the whole process and relies on the cloud to tear down the executor and restart. The downside of this is when a query causes panic on the function, the cloud typically waits until timeout before acknowledging the failure. Sometimes this is necessary to reference !RefUnwindSafe types. Connection pool, for example, usually is not unwind-safe.

Built-in Events

When enabling the correspondent builtin-* feature, auto serialization and deserialization of certain built-in events and reponses are supported.

Current supported built-in events/responses are:

  • API Gateway: enabled by feature builtin-api-gateway, supports trigger as described in API Gateway Trigger. builtin::api::Request and builtin::api::Response will be provided, which are re-export of http::Request and http::Response with support of auto deserialization/serialization.

    For more information, see the module-level documentation.

    use std::convert::Infallible;
    use tencent_scf::{
        builtin::api::{Request, Response, ResponseBuilder},
        make_scf, start, Context,
    fn main() {
        let scf = make_scf(
            |event: Request<String>, _context: Context| -> Result<Response<String>, Infallible> {
                    .body("Hello World".to_string())

Custom Event and Response

Custom types of event and reponse are supported.

Auto Serialization/Deserialization

Any type that implements serde::Deserialize and is marked as convert::AsJson can be deserialized from JSON automatically when feature "json" is enabled. Similarly for types implementing serde::Serialize and is marked as convert::AsJson.


use std::convert::Infallible;

use serde::{Deserialize, Serialize};
// with "json" feature enabled
use tencent_scf::{convert::AsJson, make_scf, start, Context};

// define a custom event
struct CustomEvent {
    a: i32,
    b: i32,

// mark the custom event as json for auto deserialization
impl AsJson for CustomEvent {}

// define a custom response
struct CustomResponse {
    a_plus_b: i32,

// mark the custom response as json for auto serialization
impl AsJson for CustomResponse {}

// make the scf
let scf = make_scf(
    |event: CustomEvent, _context: Context| -> Result<CustomResponse, Infallible> {
        Ok(CustomResponse {
            a_plus_b: event.a + event.b,
// start the runtime in the main function

Manual Serialization/Deserialization

User can also chose to implement convert::FromReader for incoming events and convert::IntoBytes for outgoing response.



Collection of built-in Event and Response for auto-deserialization/serialization


Module that contains traits for converting incoming payload to Event and Response to outgoing payload.



The context of the invocation, assembled from environment variables and invocation headers.



Main trait for a serverless compute function.



Create a ServerlessComputeFunction from a closure.


Start the runtime with the given serverless compute function.


Start the runtime with the given serverless compute function without catching panics.