pub struct Table<'a> { /* private fields */ }
Expand description
An unordered collection of key-value pairs.
Implementations§
Source§impl<'a> Table<'a>
impl<'a> Table<'a>
Sourcepub fn new() -> Table<'a>
pub fn new() -> Table<'a>
Constructs an empty table.
§Examples
use table::Table;
let table = Table::new();
assert!(table.is_empty());
Sourcepub fn with_map(map: HashMap<Key<'a>, Value<'a>>) -> Table<'a>
pub fn with_map(map: HashMap<Key<'a>, Value<'a>>) -> Table<'a>
Constructs a table from an existing map of key-value pairs.
§Examples
use std::collections::HashMap;
use table::{Key, Value, Table};
let mut map = HashMap::new();
map.insert(Key::from("foo"), Value::from("bar"));
let table = Table::with_map(map);
assert_eq!(table.get::<_, str>("foo"), Some("bar"));
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of key-value pairs contained by the table.
§Examples
use table::Table;
let mut table = Table::new();
assert_eq!(table.len(), 0);
table.insert("foo", "bar");
table.insert("baz", 2);
assert_eq!(table.len(), 2);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
when the table is empty (has no entries).
§Examples
use table::Table;
let mut table = Table::new();
assert!(table.is_empty());
table.insert("foo", "bar");
assert!(!table.is_empty());
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes all key-value pairs in the table, resetting its length to zero.
§Examples
use table::Table;
let mut table = Table::new();
table.insert("foo", "bar");
table.clear();
assert!(table.is_empty());
Sourcepub fn contains_key<K>(&self, key: K) -> bool
pub fn contains_key<K>(&self, key: K) -> bool
Returns true
if and only if a value is stored at the given key.
If this is true, calling self.get::<K, Value>(key)
will always
produce a Value
.
§Examples
use table::Table;
let mut table = Table::new();
assert!(!table.contains_key("foo"));
table.insert("foo", 45);
assert!(table.contains_key("foo"));
Sourcepub fn get<K, V>(&self, key: K) -> Option<&V>
pub fn get<K, V>(&self, key: K) -> Option<&V>
Attempts to retrieve a value and take reference to the inner value.
Calling this method with the V=Value
type parameter will not
perform the conversion, instead returning a reference to the
wrapping Value
object.
§Examples
use table::Table;
let mut table = Table::new();
table.insert("baz", 994);
assert_eq!(table.get::<_, i64>("foo"), None);
assert_eq!(table.get::<_, i64>("baz"), Some(&994));
Sourcepub fn get_mut<K, V>(&mut self, key: K) -> Option<&mut V>
pub fn get_mut<K, V>(&mut self, key: K) -> Option<&mut V>
Attempts to retrive a value and mutably reference the inner value.
Calling this method with the V=Value
type parameter will not perform
the conversion, instead returning a reference to the wrapping Value
object.
§Examples
use table::Table;
let mut table = Table::new();
table.insert("foo", "bar".to_string());
*table.get_mut("foo").unwrap() = "baz".to_string();
assert_eq!(table.get::<_, str>("foo"), Some("baz"));
Sourcepub fn insert<K, V>(&mut self, key: K, value: V) -> Option<Value<'a>>
pub fn insert<K, V>(&mut self, key: K, value: V) -> Option<Value<'a>>
Inserts a value at the given key index, returning any previous value that was stored there.
§Examples
use table::{Value, Table};
let mut table = Table::new();
assert_eq!(table.insert("foo", "bar"), None);
assert_eq!(table.insert("foo", 2), Some(Value::from("bar")));
Sourcepub fn remove<K>(&mut self, key: K) -> Option<Value<'a>>
pub fn remove<K>(&mut self, key: K) -> Option<Value<'a>>
Deletes a value at the given key index, returning any value that was previously stored there.
§Examples
use table::{Value, Table};
let mut table = Table::new();
table.insert("foo", "bar");
assert_eq!(table.remove("foo"), Some(Value::from("bar")));
assert_eq!(table.remove("foo"), None);
Sourcepub fn iter<'b>(&'b self) -> Iter<'a, 'b> ⓘ
pub fn iter<'b>(&'b self) -> Iter<'a, 'b> ⓘ
Returns a borrowing iterator over the key-value pairs in the table.
§Examples
use table::{Key, Value, Table};
let mut table = Table::new();
table.insert("foo", "bar");
let mut iter = table.iter();
assert_eq!(iter.next(), Some((&Key::from("foo"), &Value::from("bar"))));
assert_eq!(iter.next(), None);
Sourcepub fn iter_mut<'b>(&'b mut self) -> IterMut<'a, 'b> ⓘ
pub fn iter_mut<'b>(&'b mut self) -> IterMut<'a, 'b> ⓘ
Returns a mutably borrowing iterator over the key-value pairs in the table.
§Examples
use table::{Key, Value, Table};
let mut table = Table::new();
table.insert("foo", "bar");
for (_key, value) in table.iter_mut() {
*value = Value::from("baz");
}
assert_eq!(table.get::<_, Value>("foo"), Some(&Value::from("baz")));