Trait janetrs::JanetArgs

source ·
pub trait JanetArgs {
    // Required methods
    fn get_value(&self, index: usize) -> Option<Janet>;
    fn get_tagged(&self, index: usize) -> Option<TaggedJanet<'_>>;

    // Provided methods
    fn get_matches(&self, index: usize, matches: &[JanetType]) -> Janet { ... }
    fn get_tagged_matches(
        &self,
        index: usize,
        matches: &[JanetType]
    ) -> TaggedJanet<'_> { ... }
    fn try_get<T>(&self, index: usize) -> Result<T, T::Error>
       where T: TryFrom<Janet> { ... }
    fn get_or<T>(&self, index: usize, default: T) -> T
       where T: TryFrom<Janet> { ... }
    fn get_or_default<T>(&self, index: usize) -> T
       where T: TryFrom<Janet> + Default { ... }
    fn get_or_else<T, F>(&self, index: usize, op: F) -> T
       where T: TryFrom<Janet>,
             F: FnOnce(T::Error) -> T { ... }
    fn get_opt<T>(&self, index: usize, default: T) -> T
       where T: TryFrom<Janet> + JanetTypeName { ... }
    fn get_or_panic<T>(&self, index: usize) -> T
       where T: TryFrom<Janet> + JanetTypeName { ... }
}
Expand description

Trait that only exist to extend methods over [Janet] so it’s easier to get janet_fn args.

Required Methods§

source

fn get_value(&self, index: usize) -> Option<Janet>

Get the argument at the index position.

source

fn get_tagged(&self, index: usize) -> Option<TaggedJanet<'_>>

Get the argument at the index position as the TaggedJanet type.

Provided Methods§

source

fn get_matches(&self, index: usize, matches: &[JanetType]) -> Janet

Get the argument at the index position if they match one if the JanetType in matches, janet-panicking otherwise.

§Janet Panics

This function may panic if the item at index does not match any of the JanetType in matches.

source

fn get_tagged_matches( &self, index: usize, matches: &[JanetType] ) -> TaggedJanet<'_>

Get the argument at the index position if they match one if the JanetType in matches as the TaggedJanet type, janet-panicking otherwise.

§Janet Panics

This function may panic if the item at index does not match any of the JanetType in matches.

source

fn try_get<T>(&self, index: usize) -> Result<T, T::Error>
where T: TryFrom<Janet>,

Get the argument at the index position and tries to convert to T.

§Examples
use janetrs::{janet_fn, Janet, JanetArgs};

// Lets say it's a function that if receives an argument, if is not the wanted type, it
// defaults to the given value.
#[janet_fn(arity(range(0, 1)))]
fn my_func(args: &mut [Janet]) -> Janet {
    let res = args.try_get::<i32>(0);

    // Rest of the function
    todo!()
}
source

fn get_or<T>(&self, index: usize, default: T) -> T
where T: TryFrom<Janet>,

Get the argument at the index position and convert to T, if that fails, returns the default value.

§Examples
use janetrs::{janet_fn, Janet, JanetArgs};

// Lets say it's a function that if receives an argument, if is not the wanted type, it
// defaults to the given value.
#[janet_fn(arity(range(0, 1)))]
fn my_func(args: &mut [Janet]) -> Janet {
    let my_flag = args.get_or(0, false);

    // Rest of the function
    todo!()
}
source

fn get_or_default<T>(&self, index: usize) -> T
where T: TryFrom<Janet> + Default,

Get the argument at the index position and convert to T, if that fails, returns the Default::default value for T.

§Examples
use janetrs::{janet_fn, Janet, JanetArgs};

// Lets say it's a function that if receives an argument, if is not the wanted type, it
// defaults to the given value.
#[janet_fn(arity(range(0, 1)))]
fn my_func(args: &mut [Janet]) -> Janet {
    let my_flag: i32 = args.get_or_default(0);

    // Rest of the function
    todo!()
}
source

fn get_or_else<T, F>(&self, index: usize, op: F) -> T
where T: TryFrom<Janet>, F: FnOnce(T::Error) -> T,

Get the argument at the index position and convert to T, if that fails, computes the value from a closure.

§Examples
use janetrs::{janet_fn, Janet, JanetArgs};

// Lets say it's a function that if receives an argument, if is not the wanted type, it
// defaults to the given value.
#[janet_fn(arity(range(0, 1)))]
fn my_func(args: &mut [Janet]) -> Janet {
    let my_flag: i32 = args.get_or_else(0, |_| 10);

    // Rest of the function
    todo!()
}
source

fn get_opt<T>(&self, index: usize, default: T) -> T

Get the argument at the index position, if it’s Janet nil, returns the default value, but janet panics if the the value is different than nil and fail to convert to T.

§Janet Panics

This function may panic if the conversion fails.

§Examples
use janetrs::{janet_fn, Janet, JanetArgs};

// Lets say it's a function that receives a second argument that change de behavior of
// the function
#[janet_fn(arity(range(1, 2)))]
fn my_func(args: &mut [Janet]) -> Janet {
    let my_flag = args.get_opt(1, false);

    // Rest of the function
    todo!()
}
source

fn get_or_panic<T>(&self, index: usize) -> T

Get the argument at the index position and convert to T, if that fails, it janet panics.

§Janet Panics

This function may panic if the conversion fails.

§Examples
use janetrs::{janet_fn, Janet, JanetArgs, JanetString};

#[janet_fn(arity(fix(1)))]
fn my_func(args: &mut [Janet]) -> Janet {
    let my_str: JanetString = args.get_or_panic(0);

    // Rest of the function
    todo!()
}

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl JanetArgs for [Janet]

source§

fn get_value(&self, index: usize) -> Option<Janet>

source§

fn get_tagged(&self, index: usize) -> Option<TaggedJanet<'_>>

source§

impl<const N: usize> JanetArgs for [Janet; N]

source§

fn get_value(&self, index: usize) -> Option<Janet>

source§

fn get_tagged(&self, index: usize) -> Option<TaggedJanet<'_>>

Implementors§