pub trait DatabaseRw<T: Table>: DatabaseRo<T> {
// Required methods
fn put(&mut self, key: &T::Key, value: &T::Value) -> DbResult<()>;
fn delete(&mut self, key: &T::Key) -> DbResult<()>;
fn take(&mut self, key: &T::Key) -> DbResult<T::Value>;
fn pop_first(&mut self) -> DbResult<(T::Key, T::Value)>;
fn pop_last(&mut self) -> DbResult<(T::Key, T::Value)>;
// Provided method
fn update<F>(&mut self, key: &T::Key, f: F) -> DbResult<()>
where F: FnMut(T::Value) -> Option<T::Value> { ... }
}Expand description
Database (key-value store) read/write abstraction.
All DatabaseRo functions are also callable by DatabaseRw.
Required Methods§
Sourcefn put(&mut self, key: &T::Key, value: &T::Value) -> DbResult<()>
fn put(&mut self, key: &T::Key, value: &T::Value) -> DbResult<()>
Insert a key-value pair into the database.
This will overwrite any existing key-value pairs.
§Errors
This will return crate::RuntimeError::KeyNotFound if:
- Input does not exist OR
- Database is empty
This will never RuntimeError::KeyExists.
Sourcefn delete(&mut self, key: &T::Key) -> DbResult<()>
fn delete(&mut self, key: &T::Key) -> DbResult<()>
Delete a key-value pair in the database.
This will return Ok(()) if the key does not exist.
§Errors
This will return crate::RuntimeError::KeyNotFound if:
- Input does not exist OR
- Database is empty
This will never RuntimeError::KeyExists.
Sourcefn take(&mut self, key: &T::Key) -> DbResult<T::Value>
fn take(&mut self, key: &T::Key) -> DbResult<T::Value>
Delete and return a key-value pair in the database.
This is the same as DatabaseRw::delete, however,
it will serialize the T::Value and return it.
§Errors
This will return crate::RuntimeError::KeyNotFound if:
- Input does not exist OR
- Database is empty
Provided Methods§
Sourcefn update<F>(&mut self, key: &T::Key, f: F) -> DbResult<()>
fn update<F>(&mut self, key: &T::Key, f: F) -> DbResult<()>
Fetch the value, and apply a function to it - or delete the entry.
This will call DatabaseRo::get and call your provided function f on it.
The Option f returns will dictate whether update():
-
Updates the current value OR
-
Deletes the
(key, value)pair -
If
freturnsSome(value), that will beDatabaseRw::putas the new value -
If
freturnsNone, the entry will beDatabaseRw::deleted
§Errors
This will return crate::RuntimeError::KeyNotFound if:
- Input does not exist OR
- Database is empty
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.