Crate gnudbm [] [src]

Gnudbm is an ergonomic, idiomatic wrapper for gdbm.

With built in support for Serde and bincode, It provides fast and easy local key/value storage of any type implementing Serialize.

For an overview of available database operations, see the documentation for RwHandle.


Opening or creating a database file with a GdbmOpener:

let db_path = PathBuf::from("path/to/my.db");
let mut db = GdbmOpener::new()
    // there are two types of database handle;
    // the other is instantiated with `GdbmOpener::readonly`
    .expect("db creation failed");

When fetching and storing, your key can be any type that implements AsRef<[u8]>; for instance one of String/&str. Values are any type that implements Serialize and Deserialize.

// 'db' is a previously configured database"my key", "an important value").unwrap();

// fetch returns an Entry, which wraps a pointer to the raw data
let entry = db.fetch("my key").unwrap();
assert_eq!(entry.as_bytes(), "my key".as_bytes());

// the data can be deserialized, borrowing if possible.
// The result is bound to the lifetime of the Entry.
let as_str: &str = entry.deserialize().unwrap();
assert_eq!(as_str, "my key");

Use a custom type with Serde:

use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct MyStruct<'a> {
    name: &'a str,
    counts: Vec<u64>,

let name: String = "Colin".into();
let value = MyStruct {
    name: &name,
    counts: vec![4, 2, 0],

// 'db' is a previously configured database"my key", &value).unwrap();

let entry = db.fetch("my key").unwrap();
let fetched: MyStruct = entry.deserialize().unwrap();




An entry in a gdbm database.


A builder used to open gdbm files.


An iterator over keys and values in a gdbm database.


A key retrieved from a gdbm database.


A readonly reference to a gdbm database.


A read/write reference to a gdbm database.



An error when interacting with a database.


An error originating in the gdbm C library.

Type Definitions


The result type for Database operations.