Skip to content

feat: db connect trait #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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