Struct janetrs::JanetTable

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

Janet tables are mutable data structures that map keys to values. Values are put into a Janet table with a key, and can be looked up later with the same key. Tables are implemented with an underlying open hash table, so they are quite fast and cache friendly.

Any Janet value except Janet nil and Janet number that is NaN can be a key or a value in a Janet table, and a single Janet table can have any mixture of Janet types as keys and values.

To facilitate the creation of this structure, you can use the macro table.

§Examples

use janetrs::{Janet, JanetTable};
let mut table = JanetTable::new();

table.insert("key", 10.0);
table.insert(10, 20.3);

println!("{}", Janet::table(table));

Implementations§

source§

impl<'data> JanetTable<'data>

source

pub fn new() -> Self

Create a empty JanetTable.

It is initially created with capacity 1, so it will not allocate until it is second inserted into.

§Examples
use janetrs::JanetTable;

let table = JanetTable::new();
source

pub fn with_capacity(capacity: i32) -> Self

Create a empty JanetTable given to Janet the specified capacity.

§Examples
use janetrs::JanetTable;

let table = JanetTable::with_capacity(20);
source

pub fn with_prototype(proto: JanetTable<'data>) -> Self

Create a empty JanetTable with a prototype table set to proto.

It is initially created with capacity 1, so it will not allocate until it is second inserted into.

§Examples
use janetrs::{JanetTable, table};

let table = JanetTable::with_prototype(table!(":_name" => "MyClass"));
source

pub const unsafe fn from_raw(raw: *mut CJanetTable) -> Self

Create a new JanetTable with a raw_table.

§Safety

This function do not check if the given raw_table is NULL or not. Use at your own risk.

source

pub fn capacity(&self) -> i32

Returns the number of elements the table can hold without reallocating.

This number is a lower bound; the JanetTable might be able to hold more, but is guaranteed to be able to hold at least this many.

§Examples
use janetrs::JanetTable;

let mut table = JanetTable::with_capacity(20);
assert!(table.capacity() >= 20);
source

pub fn removed(&self) -> i32

Returns the number of elements that was removed from the table.

§Examples
use janetrs::JanetTable;

let mut table = JanetTable::with_capacity(2);
table.insert(10, "ten");
table.insert(20, "twenty");

assert_eq!(table.removed(), 0);
table.remove(20);
assert_eq!(table.removed(), 1);
source

pub fn clear(&mut self)

Clears the table, removing all key-value pairs. Keeps the allocated memory for reuse.

§Examples
use janetrs::JanetTable;

let mut table = JanetTable::with_capacity(20);
table.insert(10, "ten");

table.clear();
assert!(table.is_empty());
source

pub fn len(&self) -> i32

Returns the number of elements of the table, also referred to as its ‘length’.

§Examples
use janetrs::JanetTable;

let mut table = JanetTable::with_capacity(20);

assert_eq!(table.len(), 0);
table.insert(10, "ten");
assert_eq!(table.len(), 1);
source

pub fn is_empty(&self) -> bool

Returns true if the table contains no elements.

§Examples
use janetrs::JanetTable;

let mut table = JanetTable::with_capacity(20);

assert!(table.is_empty());
table.insert(10, "ten");
assert!(!table.is_empty());
source

pub fn prototype(&self) -> Option<Self>

Get the prototype table of the table.

source

pub fn set_prototype(&mut self, proto: &JanetTable<'_>)

Set the prototype of the table with the values of proto.

§Examples
use janetrs::{table, Janet, JanetTable};

let mut table = table! {1 => "a", 2 => "b"};
let proto = table! {":_name" => "MyClass"};

table.set_prototype(&proto);

assert_eq!(table.prototype(), Some(proto));
source

pub fn get(&self, key: impl Into<Janet>) -> Option<&Janet>

Returns the value corresponding to the supplied key.

§Examples
use janetrs::{Janet, JanetTable};

let mut table = JanetTable::with_capacity(20);
table.insert(10, "ten");

assert_eq!(table.get(10), Some(&Janet::from("ten")));
assert_eq!(table.get(11), None);
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, JanetTable};

let mut table = JanetTable::with_capacity(20);
table.insert(10, "ten");

assert_eq!(
    table.get_key_value(10),
    Some((&Janet::integer(10), &Janet::from("ten")))
);
assert_eq!(table.get_key_value(11), None);
source

pub fn get_mut(&mut self, key: impl Into<Janet>) -> Option<&'data mut Janet>

Returns a mutable reference to the value corresponding to the key.

§Examples
use janetrs::{Janet, JanetTable};

let mut table = JanetTable::with_capacity(20);
table.insert(10, "ten");

if let Some(val) = table.get_mut(10) {
    *val = Janet::boolean(true);
}

assert_eq!(table.get(10), Some(&Janet::boolean(true)));
source

pub fn get_key_value_mut( &mut self, key: impl Into<Janet> ) -> Option<(&Janet, &'data mut Janet)>

Returns the key-value pair corresponding to the supplied key, with a mutable reference to value.

§Examples
use janetrs::{Janet, JanetString, JanetTable};

let mut table = JanetTable::with_capacity(2);
table.insert(10, "ten");

let (k, v) = table.get_key_value_mut(10).unwrap();

assert_eq!(&Janet::integer(10), k);
assert_eq!(&mut Janet::from("ten"), v);

*v = Janet::string(JanetString::new("ten but modified"));

assert_eq!(
    table.get_key_value_mut(10),
    Some((&Janet::integer(10), &mut Janet::from("ten but modified")))
);
assert_eq!(table.get_key_value_mut(11), None);
source

pub fn get_proto(&self, key: impl Into<Janet>) -> Option<&Janet>

Returns the reference to the value corresponding to the supplied key, with prototype lookup.

§Examples
use janetrs::{table, Janet, JanetTable};

let mut table = table! {1 => "a", 2 => "b"};
let proto = table! {3 => "c"};

table.set_prototype(&proto);

assert_eq!(table.get_proto(3), Some(&Janet::from("c")));
assert_eq!(table.get_proto(11), None);
source

pub fn get_proto_mut(&mut self, key: impl Into<Janet>) -> Option<&mut Janet>

Returns the exclusive reference to the value corresponding to the supplied key, with prototype lookup.

§Examples
use janetrs::{table, Janet, JanetTable};

let mut table = table! {1 => "a", 2 => "b"};
let proto = table! {3 => "c"};

table.set_prototype(&proto);

assert_eq!(table.get_proto_mut(3), Some(&mut Janet::from("c")));
assert_eq!(table.get_proto_mut(11), None);
source

pub fn get_key_value_proto_mut( &mut self, key: impl Into<Janet> ) -> Option<(&Janet, &mut Janet)>

Returns the key-value pair corresponding to the supplied key with a mutable reference to value, with prototype lookup.

§Examples
use janetrs::{table, Janet, JanetTable};

let mut table = table! {1 => "a", 2 => "b"};
let proto = table! {3 => "c"};

table.set_prototype(&proto);

assert_eq!(
    table.get_key_value_proto_mut(3),
    Some((&Janet::integer(3), &mut Janet::from("c")))
);
assert_eq!(table.get_key_value_proto_mut(11), None);
source

pub fn get_key_value_proto( &self, key: impl Into<Janet> ) -> Option<(&Janet, &Janet)>

Returns the key-value pair corresponding to the supplied key with prototype lookup.

§Examples
use janetrs::{table, Janet, JanetTable};

let mut table = table! {1 => "a", 2 => "b"};
let proto = table! {3 => "c"};

table.set_prototype(&proto);

assert_eq!(
    table.get_key_value_proto(3),
    Some((&Janet::integer(3), &Janet::from("c")))
);
assert_eq!(table.get_key_value_proto(11), None);
source

pub fn get_owned(&self, key: impl Into<Janet>) -> Option<Janet>

Returns the value corresponding to the supplied key checking prototype tables.

§Examples
use janetrs::{Janet, JanetTable};

let mut table = JanetTable::with_capacity(20);
table.insert(10, "ten");

assert_eq!(table.get_owned(10), Some(Janet::from("ten")));
assert_eq!(table.get_owned(11), None);
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 checking prototype tables.

§Examples
use janetrs::{Janet, JanetTable};

let mut table = JanetTable::with_capacity(20);
table.insert(10, "ten");

assert_eq!(
    table.get_owned_key_value(10),
    Some((Janet::integer(10), Janet::from("ten")))
);
assert_eq!(table.get_owned_key_value(11), None);
source

pub fn raw_get_owned(&self, key: impl Into<Janet>) -> Option<Janet>

Returns the value corresponding to the supplied key without checking prototype tables.

§Examples
use janetrs::{Janet, JanetTable};

let mut table = JanetTable::with_capacity(20);
table.insert(10, "ten");

assert_eq!(table.raw_get_owned(10), Some(Janet::from("ten")));
assert_eq!(table.raw_get_owned(11), None);
source

pub fn raw_get_owned_key_value( &self, key: impl Into<Janet> ) -> Option<(Janet, Janet)>

Returns the key-value pair corresponding to the supplied key without checking prototype tables.

§Examples
use janetrs::{Janet, JanetTable};

let mut table = JanetTable::with_capacity(20);
table.insert(10, "ten");

assert_eq!(
    table.raw_get_owned_key_value(10),
    Some((Janet::integer(10), Janet::from("ten")))
);
assert_eq!(table.raw_get_owned_key_value(11), None);
source

pub fn remove(&mut self, key: impl Into<Janet>) -> Option<Janet>

Removes key from the table, returning the value of the key.

§Examples
use janetrs::{Janet, JanetTable};

let mut table = JanetTable::with_capacity(20);
table.insert(10, "ten");

assert_eq!(table.remove(10), Some(Janet::from("ten")));
assert_eq!(table.remove(10), None);
source

pub fn insert( &mut self, key: impl Into<Janet>, value: impl Into<Janet> ) -> Option<Janet>

Inserts a key-value pair into the table.

If the table did not have this key present or if the key is a Janet nil, None is returned.

If the map did have this key present, the value is updated, and the old value is returned.

§Examples
use janetrs::{Janet, JanetTable};

let mut table = JanetTable::new();

assert!(table.is_empty());
assert_eq!(table.insert(37, "a"), None);
assert!(!table.is_empty());

table.insert(37, "b");
assert_eq!(table.insert(37, "c"), Some(Janet::from("b")));
assert_eq!(table.get(37), Some(&Janet::from("c")));
source

pub fn try_insert( &mut self, key: impl Into<Janet>, value: impl Into<Janet> ) -> Result<&mut Janet, OccupiedError<'_, 'data>>

Tries to insert a key-value pair into the map, and returns a mutable reference to the value in the entry.

§Errors

If the map already had this key present, nothing is updated, and an error containing the occupied entry and the value is returned.

§Examples

Basic usage:

use janetrs::{Janet, JanetTable};

let mut map = JanetTable::new();
assert_eq!(map.try_insert(37, "a").unwrap(), &Janet::from("a"));

let err = map.try_insert(37, "b").unwrap_err();
assert_eq!(err.entry.key(), &Janet::from(37));
assert_eq!(err.entry.get(), &Janet::from("a"));
assert_eq!(err.value, Janet::from("b"));
source

pub fn contains_key(&self, key: impl Into<Janet>) -> bool

Returns true if the table contains a value for the specified key.

§Examples
use janetrs::{Janet, JanetTable};

let mut table = JanetTable::new();
table.insert(10, "ten");

assert!(table.contains_key(10));
assert!(!table.contains_key(11));
source

pub fn contains(&self, value: impl Into<Janet>) -> bool

Returns true if the table contais a given value.

§Examples
use janetrs::{Janet, JanetTable};

let mut table = JanetTable::new();
table.insert(10, "ten");

assert!(table.contains("ten"));
assert!(!table.contains(11));
source

pub fn keys(&self) -> Keys<'_, '_>

Creates a iterator over the reference of the table keys.

§Examples
use janetrs::table;

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

for key in table.keys() {
    println!("Key: {}", key);
}
source

pub fn values(&self) -> Values<'_, '_>

Creates a iterator over the reference of the table values.

§Examples
use janetrs::table;

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

for val in table.values() {
    println!("Value: {}", val);
}
source

pub fn values_mut(&mut self) -> ValuesMut<'_, '_>

Creates a iterator over the mutable reference of the table values.

§Examples
use janetrs::{table, Janet};

let mut table = table! { 1 => "10", true => 10.0};

for val in table.values_mut() {
    *val = Janet::number(100.0);
}

assert!(table.values().all(|v| *v == Janet::number(100.0)));
source

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

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

§Examples
use janetrs::table;

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

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

pub fn iter_mut(&mut self) -> IterMut<'_, '_>

Creates a iterator over the reference of the table keys and mutable reference of the table values.

§Examples
use janetrs::{table, Janet};

let mut table = table! { 1 => "10", true => 10.0};

for (k, val) in table.iter_mut() {
    *val = Janet::number(100.0);
}

assert!(table.values().all(|v| *v == Janet::number(100.0)));
source

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

Return a raw pointer to the buffer raw structure.

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

If you need to mutate the contents of the slice, use as_mut_ptr.

source

pub fn as_mut_raw(&mut self) -> *mut CJanetTable

Return a raw mutable pointer to the buffer raw structure.

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

source§

impl<'data> JanetTable<'data>

source

pub fn entry(&mut self, key: impl Into<Janet>) -> Entry<'_, 'data>

Gets the given key’s corresponding entry in the table for in-place manipulation.

Trait Implementations§

source§

impl Clone for JanetTable<'_>

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 JanetTable<'_>

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§

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

source§

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

source§

impl DeepEq for JanetTable<'_>

source§

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

source§

impl Default for JanetTable<'_>

source§

fn default() -> Self

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

impl<'a> Extend<(&'a Janet, &'a Janet)> for JanetTable<'_>

source§

fn extend<T: IntoIterator<Item = (&'a Janet, &'a Janet)>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl Extend<(Janet, Janet)> for JanetTable<'_>

source§

fn extend<T: IntoIterator<Item = (Janet, Janet)>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
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 Janet

source§

fn from(val: &JanetTable<'_>) -> 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<&mut JanetTable<'_>> for Janet

source§

fn from(val: &mut JanetTable<'_>) -> 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 Janet

source§

fn from(val: JanetTable<'_>) -> 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 JanetTable<'_>
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 JanetTable<'_>

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 or the entry API.

§Janet Panics

Panics if the table does not have the key.

§

type Output = Janet

The returned type after indexing.
source§

impl<'a, 'data> IntoIterator for &'a JanetTable<'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<'a, 'data> IntoIterator for &'a mut JanetTable<'data>

§

type IntoIter = IterMut<'a, 'data>

Which kind of iterator are we turning this into?
§

type Item = (&'a Janet, &'data mut 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 JanetTable<'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 JanetExtend<JanetTable<'_>> for JanetTable<'_>

source§

fn extend(&mut self, other: JanetTable<'_>)

Extend the table with all key-value pairs of the other table.

source§

impl JanetTypeName for JanetTable<'_>

source§

fn name() -> String

Returns a string with the name of the type
source§

impl Ord for JanetTable<'_>

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 JanetTable<'_>

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 JanetTable<'_>

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 JanetTable<'_>

§

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 JanetTable<'_>

Auto Trait Implementations§

§

impl<'data> Freeze for JanetTable<'data>

§

impl<'data> RefUnwindSafe for JanetTable<'data>

§

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

§

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

§

impl<'data> Unpin for JanetTable<'data>

§

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