Struct janetrs::JanetTable[][src]

#[repr(transparent)]
pub struct JanetTable<'data> { /* fields omitted */ }
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

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

Create a empty JanetTable given to Janet the specified capacity.

Examples

use janetrs::JanetTable;

let table = JanetTable::with_capacity(20);

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

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.

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

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!(table.removed() == 0);
table.remove(20);
assert!(table.removed() == 1);

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

Returns the number of elements of the table, also refered 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);

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

Get the prototype table of the table.

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

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

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

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

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

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

Returns the exclusivereference 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);

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

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

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

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

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

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

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

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

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

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

Creates a iterator over the refernece of the table keys.

Examples

use janetrs::table;

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

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

Creates a iterator over the refernece of the table values.

Examples

use janetrs::table;

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

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

Creates a iterator over the mutable refernece 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)));

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

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

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.

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.

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

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

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

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

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

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Creates a value from an iterator. Read more

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.

The returned type after indexing.

Which kind of iterator are we turning this into?

The type of the elements being iterated over.

Creates an iterator from a value. Read more

Which kind of iterator are we turning this into?

The type of the elements being iterated over.

Creates an iterator from a value. Read more

Which kind of iterator are we turning this into?

The type of the elements being iterated over.

Creates an iterator from a value. Read more

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

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.