Skip to content

Commit 91d8636

Browse files
authored
feat: db connect trait (#62)
1 parent dd662fd commit 91d8636

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/connect.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use core::convert::Infallible;
2+
use revm::{Database, DatabaseCommit};
3+
4+
/// Trait for types that can be used to connect to a database. Connectors
5+
/// should contain configuration information like filesystem paths. They are
6+
/// intended to enable parallel instantiation of multiple EVMs in multiple
7+
/// threads sharing some database configuration
8+
pub trait DbConnect: Sync {
9+
/// The database type returned when connecting.
10+
type Database: Database + DatabaseCommit;
11+
12+
/// The error type returned when connecting to the database.
13+
type Error: core::error::Error;
14+
15+
/// Connect to the database.
16+
fn connect(&self) -> Result<Self::Database, Self::Error>;
17+
}
18+
19+
impl<Db> DbConnect for Db
20+
where
21+
Db: Database + DatabaseCommit + Clone + Sync,
22+
{
23+
type Database = Self;
24+
25+
type Error = Infallible;
26+
27+
fn connect(&self) -> Result<Self::Database, Self::Error> {
28+
Ok(self.clone())
29+
}
30+
}

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,9 @@
363363

364364
extern crate alloc;
365365

366+
mod connect;
367+
pub use connect::DbConnect;
368+
366369
mod driver;
367370
pub use driver::{
368371
BlockDriver, BundleDriver, ChainDriver, DriveBlockResult, DriveBundleResult, DriveChainResult,
@@ -405,7 +408,8 @@ pub mod test_utils;
405408

406409
use revm::{Database, DatabaseCommit, EvmBuilder};
407410

408-
/// Ext trait for [`EvmBuilder`] that builds a [`Trevm`].
411+
/// Ext trait for [`EvmBuilder`] that builds a [`Trevm`], and adds features for
412+
/// [`DbConnect`].
409413
pub trait TrevmBuilder<'a, Ext, Db: Database + DatabaseCommit> {
410414
/// Builds the [`Trevm`].
411415
fn build_trevm(self) -> EvmNeedsCfg<'a, Ext, Db>;

0 commit comments

Comments
 (0)