pub trait DatabaseRw<T: Table>: DatabaseRo<T> {
// Required methods
fn put(
&mut self,
key: &T::Key,
value: &T::Value,
) -> Result<(), RuntimeError>;
fn delete(&mut self, key: &T::Key) -> Result<(), RuntimeError>;
fn take(&mut self, key: &T::Key) -> Result<T::Value, RuntimeError>;
fn pop_first(&mut self) -> Result<(T::Key, T::Value), RuntimeError>;
fn pop_last(&mut self) -> Result<(T::Key, T::Value), RuntimeError>;
// Provided method
fn update<F>(&mut self, key: &T::Key, f: F) -> Result<(), RuntimeError>
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) -> Result<(), RuntimeError>
fn put(&mut self, key: &T::Key, value: &T::Value) -> Result<(), RuntimeError>
Insert a key-value pair into the database.
This will overwrite any existing key-value pairs.
§Errors
This will return RuntimeError::KeyNotFound
if:
- Input does not exist OR
- Database is empty
This will never RuntimeError::KeyExists
.
Sourcefn delete(&mut self, key: &T::Key) -> Result<(), RuntimeError>
fn delete(&mut self, key: &T::Key) -> Result<(), RuntimeError>
Delete a key-value pair in the database.
This will return Ok(())
if the key does not exist.
§Errors
This will return RuntimeError::KeyNotFound
if:
- Input does not exist OR
- Database is empty
This will never RuntimeError::KeyExists
.
Sourcefn take(&mut self, key: &T::Key) -> Result<T::Value, RuntimeError>
fn take(&mut self, key: &T::Key) -> Result<T::Value, RuntimeError>
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 RuntimeError::KeyNotFound
if:
- Input does not exist OR
- Database is empty
Sourcefn pop_first(&mut self) -> Result<(T::Key, T::Value), RuntimeError>
fn pop_first(&mut self) -> Result<(T::Key, T::Value), RuntimeError>
Removes and returns the first (key, value)
pair in the database.
§Errors
This will return RuntimeError::KeyNotFound
if:
- Input does not exist OR
- Database is empty
Sourcefn pop_last(&mut self) -> Result<(T::Key, T::Value), RuntimeError>
fn pop_last(&mut self) -> Result<(T::Key, T::Value), RuntimeError>
Removes and returns the last (key, value)
pair in the database.
§Errors
This will return RuntimeError::KeyNotFound
if:
- Input does not exist OR
- Database is empty
Provided Methods§
Sourcefn update<F>(&mut self, key: &T::Key, f: F) -> Result<(), RuntimeError>
fn update<F>(&mut self, key: &T::Key, f: F) -> Result<(), RuntimeError>
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
f
returnsSome(value)
, that will beDatabaseRw::put
as the new value -
If
f
returnsNone
, the entry will beDatabaseRw::delete
d
§Errors
This will return 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.