pub struct Janet { /* private fields */ }
Expand description
Janet
is the central structure of the Janet Language.
All possible Janet types are represented at some point as this structure, either to receive as arguments ou return something to Janet VM.
§Creating new values
With exception to Janet
nil
value the best way to create a Janet
value is to use the Janet::wrap
function, it can receive anything that can be
turned Into
Janet
. For the nil
value, there is a nice function for that, the
Janet::nil
function.
It is also possible to use the From
trait to convert as well.
§Examples
use janetrs::Janet;
let j_nil = Janet::nil();
let jnt = Janet::wrap(10); // A Number Janet
let jnt_str = Janet::wrap("Hello"); // A JanetString Janet
let from_jnt = Janet::from(true); // A boolean Janet
§Extracting/Unwrapping Janet values
To extract/unwrap the Janet
value you can use the Janet::unwrap
method, that
will return a TaggedJanet
that you can pattern match to use the extracted value.
§Example
use janetrs::{Janet, TaggedJanet};
let jnt = Janet::wrap(10); // A Number Janet
match jnt.unwrap() {
TaggedJanet::Abstract(jabstract) => {},
TaggedJanet::Array(array) => {},
TaggedJanet::Boolean(boolean) => {},
TaggedJanet::Buffer(buffer) => {},
TaggedJanet::CFunction(c_fun) => {},
TaggedJanet::Fiber(fiber) => {},
TaggedJanet::Function(func) => {},
TaggedJanet::Keyword(keyword) => {},
TaggedJanet::Nil => {},
TaggedJanet::Number(num) => {},
TaggedJanet::Pointer(ptr) => {},
TaggedJanet::String(string) => {},
TaggedJanet::Struct(st) => {},
TaggedJanet::Symbol(symbol) => {},
TaggedJanet::Table(table) => {},
TaggedJanet::Tuple(tuple) => {},
};
To extract/unwrap the Janet
value you can use the Janet::try_unwrap
method, that
will return a Result
either with the resulted type of and conversion error.
It is possible to use the TryFrom
trait, but that requires to include the trait in
the context.
§Example
use janetrs::{Janet, JanetString};
let jnt = Janet::wrap(10); // A Number Janet
let res_num: Result<f64, _> = jnt.try_unwrap();
assert!(res_num.is_ok());
let res_string = jnt.try_unwrap::<JanetString>();
assert!(res_string.is_err());
Implementations§
source§impl Janet
impl Janet
sourcepub fn array(value: JanetArray<'_>) -> Self
pub fn array(value: JanetArray<'_>) -> Self
Create a array Janet
with value
.
sourcepub fn buffer(value: JanetBuffer<'_>) -> Self
pub fn buffer(value: JanetBuffer<'_>) -> Self
Create a buffer Janet
with value
.
sourcepub fn table(value: JanetTable<'_>) -> Self
pub fn table(value: JanetTable<'_>) -> Self
Create a table Janet
with value
.
sourcepub fn tuple(value: JanetTuple<'_>) -> Self
pub fn tuple(value: JanetTuple<'_>) -> Self
Create a tuple Janet
with value
.
sourcepub fn string(value: JanetString<'_>) -> Self
pub fn string(value: JanetString<'_>) -> Self
Create a string Janet
with value
.
sourcepub fn structs(value: JanetStruct<'_>) -> Self
pub fn structs(value: JanetStruct<'_>) -> Self
Create a struct Janet
with value
.
sourcepub fn symbol(value: JanetSymbol<'_>) -> Self
pub fn symbol(value: JanetSymbol<'_>) -> Self
Create a symbol Janet
with value
.
sourcepub fn keyword(value: JanetKeyword<'_>) -> Self
pub fn keyword(value: JanetKeyword<'_>) -> Self
Create a keyword Janet
with value
.
sourcepub fn fiber(value: JanetFiber<'_>) -> Self
pub fn fiber(value: JanetFiber<'_>) -> Self
Create a fiber Janet
with value
.
sourcepub fn function(value: JanetFunction<'_>) -> Self
pub fn function(value: JanetFunction<'_>) -> Self
Create a function Janet
with value
.
sourcepub fn c_function(value: JanetCFunction) -> Self
pub fn c_function(value: JanetCFunction) -> Self
Create a C function Janet
with value
.
sourcepub fn pointer(value: JanetPointer) -> Self
pub fn pointer(value: JanetPointer) -> Self
Create a pointer Janet
with value
.
sourcepub fn j_abstract(value: JanetAbstract) -> Self
pub fn j_abstract(value: JanetAbstract) -> Self
Create as abstract Janet
with value
.
sourcepub fn dynamic(key: impl AsRef<[u8]>) -> Option<Self>
pub fn dynamic(key: impl AsRef<[u8]>) -> Option<Self>
Get a dynamic keyword binding from the environment if it exists.
sourcepub fn from_core<'a>(symbol: impl Into<JanetSymbol<'a>>) -> Option<Self>
pub fn from_core<'a>(symbol: impl Into<JanetSymbol<'a>>) -> Option<Self>
Resolve a symbol
in the core environment.
sourcepub fn unwrap<'data>(self) -> TaggedJanet<'data>
pub fn unwrap<'data>(self) -> TaggedJanet<'data>
Unwrap the Janet
value into a enum that holds the type value
sourcepub fn unwrap_or<T: TryFrom<Self>>(self, default: T) -> T
pub fn unwrap_or<T: TryFrom<Self>>(self, default: T) -> T
Returns the contained value or a provided default
Consumes the self
argument then, if the conversion to type T
goes correctly,
returns the contained value, otherwise if the conversion fails, returns the
given default
value for that type.
Arguments passed to unwrap_or
are eagerly evaluated; if you are passing
the result of a function call, it is recommended to use
unwrap_or_else
, which is lazily evaluated.
§Example
use janetrs::Janet;
let default = 2.0;
let x = Janet::number(9.0);
assert_eq!(x.unwrap_or(default), 9.0);
let x = Janet::boolean(true);
assert_eq!(x.unwrap_or(default), default);
sourcepub fn unwrap_or_else<T, F>(self, op: F) -> T
pub fn unwrap_or_else<T, F>(self, op: F) -> T
Returns the contained value or computes it from a closure.
The closure holds the value of the conversion error to better handle it to decide a default value to return.
§Example
use janetrs::Janet;
let default = |_| 1.0;
let x = Janet::number(9.0);
assert_eq!(x.unwrap_or_else(default), 9.0);
let x = Janet::boolean(true);
assert_eq!(x.unwrap_or_else(default), 1.0);
sourcepub fn unwrap_or_default<T>(self) -> T
pub fn unwrap_or_default<T>(self) -> T
Returns the contained value or a default
Consumes the self
argument then, if the conversion to type T
goes correctly,
returns the contained value, otherwise if the conversion fails, returns the
default value for that type.
§Examples
use janetrs::Janet;
let x = Janet::number(9.0);
assert_eq!(x.unwrap_or_default::<f64>(), 9.0);
let x = Janet::boolean(true);
assert_eq!(x.unwrap_or_default::<f64>(), 0.0);
sourcepub fn try_unwrap<T: TryFrom<Self>>(self) -> Result<T, T::Error>
pub fn try_unwrap<T: TryFrom<Self>>(self) -> Result<T, T::Error>
sourcepub fn len(&self) -> Option<i32>
pub fn len(&self) -> Option<i32>
Returns the length of a Janet
if it is of a applicable type (Abstract, Array,
Buffer, Keyword, Struct, Symbol, Table, Tuple).
Janet Panics:
If the Janet
value is a Janet Abstract and the method to get the
length (janet) panics, this function janet panics as well.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if Janet
has a applicable type (Abstract, Array, Buffer, Keyword,
Struct, Symbol, Table, Tuple) and the length of it is zero, and false
otherwise.
Janet Panic:
This function may panic for the same reason as Janet::len
sourcepub fn get_method(&self, method_name: &str) -> Option<Self>
pub fn get_method(&self, method_name: &str) -> Option<Self>
Returns a Janet
value containing the requested method if it exists.
sourcepub fn has_method(&self, method_name: &str) -> bool
pub fn has_method(&self, method_name: &str) -> bool
Returns true
if the Janet
has a method called method_name
Trait Implementations§
source§impl Error for Janet
Available on crate feature std
only.
impl Error for Janet
std
only.1.30.0 · source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · source§fn description(&self) -> &str
fn description(&self) -> &str
source§impl<'a> Extend<&'a Janet> for JanetArray<'_>
impl<'a> Extend<&'a Janet> for JanetArray<'_>
source§fn extend<T: IntoIterator<Item = &'a Janet>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = &'a Janet>>(&mut self, iter: T)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl Extend<Janet> for JanetArray<'_>
impl Extend<Janet> for JanetArray<'_>
source§fn extend<T: IntoIterator<Item = Janet>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = Janet>>(&mut self, iter: T)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl From<&JanetArray<'_>> for Janet
impl From<&JanetArray<'_>> for Janet
source§fn from(val: &JanetArray<'_>) -> Self
fn from(val: &JanetArray<'_>) -> Self
source§impl From<&JanetBuffer<'_>> for Janet
impl From<&JanetBuffer<'_>> for Janet
source§fn from(val: &JanetBuffer<'_>) -> Self
fn from(val: &JanetBuffer<'_>) -> 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 From<&JanetFunction<'_>> for Janet
impl From<&JanetFunction<'_>> for Janet
source§fn from(val: &JanetFunction<'_>) -> Self
fn from(val: &JanetFunction<'_>) -> Self
source§impl From<&JanetKeyword<'_>> for Janet
impl From<&JanetKeyword<'_>> for Janet
source§fn from(val: &JanetKeyword<'_>) -> Self
fn from(val: &JanetKeyword<'_>) -> Self
source§impl From<&JanetPointer> for Janet
impl From<&JanetPointer> for Janet
source§fn from(val: &JanetPointer) -> Self
fn from(val: &JanetPointer) -> Self
source§impl From<&JanetString<'_>> for Janet
impl From<&JanetString<'_>> for Janet
source§fn from(val: &JanetString<'_>) -> Self
fn from(val: &JanetString<'_>) -> Self
source§impl From<&JanetStruct<'_>> for Janet
impl From<&JanetStruct<'_>> for Janet
source§fn from(val: &JanetStruct<'_>) -> Self
fn from(val: &JanetStruct<'_>) -> Self
source§impl From<&JanetSymbol<'_>> for Janet
impl From<&JanetSymbol<'_>> for Janet
source§fn from(val: &JanetSymbol<'_>) -> Self
fn from(val: &JanetSymbol<'_>) -> Self
source§impl From<&JanetTable<'_>> for Janet
impl From<&JanetTable<'_>> for Janet
source§fn from(val: &JanetTable<'_>) -> Self
fn from(val: &JanetTable<'_>) -> Self
source§impl From<&JanetTuple<'_>> for Janet
impl From<&JanetTuple<'_>> for Janet
source§fn from(val: &JanetTuple<'_>) -> Self
fn from(val: &JanetTuple<'_>) -> Self
source§impl From<&mut JanetArray<'_>> for Janet
impl From<&mut JanetArray<'_>> for Janet
source§fn from(val: &mut JanetArray<'_>) -> Self
fn from(val: &mut JanetArray<'_>) -> Self
source§impl From<&mut JanetBuffer<'_>> for Janet
impl From<&mut JanetBuffer<'_>> for Janet
source§fn from(val: &mut JanetBuffer<'_>) -> Self
fn from(val: &mut JanetBuffer<'_>) -> Self
source§impl From<&mut JanetTable<'_>> for Janet
impl From<&mut JanetTable<'_>> for Janet
source§fn from(val: &mut JanetTable<'_>) -> Self
fn from(val: &mut JanetTable<'_>) -> Self
source§impl From<Janet> for TaggedJanet<'_>
impl From<Janet> for TaggedJanet<'_>
source§impl From<JanetAbstract> for Janet
impl From<JanetAbstract> for Janet
source§fn from(val: JanetAbstract) -> Self
fn from(val: JanetAbstract) -> Self
source§impl From<JanetArray<'_>> for Janet
impl From<JanetArray<'_>> for Janet
source§fn from(val: JanetArray<'_>) -> Self
fn from(val: JanetArray<'_>) -> Self
source§impl From<JanetBuffer<'_>> for Janet
impl From<JanetBuffer<'_>> for Janet
source§fn from(val: JanetBuffer<'_>) -> Self
fn from(val: JanetBuffer<'_>) -> 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 From<JanetFunction<'_>> for Janet
impl From<JanetFunction<'_>> for Janet
source§fn from(val: JanetFunction<'_>) -> Self
fn from(val: JanetFunction<'_>) -> Self
source§impl From<JanetKeyword<'_>> for Janet
impl From<JanetKeyword<'_>> for Janet
source§fn from(val: JanetKeyword<'_>) -> Self
fn from(val: JanetKeyword<'_>) -> Self
source§impl From<JanetPointer> for Janet
impl From<JanetPointer> for Janet
source§fn from(val: JanetPointer) -> Self
fn from(val: JanetPointer) -> Self
source§impl From<JanetString<'_>> for Janet
impl From<JanetString<'_>> for Janet
source§fn from(val: JanetString<'_>) -> Self
fn from(val: JanetString<'_>) -> Self
source§impl From<JanetStruct<'_>> for Janet
impl From<JanetStruct<'_>> for Janet
source§fn from(val: JanetStruct<'_>) -> Self
fn from(val: JanetStruct<'_>) -> Self
source§impl From<JanetSymbol<'_>> for Janet
impl From<JanetSymbol<'_>> for Janet
source§fn from(val: JanetSymbol<'_>) -> Self
fn from(val: JanetSymbol<'_>) -> Self
source§impl From<JanetTable<'_>> for Janet
impl From<JanetTable<'_>> for Janet
source§fn from(val: JanetTable<'_>) -> Self
fn from(val: JanetTable<'_>) -> Self
source§impl From<JanetTuple<'_>> for Janet
impl From<JanetTuple<'_>> for Janet
source§fn from(val: JanetTuple<'_>) -> Self
fn from(val: JanetTuple<'_>) -> Self
source§impl From<Option<unsafe extern "C-unwind" fn(_: i32, _: *mut Janet) -> Janet>> for Janet
impl From<Option<unsafe extern "C-unwind" fn(_: i32, _: *mut Janet) -> Janet>> for Janet
source§fn from(val: JanetCFunction) -> Self
fn from(val: JanetCFunction) -> Self
source§impl From<TaggedJanet<'_>> for Janet
impl From<TaggedJanet<'_>> for Janet
source§fn from(val: TaggedJanet<'_>) -> Self
fn from(val: TaggedJanet<'_>) -> Self
source§impl Ord for Janet
impl Ord for Janet
source§impl PartialEq<&Janet> for Janet
impl PartialEq<&Janet> for Janet
source§impl PartialEq<&Janet> for Janet
impl PartialEq<&Janet> for Janet
source§impl PartialEq<&Janet> for Janet
impl PartialEq<&Janet> for Janet
source§impl PartialEq<Janet> for &Janet
impl PartialEq<Janet> for &Janet
source§impl PartialEq<Janet> for Janet
impl PartialEq<Janet> for Janet
source§impl PartialEq<Janet> for Janet
impl PartialEq<Janet> for Janet
source§impl PartialEq for Janet
impl PartialEq for Janet
source§impl PartialOrd<&Janet> for Janet
impl PartialOrd<&Janet> for Janet
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 moresource§impl PartialOrd<&Janet> for Janet
impl PartialOrd<&Janet> for Janet
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 moresource§impl PartialOrd<&Janet> for Janet
impl PartialOrd<&Janet> for Janet
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 moresource§impl PartialOrd<Janet> for &Janet
impl PartialOrd<Janet> for &Janet
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 moresource§impl PartialOrd<Janet> for Janet
impl PartialOrd<Janet> for Janet
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 moresource§impl PartialOrd<Janet> for Janet
impl PartialOrd<Janet> for Janet
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 moresource§impl PartialOrd for Janet
impl PartialOrd for Janet
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