Struct Db

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

The database object. All interraction with mhdb databases is done through this object.

See crate documentation for examples.

Implementations§

Source§

impl Db

Source

pub fn open(name: &str) -> Result<Db, Error>

Open a database, creating the files <name>.dir and <name>.pag unless they already exist.

§Errors

open may return an error if an I/O error is encountered.

Source

pub fn with_sources( pagsrc: Box<dyn Source>, dirsrc: Box<dyn Source>, ) -> Result<Db, Error>

Return a database using the provided sources for storing database content.

Any type implementing Source can be provided as source.

Source

pub fn store(&mut self, key: impl Datum, val: impl Datum) -> Result<(), Error>

Store a key in the database.

Any type implementing Datum may be provided as key and val. Datum is implemented on any type implementing Serialize and Deserialize.

§Errors

If converting the key or value to bytes fails, or if the key-value pair is larger than 506B, an error variant will be returned.

Source

pub fn fetch<T: Datum>(&mut self, key: impl Datum) -> Result<Option<T>, Error>

Fetch a key from the database. If the key doesn’t exist None is returned instead.

The value is deserialized before it is returned. The database doesn’t remember types, and apart from checking that deserialization doesn’t fail, no type checking is performed.

The following two examples may therefore return a value, without errors, but only one would be correct:

let key = "myint".to_owned();
let num: Option<u64> = db.fetch(key)?;
let key = "myint".to_owned();
let num: Option<f64> = db.fetch(key)?;
§Errors

If converting the key to bytes fails, an error variant will be returned.

Source

pub fn delete(&mut self, key: impl Datum) -> Result<bool, Error>

Delete a key from the database.

If the key doesn’t exist false is returned.

§Errors

If converting the key to bytes fails, an error variant will be returned.

Source

pub fn contains(&mut self, key: impl Datum) -> Result<bool, Error>

Check if a key exists in the database.

Auto Trait Implementations§

§

impl Freeze for Db

§

impl !RefUnwindSafe for Db

§

impl Send for Db

§

impl Sync for Db

§

impl Unpin for Db

§

impl !UnwindSafe for Db

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.