Skip to content

Commit

Permalink
feat: db connect trait (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich authored Nov 27, 2024
1 parent dd662fd commit 91d8636
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/connect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use core::convert::Infallible;
use revm::{Database, DatabaseCommit};

/// Trait for types that can be used to connect to a database. Connectors
/// should contain configuration information like filesystem paths. They are
/// intended to enable parallel instantiation of multiple EVMs in multiple
/// threads sharing some database configuration
pub trait DbConnect: Sync {
/// The database type returned when connecting.
type Database: Database + DatabaseCommit;

/// The error type returned when connecting to the database.
type Error: core::error::Error;

/// Connect to the database.
fn connect(&self) -> Result<Self::Database, Self::Error>;
}

impl<Db> DbConnect for Db
where
Db: Database + DatabaseCommit + Clone + Sync,
{
type Database = Self;

type Error = Infallible;

fn connect(&self) -> Result<Self::Database, Self::Error> {
Ok(self.clone())
}
}
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@

extern crate alloc;

mod connect;
pub use connect::DbConnect;

mod driver;
pub use driver::{
BlockDriver, BundleDriver, ChainDriver, DriveBlockResult, DriveBundleResult, DriveChainResult,
Expand Down Expand Up @@ -405,7 +408,8 @@ pub mod test_utils;

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

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

0 comments on commit 91d8636

Please sign in to comment.