redb_bincode/
database.rs

1use std::path::Path;
2
3use redb::{DatabaseError, TransactionError};
4
5use super::tx::{ReadTransaction, WriteTransaction};
6use crate::tx;
7
8#[derive(Debug)]
9pub struct Database(redb::Database);
10
11impl Database {
12    pub fn create(path: impl AsRef<Path>) -> Result<Database, DatabaseError> {
13        Ok(Self(redb::Database::create(path)?))
14    }
15
16    pub fn open(path: impl AsRef<Path>) -> Result<Database, DatabaseError> {
17        Ok(Self(redb::Database::open(path)?))
18    }
19
20    pub fn begin_read(&self) -> Result<tx::ReadTransaction, TransactionError> {
21        Ok(ReadTransaction::from(self.0.begin_read()?))
22    }
23
24    pub fn begin_write(&self) -> Result<tx::WriteTransaction, TransactionError> {
25        Ok(WriteTransaction::from(self.0.begin_write()?))
26    }
27
28    /// Get the inner [`redb::Database`]
29    pub fn as_raw(&self) -> &redb::Database {
30        &self.0
31    }
32    pub fn as_raw_mut(&mut self) -> &mut redb::Database {
33        &mut self.0
34    }
35}
36
37impl From<redb::Database> for Database {
38    fn from(value: redb::Database) -> Self {
39        Self(value)
40    }
41}