Struct janetrs::JanetFiber
source · pub struct JanetFiber<'data> { /* private fields */ }
Expand description
A lightweight green thread in Janet. It does not correspond to operating system threads.
Fibers allow a process to stop and resume execution later, essentially enabling multiple returns from a function.
Different from traditional coroutines, Janet’s fibers implement a signaling mechanism, which is used to differentiate different kinds of returns. When a fiber yields or throws an error, control is returned to the calling fiber. The parent fiber must then check what kind of state the fiber is in to differentiate errors from return values from user-defined signals.
Implementations§
source§impl<'data> JanetFiber<'data>
impl<'data> JanetFiber<'data>
sourcepub fn new(
capacity: i32,
f: &mut JanetFunction<'_>,
args: impl AsRef<[Janet]>
) -> Option<Self>
pub fn new( capacity: i32, f: &mut JanetFunction<'_>, args: impl AsRef<[Janet]> ) -> Option<Self>
Create a new JanetFiber
from a JanetFunction
and it’s arguments.
In case any passed argument is invalid, returns None
.
sourcepub fn with_env(
env: JanetTable<'_>,
capacity: i32,
f: &mut JanetFunction<'_>,
args: impl AsRef<[Janet]>
) -> Option<Self>
pub fn with_env( env: JanetTable<'_>, capacity: i32, f: &mut JanetFunction<'_>, args: impl AsRef<[Janet]> ) -> Option<Self>
Create a new JanetFiber
from a JanetFunction
and it’s arguments with a
given environments.
In case any passed argument is invalid, returns None
.
sourcepub fn current() -> Option<Self>
pub fn current() -> Option<Self>
Return the current JanetFiber
if it exists.
sourcepub fn root() -> Option<Self>
pub fn root() -> Option<Self>
Return the root JanetFiber
if it exists.
The root fiber is the oldest ancestor that does not have a parent.
sourcepub const unsafe fn from_raw(raw: *mut CJanetFiber) -> Self
pub const unsafe fn from_raw(raw: *mut CJanetFiber) -> Self
Create a new JanetFiber
with a raw
pointer.
§Safety
This function do not check if the given raw
is NULL
or not. Use at your
own risk.
sourcepub fn status(&self) -> FiberStatus
pub fn status(&self) -> FiberStatus
Returns the fiber status.
sourcepub fn can_resume(&self) -> bool
pub fn can_resume(&self) -> bool
Returns if the fiber can be resumed.
sourcepub fn can_resume_native(&self) -> bool
pub fn can_resume_native(&self) -> bool
Returns if the fiber can be resumed.
sourcepub fn exec<'a>(&'a mut self) -> Exec<'a, 'data> ⓘ
pub fn exec<'a>(&'a mut self) -> Exec<'a, 'data> ⓘ
Creates a iterator that can execute the fiber function until it’s done.
§Examples
use janetrs::{client::JanetClient, JanetFiber, JanetFunction};
let _client = JanetClient::init_with_default_env()?;
let f = _client.run(
"(fn []
(yield 1)
(yield 2)
(yield 3)
(yield 4)
5)",
)?;
let mut f_concrete: JanetFunction = f.try_unwrap()?;
let mut fiber = JanetFiber::new(64, &mut f_concrete, &[]).unwrap();
fiber.exec().for_each(|j| println!("{}", j));
sourcepub fn exec_input<'a>(&'a mut self, input: Janet) -> Exec<'a, 'data> ⓘ
pub fn exec_input<'a>(&'a mut self, input: Janet) -> Exec<'a, 'data> ⓘ
Creates a iterator that can execute the fiber function until it’s done, modifying
the input to input
.
A input
of value of Janet nil is the same as calling the exec
method.
§Examples
use janetrs::{client::JanetClient, Janet, JanetFiber, JanetFunction};
let _client = JanetClient::init_with_default_env()?;
let f = _client.run(
"(fn [x]
(yield (+ x 1))
(yield (+ x 2))
(yield (* x 2))
(yield (* x 3))
x)",
)?;
let mut f_concrete: JanetFunction = f.try_unwrap()?;
let mut fiber = JanetFiber::new(64, &mut f_concrete, &[10i64.into()]).unwrap();
fiber
.exec_input(Janet::integer(12))
.for_each(|j| println!("{}", j));
sourcepub fn exec_with<'a, F>(&'a mut self, f: F) -> Exec<'a, 'data> ⓘ
pub fn exec_with<'a, F>(&'a mut self, f: F) -> Exec<'a, 'data> ⓘ
Creates a iterator that can execute the fiber function until it’s done, modifying the input with the given function.
A F
that returns the value of Janet nil is the same as calling the exec
method.
§Examples
use janetrs::{client::JanetClient, Janet, JanetFiber, JanetFunction};
let _client = JanetClient::init_with_default_env()?;
let f = _client.run(
"(fn [x]
(yield (+ x 1))
(yield (+ x 2))
(yield (* x 2))
(yield (* x 3))
x)",
)?;
let mut f_concrete: JanetFunction = f.try_unwrap()?;
let mut fiber = JanetFiber::new(64, &mut f_concrete, &[10i64.into()]).unwrap();
fiber
.exec_with(|| Janet::integer(3))
.for_each(|j| println!("{}", j));
sourcepub const fn as_raw(&self) -> *const CJanetFiber
pub const fn as_raw(&self) -> *const CJanetFiber
Return a raw pointer to the fiber raw structure.
The caller must ensure that the fiber outlives the pointer this function returns, or else it will end up pointing to garbage.
If you need to mutate the contents of the slice, use as_mut_ptr
.
sourcepub fn as_mut_raw(&mut self) -> *mut CJanetFiber
pub fn as_mut_raw(&mut self) -> *mut CJanetFiber
Return a raw mutable pointer to the fiber raw structure.
The caller must ensure that the fiber outlives the pointer this function returns, or else it will end up pointing to garbage.
Trait Implementations§
source§impl<'data> Clone for JanetFiber<'data>
impl<'data> Clone for JanetFiber<'data>
source§fn clone(&self) -> JanetFiber<'data>
fn clone(&self) -> JanetFiber<'data>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<'data> Debug for JanetFiber<'data>
impl<'data> Debug for JanetFiber<'data>
source§impl From<&JanetFiber<'_>> for Janet
impl From<&JanetFiber<'_>> for Janet
source§fn from(val: &JanetFiber<'_>) -> Self
fn from(val: &JanetFiber<'_>) -> Self
source§impl From<JanetFiber<'_>> for Janet
impl From<JanetFiber<'_>> for Janet
source§fn from(val: JanetFiber<'_>) -> Self
fn from(val: JanetFiber<'_>) -> Self
source§impl JanetTypeName for JanetFiber<'_>
impl JanetTypeName for JanetFiber<'_>
source§impl<'data> Ord for JanetFiber<'data>
impl<'data> Ord for JanetFiber<'data>
source§fn cmp(&self, other: &JanetFiber<'data>) -> Ordering
fn cmp(&self, other: &JanetFiber<'data>) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<'data> PartialEq for JanetFiber<'data>
impl<'data> PartialEq for JanetFiber<'data>
source§fn eq(&self, other: &JanetFiber<'data>) -> bool
fn eq(&self, other: &JanetFiber<'data>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<'data> PartialOrd for JanetFiber<'data>
impl<'data> PartialOrd for JanetFiber<'data>
source§fn partial_cmp(&self, other: &JanetFiber<'data>) -> Option<Ordering>
fn partial_cmp(&self, other: &JanetFiber<'data>) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more