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>
impl<'data> JanetTable<'data>
sourcepub fn new() -> Self
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();
sourcepub fn with_capacity(capacity: i32) -> Self
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);
sourcepub fn with_prototype(proto: JanetTable<'data>) -> Self
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"));
sourcepub const unsafe fn from_raw(raw: *mut CJanetTable) -> Self
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.
sourcepub fn capacity(&self) -> i32
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);
sourcepub fn removed(&self) -> i32
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);
sourcepub fn clear(&mut self)
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());
sourcepub fn len(&self) -> i32
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);
sourcepub fn is_empty(&self) -> bool
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());
sourcepub fn set_prototype(&mut self, proto: &JanetTable<'_>)
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));
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, 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);
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, 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);
sourcepub fn get_mut(&mut self, key: impl Into<Janet>) -> Option<&'data mut Janet>
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)));
sourcepub fn get_key_value_mut(
&mut self,
key: impl Into<Janet>
) -> Option<(&Janet, &'data mut Janet)>
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);
sourcepub fn get_proto(&self, key: impl Into<Janet>) -> Option<&Janet>
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);
sourcepub fn get_proto_mut(&mut self, key: impl Into<Janet>) -> Option<&mut Janet>
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);
sourcepub fn get_key_value_proto_mut(
&mut self,
key: impl Into<Janet>
) -> Option<(&Janet, &mut Janet)>
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);
sourcepub fn get_key_value_proto(
&self,
key: impl Into<Janet>
) -> Option<(&Janet, &Janet)>
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);
sourcepub fn get_owned(&self, key: impl Into<Janet>) -> Option<Janet>
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);
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
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);
sourcepub fn raw_get_owned(&self, key: impl Into<Janet>) -> Option<Janet>
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);
sourcepub fn raw_get_owned_key_value(
&self,
key: impl Into<Janet>
) -> Option<(Janet, Janet)>
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);
sourcepub fn remove(&mut self, key: impl Into<Janet>) -> Option<Janet>
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);
sourcepub fn insert(
&mut self,
key: impl Into<Janet>,
value: impl Into<Janet>
) -> Option<Janet>
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")));
sourcepub fn try_insert(
&mut self,
key: impl Into<Janet>,
value: impl Into<Janet>
) -> Result<&mut Janet, OccupiedError<'_, 'data>>
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"));
sourcepub fn contains_key(&self, key: impl Into<Janet>) -> bool
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));
sourcepub fn contains(&self, value: impl Into<Janet>) -> bool
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));
sourcepub fn keys(&self) -> Keys<'_, '_> ⓘ
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);
}
sourcepub fn values(&self) -> Values<'_, '_> ⓘ
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);
}
sourcepub fn values_mut(&mut self) -> ValuesMut<'_, '_> ⓘ
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)));
sourcepub fn iter(&self) -> Iter<'_, '_> ⓘ
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);
}
sourcepub fn iter_mut(&mut self) -> IterMut<'_, '_> ⓘ
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)));
sourcepub const fn as_raw(&self) -> *const CJanetTable
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
.
sourcepub fn as_mut_raw(&mut self) -> *mut CJanetTable
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.
Trait Implementations§
source§impl Clone for JanetTable<'_>
impl Clone for JanetTable<'_>
source§impl Debug for JanetTable<'_>
impl Debug for JanetTable<'_>
source§impl DeepEq<JanetStruct<'_>> for JanetTable<'_>
impl DeepEq<JanetStruct<'_>> for JanetTable<'_>
fn deep_eq(&self, other: &JanetStruct<'_>) -> bool
source§impl DeepEq<JanetTable<'_>> for JanetStruct<'_>
impl DeepEq<JanetTable<'_>> for JanetStruct<'_>
fn deep_eq(&self, other: &JanetTable<'_>) -> bool
source§impl Default for JanetTable<'_>
impl Default for JanetTable<'_>
source§impl<'a> Extend<(&'a Janet, &'a Janet)> for JanetTable<'_>
impl<'a> Extend<(&'a Janet, &'a Janet)> for JanetTable<'_>
source§fn extend<T: IntoIterator<Item = (&'a Janet, &'a Janet)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (&'a Janet, &'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, Janet)> for JanetTable<'_>
impl Extend<(Janet, Janet)> for JanetTable<'_>
source§fn extend<T: IntoIterator<Item = (Janet, Janet)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (Janet, 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<&JanetStruct<'_>> for JanetTable<'_>
impl From<&JanetStruct<'_>> for JanetTable<'_>
source§fn from(val: &JanetStruct<'_>) -> Self
fn from(val: &JanetStruct<'_>) -> 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<&JanetTable<'_>> for JanetStruct<'_>
impl From<&JanetTable<'_>> for JanetStruct<'_>
source§fn from(table: &JanetTable<'_>) -> Self
fn from(table: &JanetTable<'_>) -> 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<JanetStruct<'_>> for JanetTable<'_>
impl From<JanetStruct<'_>> for JanetTable<'_>
source§fn from(val: JanetStruct<'_>) -> Self
fn from(val: JanetStruct<'_>) -> 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<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 JanetTable<'_>
impl<U, J> FromIterator<(U, J)> for JanetTable<'_>
source§impl<'a, 'data> IntoIterator for &'a JanetTable<'data>
impl<'a, 'data> IntoIterator for &'a JanetTable<'data>
source§impl<'a, 'data> IntoIterator for &'a mut JanetTable<'data>
impl<'a, 'data> IntoIterator for &'a mut JanetTable<'data>
source§impl<'data> IntoIterator for JanetTable<'data>
impl<'data> IntoIterator for JanetTable<'data>
source§impl JanetExtend<JanetTable<'_>> for JanetTable<'_>
impl JanetExtend<JanetTable<'_>> for JanetTable<'_>
source§fn extend(&mut self, other: JanetTable<'_>)
fn extend(&mut self, other: JanetTable<'_>)
Extend the table with all key-value pairs of the other
table.
source§impl JanetTypeName for JanetTable<'_>
impl JanetTypeName for JanetTable<'_>
source§impl Ord for JanetTable<'_>
impl Ord for JanetTable<'_>
source§impl PartialEq for JanetTable<'_>
impl PartialEq for JanetTable<'_>
source§impl PartialOrd for JanetTable<'_>
impl PartialOrd for JanetTable<'_>
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