Trait TableReadInterface

Source
pub trait TableReadInterface {
Show 13 methods // Required methods fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>>; fn keys(&self) -> Result<Vec<String>>; fn values<T: DeserializeOwned>(&self) -> Result<Vec<T>>; fn entries<T: DeserializeOwned>(&self) -> Result<Vec<(String, T)>>; fn len(&self) -> Result<usize>; fn contains_key(&self, key: &str) -> Result<bool>; fn is_empty(&self) -> Result<bool>; fn size(&self) -> Result<usize>; fn contains(&self, key: &str) -> Result<bool>; fn has(&self, key: &str) -> Result<bool>; fn get_or<T: DeserializeOwned>(&self, key: &str, default: T) -> Result<T>; fn get_or_else<T: DeserializeOwned, F: FnOnce() -> T>( &self, key: &str, default: F, ) -> Result<T>; fn get_or_default<T: DeserializeOwned + Default>( &self, key: &str, ) -> Result<T>;
}
Expand description

A trait for reading from a table

Required Methods§

Source

fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>>

Gets the value associated with the given key.

let db = Database::open("my_database.db")?;
let value1: Option<String> = db.get("key")?;
let value2 = db.get::<String>("key2")?;
println!("got values: {:?}, {:?}", value1, value2);
Source

fn keys(&self) -> Result<Vec<String>>

Gets a list of all keys in the table.

let db = Database::open("my_database.db")?;
let keys = db.keys()?;
for key in keys {
    println!("{}", key);
}
Source

fn values<T: DeserializeOwned>(&self) -> Result<Vec<T>>

Gets a list of all values in the table (that can be deserialized into the given type).

let db = Database::open("my_database.db")?;
let values = db.values::<String>()?;
for value in values {
    println!("{}", value);
}
Source

fn entries<T: DeserializeOwned>(&self) -> Result<Vec<(String, T)>>

Gets a list of all entries in the table (that can be deserialized into the given type).

let db = Database::open("my_database.db")?;
let entries = db.entries::<i32>()?;
for (key, value) in entries {
    println!("{}: {}", key, value);
}
Source

fn len(&self) -> Result<usize>

Gets the number of entries in the table.
aliases: size()

let db = Database::open("my_database.db")?;
let len = db.len()?;
println!("the default table has {} entries", len);
Source

fn contains_key(&self, key: &str) -> Result<bool>

Checks if the table contains the given key.
aliases: contains(), has()

let db = Database::open("my_database.db")?;
let contains = db.contains_key("key")?;
if contains {
    println!("the default table contains the key");
} else {
    println!("the default table does not contain the key");
}
Source

fn is_empty(&self) -> Result<bool>

Checks if the table is empty.

let db = Database::open("my_database.db")?;
let is_empty = db.is_empty()?;
if is_empty {
    println!("the default table is empty");
} else {
    println!("the default table is not empty");
}
Source

fn size(&self) -> Result<usize>

Gets the number of entries in the table.
aliases: len()

let db = Database::open("my_database.db")?;
let size = db.size()?;
println!("the default table has {} entries", size);
Source

fn contains(&self, key: &str) -> Result<bool>

Checks if the table contains the given key.
aliases: contains_key(), has()

let db = Database::open("my_database.db")?;
let contains = db.contains("key")?;
if contains {
    println!("the default table contains the key");
} else {
    println!("the default table does not contain the key");
}
Source

fn has(&self, key: &str) -> Result<bool>

Checks if the table contains the given key.
aliases: contains(), has()

let db = Database::open("my_database.db")?;
let has = db.has("key")?;
if has {
    println!("the default table contains the key");
} else {
    println!("the default table does not contain the key");
}
Source

fn get_or<T: DeserializeOwned>(&self, key: &str, default: T) -> Result<T>

Gets the value associated with the given key,
if an error occurs, or no value is found, returns the given default value.

let db = Database::open("my_database.db")?;
let value = db.get_or("my_number", 69)?;
println!("got nice number maybe: {}", value);
Source

fn get_or_else<T: DeserializeOwned, F: FnOnce() -> T>( &self, key: &str, default: F, ) -> Result<T>

Gets the value associated with the given key,
if an error occurs, or no value is found, calls the given closure and returns the result.

let db = Database::open("my_database.db")?;
let value = db.get_or_else("my_number", || {
    // some expensive calculation that needs to be done lazily
    20 + 400
})?;
println!("got not as nice number maybe: {}", value);
Source

fn get_or_default<T: DeserializeOwned + Default>(&self, key: &str) -> Result<T>

Gets the value associated with the given key,
if an error occurs, or no value is found, returns the default value for the given type. if no value is found, returns the default value for the given type.

let db = Database::open("my_database.db")?;
let value = db.get_or_default::<i32>("my_number")?;
println!("got zero maybe: {}", value);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§