Struct janetrs::JanetStruct

source ·
pub struct JanetStruct<'data> { /* private fields */ }
Expand description

JanetStructs are immutable data structures that map keys to values.

They are semantically similar to JanetTables, but are immutable. Like JanetTables, 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>

source

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.

source

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.

source

pub fn capacity(&self) -> i32

Returns the number of elements the struct can hold.

source

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

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

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

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

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

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

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.

source

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

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

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

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

pub fn iter(&self) -> Iter<'_, '_>

Creates a iterator over the reference of the struct key-value pairs.

§Examples
use janetrs::structs;

let st = structs! { 1 => "10", true => 10.0};

for (k, v) in st.iter() {
    println!("Key: {}\tValue: {}", k, v);
}
source

pub const fn as_raw(&self) -> *const JanetKV

Return a raw pointer to the struct raw structure.

The caller must ensure that the buffer outlives the pointer this function returns, or else it will end up pointing to garbage.

Trait Implementations§

source§

impl Clone for JanetStruct<'_>

source§

fn clone(&self) -> Self

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 Debug for JanetStruct<'_>

source§

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

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

impl DeepEq<JanetStruct<'_>> for JanetTable<'_>

source§

fn deep_eq(&self, other: &JanetStruct<'_>) -> bool

source§

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

source§

impl DeepEq<JanetTable<'_>> for JanetStruct<'_>

source§

fn deep_eq(&self, other: &JanetTable<'_>) -> bool

source§

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

source§

impl DeepEq for JanetStruct<'_>

source§

fn deep_eq(&self, other: &Self) -> bool

source§

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

source§

impl Default for JanetStruct<'_>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

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

source§

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

Converts to this type from the input type.
source§

impl From<&JanetStruct<'_>> for JanetTable<'_>

source§

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

Converts to this type from the input type.
source§

impl From<&JanetTable<'_>> for JanetStruct<'_>

source§

fn from(table: &JanetTable<'_>) -> Self

Converts to this type from the input type.
source§

impl From<JanetStruct<'_>> for Janet

source§

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

Converts to this type from the input type.
source§

impl From<JanetStruct<'_>> for JanetTable<'_>

source§

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

Converts to this type from the input type.
source§

impl From<JanetTable<'_>> for JanetStruct<'_>

source§

fn from(table: JanetTable<'_>) -> Self

Converts to this type from the input type.
source§

impl<U, J> FromIterator<(U, J)> for JanetStruct<'_>
where U: Into<Janet>, J: Into<Janet>,

source§

fn from_iter<T: IntoIterator<Item = (U, J)>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl<T: Into<Janet>> Index<T> for JanetStruct<'_>

source§

fn index(&self, key: T) -> &Self::Output

Get a reference to the value of a given key.

It is recommended to use get method.

§Janet Panics

Panics if the struct does not have the key.

§

type Output = Janet

The returned type after indexing.
source§

impl<'a, 'data> IntoIterator for &'a JanetStruct<'data>

§

type IntoIter = Iter<'a, 'data>

Which kind of iterator are we turning this into?
§

type Item = (&'a Janet, &'a Janet)

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'data> IntoIterator for JanetStruct<'data>

§

type IntoIter = IntoIter<'data>

Which kind of iterator are we turning this into?
§

type Item = (Janet, Janet)

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl JanetTypeName for JanetStruct<'_>

source§

fn name() -> String

Returns a string with the name of the type
source§

impl Ord for JanetStruct<'_>

source§

fn cmp(&self, other: &Self) -> 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 PartialEq for JanetStruct<'_>

source§

fn eq(&self, other: &Self) -> 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 PartialOrd for JanetStruct<'_>

source§

fn partial_cmp(&self, other: &Self) -> 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 JanetStruct<'_>

§

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 Eq for JanetStruct<'_>

Auto Trait Implementations§

§

impl<'data> Freeze for JanetStruct<'data>

§

impl<'data> RefUnwindSafe for JanetStruct<'data>

§

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

§

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

§

impl<'data> Unpin for JanetStruct<'data>

§

impl<'data> UnwindSafe for JanetStruct<'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.