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>

source

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.

source

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.

source

pub fn current() -> Option<Self>

Return the current JanetFiber if it exists.

source

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.

source

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.

source

pub fn status(&self) -> FiberStatus

Returns the fiber status.

source

pub fn can_resume(&self) -> bool

Returns if the fiber can be resumed.

source

pub fn can_resume_native(&self) -> bool

Returns if the fiber can be resumed.

source

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));
source

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));
source

pub fn exec_with<'a, F>(&'a mut self, f: F) -> Exec<'a, 'data>
where F: FnOnce() -> Janet,

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));
source

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.

source

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>

source§

fn clone(&self) -> JanetFiber<'data>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'data> Debug for JanetFiber<'data>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<&JanetFiber<'_>> for Janet

source§

fn from(val: &JanetFiber<'_>) -> Self

Converts to this type from the input type.
source§

impl From<JanetFiber<'_>> for Janet

source§

fn from(val: JanetFiber<'_>) -> Self

Converts to this type from the input type.
source§

impl JanetTypeName for JanetFiber<'_>

source§

fn name() -> String

Returns a string with the name of the type
source§

impl<'data> Ord for JanetFiber<'data>

source§

fn cmp(&self, other: &JanetFiber<'data>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<'data> PartialEq for JanetFiber<'data>

source§

fn eq(&self, other: &JanetFiber<'data>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'data> PartialOrd for JanetFiber<'data>

source§

fn partial_cmp(&self, other: &JanetFiber<'data>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl TryFrom<Janet> for JanetFiber<'_>

§

type Error = JanetConversionError

The type returned in the event of a conversion error.
source§

fn try_from(value: Janet) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'data> Eq for JanetFiber<'data>

source§

impl<'data> StructuralPartialEq for JanetFiber<'data>

Auto Trait Implementations§

§

impl<'data> Freeze for JanetFiber<'data>

§

impl<'data> RefUnwindSafe for JanetFiber<'data>

§

impl<'data> !Send for JanetFiber<'data>

§

impl<'data> !Sync for JanetFiber<'data>

§

impl<'data> Unpin for JanetFiber<'data>

§

impl<'data> UnwindSafe for JanetFiber<'data>

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, 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.