momento_functions_host/
spawn.rs

1use momento_functions_wit::host::momento::host::spawn;
2
3use crate::encoding::{Encode, EncodeError};
4
5/// An error occurred while spawning a function.
6#[derive(Debug, thiserror::Error)]
7pub enum FunctionSpawnError<E: EncodeError> {
8    /// An error occurred while calling the host interface function.
9    #[error(transparent)]
10    FunctionSpawnError(#[from] spawn::SpawnError),
11    /// An error occurred while encoding the provided payload.
12    #[error("Failed to encode payload")]
13    EncodeFailed {
14        /// The underlying encoding error.
15        cause: E,
16    },
17}
18
19/// Spawn a fire-and-forget Function.
20///
21/// ```rust
22/// # use momento_functions_host::spawn::{self, FunctionSpawnError};
23///
24/// # fn f() -> Result<(), FunctionSpawnError<&'static str>> {
25/// spawn("my_function", b"a payload for my_function".as_slice())?;
26/// # Ok(()) }
27/// ```
28pub fn spawn<E: Encode>(
29    function_name: impl AsRef<str>,
30    payload: E,
31) -> Result<(), FunctionSpawnError<E::Error>> {
32    spawn::spawn_function(
33        function_name.as_ref(),
34        &payload
35            .try_serialize()
36            .map_err(|e| FunctionSpawnError::EncodeFailed { cause: e })?
37            .into(),
38    )
39    .map_err(Into::into)
40}