Expand description
This module contains a macro for creating a static runtime instance It creates a safe, thread-local runtime static.
Can be used with default RuntimeOptions
like so:
use rustyscript::{RuntimeOptions, Error, static_runtime};
use std::time::Duration;
static_runtime!(MY_DEFAULT_RUNTIME);
fn main() -> Result<(), Error> {
MY_DEFAULT_RUNTIME::with(|runtime| {
runtime.eval::<()>("console.log('Hello, world!')")
})
}
Or with custom RuntimeOptions
:
use rustyscript::{Error, RuntimeOptions, static_runtime};
use std::time::Duration;
static_runtime!(MY_CUSTOM_RUNTIME, {
RuntimeOptions {
timeout: Duration::from_secs(5),
..Default::default()
}
});
fn main() -> Result<(), Error> {
MY_CUSTOM_RUNTIME::with(|runtime| {
runtime.eval::<()>("console.log('Hello, world!')")
})
}
Structsยง
- Static
Runtime - A static runtime instance
Uses
OnceCell
to ensure that the runtime is only initialized once AndRefCell
to ensure that the runtime is never accessed concurrently And finally, aResult
to catch initialization errors. - Static
Runtime Lock - A lock for the static runtime
Created using
StaticRuntime::lock
Should be mutable to be of any use