diff --git a/src/connect.rs b/src/connect.rs new file mode 100644 index 0000000..39eecf3 --- /dev/null +++ b/src/connect.rs @@ -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; +} + +impl DbConnect for Db +where + Db: Database + DatabaseCommit + Clone + Sync, +{ + type Database = Self; + + type Error = Infallible; + + fn connect(&self) -> Result { + Ok(self.clone()) + } +} diff --git a/src/lib.rs b/src/lib.rs index 418addb..7e24f5e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -363,6 +363,9 @@ extern crate alloc; +mod connect; +pub use connect::DbConnect; + mod driver; pub use driver::{ BlockDriver, BundleDriver, ChainDriver, DriveBlockResult, DriveBundleResult, DriveChainResult, @@ -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>;