Struct janetrs::JanetStruct
source · pub struct JanetStruct<'data> { /* private fields */ }
Expand description
JanetStruct
s are immutable data structures that map keys to values.
They are semantically similar to JanetTable
s, but are immutable. Like
JanetTable
s, they are backed by an efficient, native hash table.
To facilitate the creation of this structure, you can use the macro
structs
.
§Examples
use janetrs::JanetStruct;
let st = JanetStruct::builder(2)
.put("ten", 10)
.put("eleven", 11)
.finalize();
Implementations§
source§impl<'data> JanetStruct<'data>
impl<'data> JanetStruct<'data>
sourcepub fn builder(len: i32) -> JanetStructBuilder<'data>
pub fn builder(len: i32) -> JanetStructBuilder<'data>
Start the build process to create a JanetStruct
.
If the given len
is lesser than zero it behaves the same as if len
is zero.
sourcepub const unsafe fn from_raw(raw: *const JanetKV) -> Self
pub const unsafe fn from_raw(raw: *const JanetKV) -> Self
Create a new JanetStruct
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 len(&self) -> i32
pub fn len(&self) -> i32
Returns the number of elements in the struct, also referred to as its ‘length’.
§Examples
use janetrs::JanetStruct;
let st = JanetStruct::builder(2)
.put(10, "ten")
.put(11, "eleven")
.finalize();
assert_eq!(st.len(), 2);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the struct contains no elements.
§Examples
use janetrs::JanetStruct;
let st = JanetStruct::builder(2)
.put(10, "ten")
.put(11, "eleven")
.finalize();
assert!(!st.is_empty());
let st = JanetStruct::builder(0).finalize();
assert!(st.is_empty());
sourcepub fn get(&self, key: impl Into<Janet>) -> Option<&Janet>
pub fn get(&self, key: impl Into<Janet>) -> Option<&Janet>
Returns the value corresponding to the supplied key
.
§Examples
use janetrs::{Janet, JanetStruct};
let st = JanetStruct::builder(2)
.put(10, "ten")
.put(11, "eleven")
.finalize();
assert_eq!(st.get(10), Some(&Janet::from("ten")));
sourcepub fn get_key_value(&self, key: impl Into<Janet>) -> Option<(&Janet, &Janet)>
pub fn get_key_value(&self, key: impl Into<Janet>) -> Option<(&Janet, &Janet)>
Returns the key-value pair corresponding to the supplied key
.
§Examples
use janetrs::{Janet, JanetStruct};
let st = JanetStruct::builder(2)
.put(10, "ten")
.put(11, "eleven")
.finalize();
assert_eq!(
st.get_key_value(10),
Some((&Janet::integer(10), &Janet::from("ten")))
);
sourcepub fn get_owned(&self, key: impl Into<Janet>) -> Option<Janet>
pub fn get_owned(&self, key: impl Into<Janet>) -> Option<Janet>
Returns a owned value corresponding to the supplied key
.
§Examples
use janetrs::{Janet, JanetStruct};
let st = JanetStruct::builder(2)
.put(10, "ten")
.put(11, "eleven")
.finalize();
assert_eq!(st.get_owned(10), Some(Janet::from("ten")));
sourcepub fn get_owned_key_value(
&self,
key: impl Into<Janet>
) -> Option<(Janet, Janet)>
pub fn get_owned_key_value( &self, key: impl Into<Janet> ) -> Option<(Janet, Janet)>
Returns the key-value pair corresponding to the supplied key
.
§Examples
use janetrs::{Janet, JanetStruct};
let st = JanetStruct::builder(2).put(10, "ten").put(11, "eleven").finalize();
assert_eq!(st.get_owned_key_value(10), Some((Janet::integer(10), Janet::from("ten"))));
sourcepub fn find(&self, key: impl Into<Janet>) -> Option<(&Janet, &Janet)>
pub fn find(&self, key: impl Into<Janet>) -> Option<(&Janet, &Janet)>
Find the bucket that contains the given key
.
Notice that if there is no key-value pair in the table, this function will return
a tuple of mutable references to Janet nil
.
sourcepub fn contains_key(&self, key: impl Into<Janet>) -> bool
pub fn contains_key(&self, key: impl Into<Janet>) -> bool
Returns true
if the struct contains a value for the specified key
.
§Examples
use janetrs::{structs, Janet};
let st = structs! {10 => "ten"};
assert!(st.contains_key(10));
assert!(!st.contains_key(11));
sourcepub fn contains(&self, value: impl Into<Janet>) -> bool
pub fn contains(&self, value: impl Into<Janet>) -> bool
Returns true
if the struct contains a given value.
§Examples
use janetrs::{structs, Janet};
let st = structs! {10 => "ten"};
assert!(st.contains("ten"));
assert!(!st.contains(11));
sourcepub fn keys(&self) -> Keys<'_, '_> ⓘ
pub fn keys(&self) -> Keys<'_, '_> ⓘ
Creates a iterator over the reference of the struct keys.
§Examples
use janetrs::structs;
let st = structs! { 1 => "10", true => 10.0};
for key in st.keys() {
println!("Key: {}", key);
}
sourcepub fn values(&self) -> Values<'_, '_> ⓘ
pub fn values(&self) -> Values<'_, '_> ⓘ
Creates a iterator over the reference of the struct values.
§Examples
use janetrs::structs;
let st = structs! { 1 => "10", true => 10.0};
for val in st.values() {
println!("Value: {}", val);
}
Trait Implementations§
source§impl Clone for JanetStruct<'_>
impl Clone for JanetStruct<'_>
source§impl Debug for JanetStruct<'_>
impl Debug for JanetStruct<'_>
source§impl DeepEq<JanetStruct<'_>> for JanetTable<'_>
impl DeepEq<JanetStruct<'_>> for JanetTable<'_>
source§impl DeepEq<JanetTable<'_>> for JanetStruct<'_>
impl DeepEq<JanetTable<'_>> for JanetStruct<'_>
source§impl DeepEq for JanetStruct<'_>
impl DeepEq for JanetStruct<'_>
source§impl Default for JanetStruct<'_>
impl Default for JanetStruct<'_>
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<&JanetStruct<'_>> for JanetTable<'_>
impl From<&JanetStruct<'_>> for JanetTable<'_>
source§fn from(val: &JanetStruct<'_>) -> Self
fn from(val: &JanetStruct<'_>) -> Self
source§impl From<&JanetTable<'_>> for JanetStruct<'_>
impl From<&JanetTable<'_>> for JanetStruct<'_>
source§fn from(table: &JanetTable<'_>) -> Self
fn from(table: &JanetTable<'_>) -> 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<JanetStruct<'_>> for JanetTable<'_>
impl From<JanetStruct<'_>> for JanetTable<'_>
source§fn from(val: JanetStruct<'_>) -> Self
fn from(val: JanetStruct<'_>) -> Self
source§impl From<JanetTable<'_>> for JanetStruct<'_>
impl From<JanetTable<'_>> for JanetStruct<'_>
source§fn from(table: JanetTable<'_>) -> Self
fn from(table: JanetTable<'_>) -> Self
source§impl<U, J> FromIterator<(U, J)> for JanetStruct<'_>
impl<U, J> FromIterator<(U, J)> for JanetStruct<'_>
source§impl<'a, 'data> IntoIterator for &'a JanetStruct<'data>
impl<'a, 'data> IntoIterator for &'a JanetStruct<'data>
source§impl<'data> IntoIterator for JanetStruct<'data>
impl<'data> IntoIterator for JanetStruct<'data>
source§impl JanetTypeName for JanetStruct<'_>
impl JanetTypeName for JanetStruct<'_>
source§impl Ord for JanetStruct<'_>
impl Ord for JanetStruct<'_>
source§impl PartialEq for JanetStruct<'_>
impl PartialEq for JanetStruct<'_>
source§impl PartialOrd for JanetStruct<'_>
impl PartialOrd for JanetStruct<'_>
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