Struct Function

Source
pub struct Function<S: Sized> { /* private fields */ }
Expand description

A function runtime which wraps a simpler function written by the user and deals with all the complexity of the Fn platform contract while providing an easy to use API.

Implementations§

Source§

impl<S: Sized> Function<S>

Source

pub fn new(func: fn(&RuntimeContext) -> Result<S, FunctionError>) -> Function<S>

Create a new Function with an initializer that is basically a factory for the function’s state. The initializer takes a RuntimeContext from which configuration data can be extracted.

struct MyState {
    greeting: String
}

let func = fdk::Function::new(|context| Ok(MyState {
        greeting: context.config().get("GREETING")
            .unwrap_or(&"Hello!\n".to_string()).clone()
    }));
Source

pub fn run<T, U>(self, func: fn(&mut S, T) -> Result<U, FunctionError>) -> i32

Runs the function runtime and processes any request to pass it down to the code provided. When the execution finishes, returns the exit code the program should exit with.

The code provided must take the function state as the first parameter and the function input as the second. It must then return a result where Ok provides the function output and the error type is a FunctionError.

struct MyState {
    greeting: String
}

let function = fdk::Function::new(|_| Ok(MyState {
    greeting: "Hello".to_string()
}));
let exit_code = function.run(|state, i: String| {
    Ok(format!("{}, {}!", state.greeting, i).to_string())
});

If the function was initialized with the STATELESS constant, the state parameter can be ignored (it is the empty type anyway).

let exit_code = fdk::Function::new(fdk::STATELESS).run(|_, i: String| {
    Ok(format!("Hello, {}!", i).to_string())
});

Most types can be coerced from Requests and to Responses by the runtime without need for explicit conversions. If a type is not already convertible, implement the InputCoercible or OutputCoercible trait for the type.

Input and output coercions are performed so that the code does not need to handle Requests or Responses directly, but it is possible to do so in cases where more control is needed.

extern crate fdk;
extern crate hyper;
// ...
let exit_code = fdk::Function::new(fdk::STATELESS).run(|_, r: hyper::Request| {
    Ok(hyper::Response::new().with_body(r.body()))
});

Auto Trait Implementations§

§

impl<S> Freeze for Function<S>

§

impl<S> RefUnwindSafe for Function<S>

§

impl<S> Send for Function<S>

§

impl<S> Sync for Function<S>

§

impl<S> Unpin for Function<S>

§

impl<S> UnwindSafe for Function<S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.