Struct Table

Source
pub struct Table<'a> { /* private fields */ }
Expand description

An unordered collection of key-value pairs.

Implementations§

Source§

impl<'a> Table<'a>

Source

pub fn new() -> Table<'a>

Constructs an empty table.

§Examples
use table::Table;

let table = Table::new();
assert!(table.is_empty());
Source

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

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

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

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

pub fn contains_key<K>(&self, key: K) -> bool
where Key<'a>: From<K>,

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

pub fn get<K, V>(&self, key: K) -> Option<&V>
where V: ?Sized, Key<'a>: From<K>, Value<'a>: TryAsRef<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));
Source

pub fn get_mut<K, V>(&mut self, key: K) -> Option<&mut V>
where V: ?Sized, Key<'a>: From<K>, Value<'a>: TryAsMut<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"));
Source

pub fn insert<K, V>(&mut self, key: K, value: V) -> Option<Value<'a>>
where Key<'a>: From<K>, Value<'a>: From<V>,

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

pub fn remove<K>(&mut self, key: K) -> Option<Value<'a>>
where Key<'a>: From<K>,

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

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

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

Trait Implementations§

Source§

impl<'a> Clone for Table<'a>

Source§

fn clone(&self) -> Table<'a>

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<'a> Debug for Table<'a>

Source§

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

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

impl<'de> Deserialize<'de> for Table<'de>

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<'a> From<&'a Table<'a>> for Value<'a>

Source§

fn from(value: &'a Table<'a>) -> Value<'a>

Converts to this type from the input type.
Source§

impl<'a> From<Table<'a>> for Value<'a>

Source§

fn from(value: Table<'a>) -> Value<'a>

Converts to this type from the input type.
Source§

impl<'a, K, V> FromIterator<(K, V)> for Table<'a>
where Key<'a>: From<K>, Value<'a>: From<V>,

Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = (K, V)>,

Creates a value from an iterator. Read more
Source§

impl<'a, K> Index<K> for Table<'a>
where Key<'a>: From<K>,

Source§

type Output = Value<'a>

The returned type after indexing.
Source§

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

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, K> IndexMut<K> for Table<'a>
where Key<'a>: From<K>,

Source§

fn index_mut(&mut self, index: K) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, 'b> IntoIterator for &'b Table<'a>

Source§

type Item = (&'b Key<'a>, &'b Value<'a>)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, 'b>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, 'b> IntoIterator for &'b mut Table<'a>

Source§

type Item = (&'b Key<'a>, &'b mut Value<'a>)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, 'b>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a> IntoIterator for Table<'a>

Source§

type Item = (Key<'a>, Value<'a>)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a> PartialEq for Table<'a>

Source§

fn eq(&self, other: &Table<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Serialize for Table<'a>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<'a> TryAsMut<Table<'a>> for Value<'a>

Source§

fn try_as_mut(&mut self) -> Option<&mut Table<'a>>

Attempt to unwrap the inner reference.
Source§

impl<'a> TryAsRef<Table<'a>> for Value<'a>

Source§

fn try_as_ref(&self) -> Option<&Table<'a>>

Attempt to unwrap the inner reference.
Source§

impl<'a> StructuralPartialEq for Table<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Table<'a>

§

impl<'a> RefUnwindSafe for Table<'a>

§

impl<'a> Send for Table<'a>

§

impl<'a> Sync for Table<'a>

§

impl<'a> Unpin for Table<'a>

§

impl<'a> UnwindSafe for Table<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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>,

Source§

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>,

Source§

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.