pub struct Runtime<Ctx: OptCtx> { /* private fields */ }Expand description
Provides the types and functions that Roto can access via FFI
Roto is an embedded language, therefore, it must be hooked up to the
outside world to do anything useful besides pure computation. This
connection is provided by the Runtime, which exposes types, methods
and functions to Roto.
Roto can run in different Runtimes, depending on the situation.
Extending the default Runtime is the primary way to extend the
capabilities of Roto.
§Compiling a script
§Registering types, functions and constants.
§Registering context type
§Printing documentation
Implementations§
Source§impl<Ctx: OptCtx> Runtime<Ctx>
impl<Ctx: OptCtx> Runtime<Ctx>
Sourcepub fn print_documentation(&self, path: &Path) -> Result<()>
pub fn print_documentation(&self, path: &Path) -> Result<()>
Print documentation for all the types registered into this runtime.
The format for the documentation is markdown that can be passed to Sphinx.
Source§impl<Ctx: OptCtx> Runtime<Ctx>
impl<Ctx: OptCtx> Runtime<Ctx>
Sourcepub fn add_io_functions(&mut self)
pub fn add_io_functions(&mut self)
Add functions using I/O to the runtime.
These functions are disabled by default because Roto might be used in a context where using I/O is not permitted.
For now, this just adds the print function. More functions will be
added in the future.
Source§impl Runtime<NoCtx>
Creating a Runtime
impl Runtime<NoCtx>
Creating a Runtime
Sourcepub fn new() -> Self
pub fn new() -> Self
A Runtime that is as empty as possible.
This contains only type information for Roto primitives.
Sourcepub fn from_lib(items: impl Registerable) -> Result<Self, RegistrationError>
pub fn from_lib(items: impl Registerable) -> Result<Self, RegistrationError>
Create a new Runtime with a given library.
This is nothing more than a convenience function around
Runtime::new followed by Runtime::add.
Source§impl<C: OptCtx> Runtime<C>
Inspecting and modifying the Runtime
impl<C: OptCtx> Runtime<C>
Inspecting and modifying the Runtime
Sourcepub fn add(&mut self, items: impl Registerable) -> Result<(), RegistrationError>
pub fn add(&mut self, items: impl Registerable) -> Result<(), RegistrationError>
Add a library of items to this Runtime
Typically, one would use the library! macro to
register items. This would look something like this:
use roto::{Runtime, library};
let mut rt = Runtime::new();
rt.add(library! {
/* define items here */
}).unwrap();See the documentation on the library! macro for more information.
However, it is – with a bit of extra boilerplate – also possible to register items without using the macro. You can directly register one of the item types:
Or you can register an Item which combines all the above.
Additionally, you can register collections of these types, such
as Vec<Item> or [Item; N].
For example:
use roto::{Runtime, Constant, Function, Impl, location, Item};
let mut rt = Runtime::new();
// Add a single constant
rt.add(
Constant::new(
"U32_MAX",
"The maximum value of a `u32`.",
u32::MAX,
location!(),
).unwrap()
).unwrap();
// Add a constant and a method:
let mut impl_block = Impl::new::<i32>(location!());
impl_block.add(Function::new(
"max",
"The maximum value of an `i32`",
vec![],
|| i32::MAX,
location!(),
).unwrap());
rt.add([
Item::Impl(impl_block),
Item::Constant(Constant::new(
"I32_MAX",
"The maximum value of an `i32`",
i32::MAX,
location!(),
).unwrap())
]).unwrap();See also Runtime::from_lib, which combines Runtime::new and
Runtime::add into a single function.
Sourcepub fn context(&self) -> &Option<ContextDescription>
pub fn context(&self) -> &Option<ContextDescription>
Get the context type, if any.
Sourcepub fn constants(&self) -> &HashMap<ResolvedName, RuntimeConstant>
pub fn constants(&self) -> &HashMap<ResolvedName, RuntimeConstant>
Get the registered constants.
Sourcepub fn try_without_ctx(self) -> Option<Runtime<NoCtx>>
pub fn try_without_ctx(self) -> Option<Runtime<NoCtx>>
Try to obtain a Runtime that does not have a context.
This is useful to turn a Runtime with a genenic C parameter into Runtime<NoCtx>.
Trait Implementations§
Auto Trait Implementations§
impl<Ctx> Freeze for Runtime<Ctx>
impl<Ctx> !RefUnwindSafe for Runtime<Ctx>
impl<Ctx> Send for Runtime<Ctx>where
Ctx: Send,
impl<Ctx> Sync for Runtime<Ctx>where
Ctx: Sync,
impl<Ctx> Unpin for Runtime<Ctx>where
Ctx: Unpin,
impl<Ctx> !UnwindSafe for Runtime<Ctx>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);