pub trait TableWriteInterface {
// Required methods
fn insert<T: Serialize>(&mut self, key: &str, value: &T) -> Result<()>;
fn remove(&mut self, key: &str) -> Result<()>;
fn clear(&mut self) -> Result<()>;
fn set<T: Serialize>(&mut self, key: &str, value: &T) -> Result<()>;
fn delete(&mut self, key: &str) -> Result<()>;
fn reset(&mut self) -> Result<()>;
fn get_or_insert<T: Serialize + DeserializeOwned>(
&mut self,
key: &str,
default: T,
) -> Result<T>;
fn get_or_insert_with<T: Serialize + DeserializeOwned, F: FnOnce() -> T>(
&mut self,
key: &str,
default: F,
) -> Result<T>;
fn get_or_insert_default<T: Serialize + DeserializeOwned + Default>(
&mut self,
key: &str,
) -> Result<T>;
}
Expand description
A trait for writing to a table
Required Methods§
Sourcefn insert<T: Serialize>(&mut self, key: &str, value: &T) -> Result<()>
fn insert<T: Serialize>(&mut self, key: &str, value: &T) -> Result<()>
Inserts a value into the table with the given key.
aliases: set()
let mut db = Database::open("my_database.db")?;
db.insert("key", &"value")?;
db.insert("key2", &1234)?;
db.insert("key3", &true)?;
Sourcefn remove(&mut self, key: &str) -> Result<()>
fn remove(&mut self, key: &str) -> Result<()>
Removes the value associated with the given key.
aliases: delete()
let mut db = Database::open("my_database.db")?;
db.remove("key")?;
assert!(!db.contains_key("key")?);
Sourcefn clear(&mut self) -> Result<()>
fn clear(&mut self) -> Result<()>
Clears the table.
aliases: reset()
let mut db = Database::open("my_database.db")?;
db.clear()?;
assert!(db.is_empty()?);
Sourcefn set<T: Serialize>(&mut self, key: &str, value: &T) -> Result<()>
fn set<T: Serialize>(&mut self, key: &str, value: &T) -> Result<()>
Inserts a value into the table with the given key.
aliases: insert()
let mut db = Database::open("my_database.db")?;
db.set("key", &"value")?;
db.set("key2", &1234)?;
db.set("key3", &true)?;
Sourcefn delete(&mut self, key: &str) -> Result<()>
fn delete(&mut self, key: &str) -> Result<()>
Removes the value associated with the given key.
aliases: remove()
let mut db = Database::open("my_database.db")?;
db.delete("key")?;
assert!(!db.contains_key("key")?);
Sourcefn reset(&mut self) -> Result<()>
fn reset(&mut self) -> Result<()>
Clears the table.
aliases: clear()
let mut db = Database::open("my_database.db")?;
db.reset()?;
assert!(db.is_empty()?);
Sourcefn get_or_insert<T: Serialize + DeserializeOwned>(
&mut self,
key: &str,
default: T,
) -> Result<T>
fn get_or_insert<T: Serialize + DeserializeOwned>( &mut self, key: &str, default: T, ) -> Result<T>
Gets the value associated with the given key,
if the no value is found, inserts the given default value into the table and returns it.
let mut db = Database::open("my_database.db")?;
let value = db.get_or_insert("key", "default".to_owned())?;
println!("got default maybe: {}", value);
assert!(db.contains_key("key")?);
Sourcefn get_or_insert_with<T: Serialize + DeserializeOwned, F: FnOnce() -> T>(
&mut self,
key: &str,
default: F,
) -> Result<T>
fn get_or_insert_with<T: Serialize + DeserializeOwned, F: FnOnce() -> T>( &mut self, key: &str, default: F, ) -> Result<T>
Gets the value associated with the given key,
if the no value is found, inserts the result of the given closure into the table and returns it.
let mut db = Database::open("my_database.db")?;
let value = db.get_or_insert_with("key", || {
// some expensive calculation that needs to be done lazily
"Hello, world!".to_owned()
})?;
println!("got hello maybe: {}", value);
assert!(db.contains_key("key")?);
Sourcefn get_or_insert_default<T: Serialize + DeserializeOwned + Default>(
&mut self,
key: &str,
) -> Result<T>
fn get_or_insert_default<T: Serialize + DeserializeOwned + Default>( &mut self, key: &str, ) -> Result<T>
Gets the value associated with the given key,
if the no value is found, inserts the default value for the given type into the table and returns it.
let mut db = Database::open("my_database.db")?;
let value = db.get_or_insert_default::<i32>("key")?;
println!("got zero maybe: {}", value);
assert!(db.contains_key("key")?);
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.