pub trait JanetArgs {
// Required methods
fn get_unwrapped<T: TryFrom<Janet>>(
&self,
index: usize
) -> Result<T, T::Error>;
fn get_or<T: TryFrom<Janet>>(&self, index: usize, default: T) -> T;
fn get_opt<T: TryFrom<Janet> + JanetTypeName>(
&self,
index: usize,
default: T
) -> T;
fn get_panic<T: TryFrom<Janet> + JanetTypeName>(&self, index: usize) -> T;
}
Expand description
Trait that only exist to extend methods over [Janet]
so it’s easier to get
janet_fn
args.
Required Methods§
sourcefn get_unwrapped<T: TryFrom<Janet>>(&self, index: usize) -> Result<T, T::Error>
fn get_unwrapped<T: TryFrom<Janet>>(&self, index: usize) -> Result<T, T::Error>
Get the argument at the index
position and tries to convert to T
.
sourcefn get_or<T: TryFrom<Janet>>(&self, index: usize, default: T) -> T
fn get_or<T: TryFrom<Janet>>(&self, index: usize, default: T) -> T
Get the argument at the index
position and convert to T
, if that fails,
returns the default
value.
§Examples
use janetrs::{bad_slot, 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!()
}
sourcefn get_opt<T: TryFrom<Janet> + JanetTypeName>(
&self,
index: usize,
default: T
) -> T
fn get_opt<T: TryFrom<Janet> + JanetTypeName>( &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::{bad_slot, 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!()
}
sourcefn get_panic<T: TryFrom<Janet> + JanetTypeName>(&self, index: usize) -> T
fn get_panic<T: TryFrom<Janet> + JanetTypeName>(&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::{bad_slot, janet_fn, Janet, JanetArgs, JanetString};
#[janet_fn(arity(fix(1)))]
fn my_func(args: &mut [Janet]) -> Janet {
let my_str: JanetString = args.get_panic(0);
// Rest of the function
todo!()
}
Object Safety§
This trait is not object safe.