Struct Database

Source
pub struct Database { /* private fields */ }
Expand description

A Database

Implementations§

Source§

impl Database

Source

pub fn open(path: impl AsRef<Path>) -> Result<Self>

Opens a file at the given path and uses it as the database.
If the file doesn’t exist, it will be created.

let db = Database::open("my_database.db")?;
Source

pub fn in_memory() -> Result<Self>

Opens an in-memory database.
Useful for tests and as a stub for a database that doesn’t need to be saved to disk.

let db = Database::in_memory()?;
Source

pub fn close(self)

Closes the databas

let db = Database::open("my_database.db")?;
db.close();
// why ?
Source

pub fn table<'a>(&'a self, name: &'a str) -> Table<'a>

Get a read-only handle to a table with the given name.

let db = Database::open("my_database.db")?;
let value = db.table("my_table").get("key")?;
Source

pub fn table_mut<'a>(&'a mut self, name: &'a str) -> TableMut<'a>

Get a read-write handle to a table with the given name.

let mut db = Database::open("my_database.db")?;
db.table_mut("my_table").set("key", &"value")?;
Source

pub fn list_tables(&self) -> Result<Vec<String>>

Returns a list of the names of all tables in the database.

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

pub fn delete_table(&mut self, name: &str) -> Result<()>

Deletes a table from the database.

let mut db = Database::open("my_database.db")?;
db.delete_table("my_table")?;
Source

pub fn len_all_tables(&self) -> Result<usize>

Returns the number of entries in all tables in the database.
aliases: size_all_tables()

let db = Database::open("my_database.db")?;
let len = db.len_all_tables()?;
println!("the database has {} entries across all tables", len);
Source

pub fn size_all_tables(&self) -> Result<usize>

Returns the number of entries in all tables in the database.
aliases: len_all_tables()

let db = Database::open("my_database.db")?;
let size = db.size_all_tables()?;
println!("the database has {} entries across all tables", size);
Source

pub fn delete_all_tables(&mut self) -> Result<()>

Deletes all tables in the database. \

let mut db = Database::open("my_database.db")?;
db.delete_all_tables()?;
assert!(db.list_tables()?.is_empty());

Trait Implementations§

Source§

impl TableReadInterface for Database

Source§

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

Gets the value associated with the given key. Read more
Source§

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

Gets a list of all keys in the table. Read more
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). Read more
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). Read more
Source§

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

Gets the number of entries in the table.
aliases: size() Read more
Source§

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

Checks if the table is empty. Read more
Source§

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

Checks if the table contains the given key.
aliases: contains(), has() Read more
Source§

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

Gets the number of entries in the table.
aliases: len() Read more
Source§

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

Checks if the table contains the given key.
aliases: contains_key(), has() Read more
Source§

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

Checks if the table contains the given key.
aliases: contains(), has() Read more
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. Read more
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. Read more
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. Read more
Source§

impl TableWriteInterface for Database

Source§

fn insert<T: Serialize>(&mut self, key: &str, value: &T) -> Result<()>

Inserts a value into the table with the given key.
aliases: set() Read more
Source§

fn remove(&mut self, key: &str) -> Result<()>

Removes the value associated with the given key.
aliases: delete() Read more
Source§

fn clear(&mut self) -> Result<()>

Clears the table.
aliases: reset() Read more
Source§

fn set<T: Serialize>(&mut self, key: &str, value: &T) -> Result<()>

Inserts a value into the table with the given key.
aliases: insert() Read more
Source§

fn delete(&mut self, key: &str) -> Result<()>

Removes the value associated with the given key.
aliases: remove() Read more
Source§

fn reset(&mut self) -> Result<()>

Clears the table.
aliases: clear() Read more
Source§

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. Read more
Source§

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. Read more
Source§

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. Read more

Auto Trait Implementations§

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