Struct SimpleSandbox

Source
pub struct SimpleSandbox;
Expand description

One-liner execution functions for maximum ease of use

Implementations§

Source§

impl SimpleSandbox

Source

pub async fn run<T, P>( source_path: P, function_name: &str, args: &[Value], ) -> Result<T>
where T: DeserializeOwned + 'static, P: AsRef<Path>,

Execute a single function with minimal setup - the simplest possible API

§Examples
use wasm_sandbox::simple;
 
#[tokio::main]
async fn main() -> Result<(), wasm_sandbox::SandboxError> {
    // Execute a function from a Rust source file
    let result: i32 = simple::run("./calculator.rs", "add", &[serde_json::Value::from(5), serde_json::Value::from(3)]).await?;
     
    // Execute from pre-compiled WASM
    let result: String = simple::run("./module.wasm", "process", &[serde_json::Value::from("hello")]).await?;
    Ok(())
}
Source

pub async fn run_with_config<T, P>( source_path: P, function_name: &str, args: &[Value], config: InstanceConfig, ) -> Result<T>
where T: DeserializeOwned + 'static, P: AsRef<Path>,

Execute with custom configuration but still simple

§Examples
use wasm_sandbox::{simple, InstanceConfig, MemoryUnit};
 
#[tokio::main]
async fn main() -> Result<(), wasm_sandbox::SandboxError> {
    let config = InstanceConfig::builder()
        .memory_limit(128.mb())
        .timeout(60)
        .filesystem_read(&["/data"])
        .build()?;
     
    let result: Vec<String> = simple::run_with_config(
        "./data_processor.rs", 
        "process_files", 
        &["/data/input.txt".into()],
        config
    ).await?;
    Ok(())
}
Source

pub async fn run_sequence<P>( source_path: P, function_calls: &[(&str, Vec<Value>)], ) -> Result<Vec<Value>>
where P: AsRef<Path>,

Execute multiple functions in sequence with the same instance

§Examples
use wasm_sandbox::simple;
use serde_json::json;
 
#[tokio::main]
async fn main() -> Result<(), wasm_sandbox::SandboxError> {
    let calls = vec![
        ("initialize", vec![json!("config.json")]),
        ("process", vec![json!("data1.txt")]),
        ("process", vec![json!("data2.txt")]),
        ("finalize", vec![]),
    ];
     
    let results = simple::run_sequence("./processor.rs", &calls).await?;
    Ok(())
}
Source

pub async fn run_sequence_with_config<P>( source_path: P, function_calls: Vec<(String, Vec<Value>)>, config: InstanceConfig, ) -> Result<Vec<Value>>
where P: AsRef<Path>,

Execute multiple functions with custom configuration

Source

pub async fn create_reusable<P>(source_path: P) -> Result<ReusableSandbox>
where P: AsRef<Path>,

Create a reusable sandbox instance for multiple operations

§Examples
use wasm_sandbox::simple;
 
#[tokio::main]
async fn main() -> Result<(), wasm_sandbox::SandboxError> {
    // Create a reusable sandbox
    let mut sandbox = simple::create_reusable("./calculator.rs").await?;
     
    // Use it multiple times
    let result1: i32 = sandbox.call("add".to_string(), vec![serde_json::Value::from(5), serde_json::Value::from(3)]).await?;
    let result2: i32 = sandbox.call("multiply".to_string(), vec![serde_json::Value::from(4), serde_json::Value::from(6)]).await?;
    Ok(())
}
Source

pub async fn create_reusable_with_config<P>( source_path: P, config: InstanceConfig, ) -> Result<ReusableSandbox>
where P: AsRef<Path>,

Create a reusable sandbox with custom configuration

Auto Trait Implementations§

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> GetSetFdFlags for T

Source§

fn get_fd_flags(&self) -> Result<FdFlags, Error>
where T: AsFilelike,

Query the “status” flags for the self file descriptor.
Source§

fn new_set_fd_flags(&self, fd_flags: FdFlags) -> Result<SetFdFlags<T>, Error>
where T: AsFilelike,

Create a new SetFdFlags value for use with set_fd_flags. Read more
Source§

fn set_fd_flags(&mut self, set_fd_flags: SetFdFlags<T>) -> Result<(), Error>
where T: AsFilelike,

Set the “status” flags for the self file descriptor. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Pointee for T

Source§

type Pointer = u32

Source§

fn debug( pointer: <T as Pointee>::Pointer, f: &mut Formatter<'_>, ) -> Result<(), Error>

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more