From: 志宇 Date: Sun, 11 Aug 2024 12:40:47 +0000 (+0000) Subject: revert(chain)!: rm `persit` module X-Git-Tag: v1.0.0-beta.2~7^2~2 X-Git-Url: http://internal-gitweb-vhost/script/%22https:/database/scripts/struct.CommandStringError.html?a=commitdiff_plain;h=06160574ba3997fa49449a251a8bc046fb1a96a5;p=bdk revert(chain)!: rm `persit` module --- diff --git a/crates/chain/src/lib.rs b/crates/chain/src/lib.rs index a756ab11..3fb8c0ed 100644 --- a/crates/chain/src/lib.rs +++ b/crates/chain/src/lib.rs @@ -37,8 +37,6 @@ pub use tx_data_traits::*; pub use tx_graph::TxGraph; mod chain_oracle; pub use chain_oracle::*; -mod persist; -pub use persist::*; #[doc(hidden)] pub mod example_utils; diff --git a/crates/chain/src/persist.rs b/crates/chain/src/persist.rs deleted file mode 100644 index 2ec88f63..00000000 --- a/crates/chain/src/persist.rs +++ /dev/null @@ -1,169 +0,0 @@ -use core::{ - future::Future, - ops::{Deref, DerefMut}, - pin::Pin, -}; - -use alloc::boxed::Box; - -use crate::Merge; - -/// Represents a type that contains staged changes. -pub trait Staged { - /// Type for staged changes. - type ChangeSet: Merge; - - /// Get mutable reference of staged changes. - fn staged(&mut self) -> &mut Self::ChangeSet; -} - -/// Trait that persists the type with `Db`. -/// -/// Methods of this trait should not be called directly. -pub trait PersistWith: Staged + Sized { - /// Parameters for [`PersistWith::create`]. - type CreateParams; - /// Parameters for [`PersistWith::load`]. - type LoadParams; - /// Error type of [`PersistWith::create`]. - type CreateError; - /// Error type of [`PersistWith::load`]. - type LoadError; - /// Error type of [`PersistWith::persist`]. - type PersistError; - - /// Initialize the `Db` and create `Self`. - fn create(db: &mut Db, params: Self::CreateParams) -> Result; - - /// Initialize the `Db` and load a previously-persisted `Self`. - fn load(db: &mut Db, params: Self::LoadParams) -> Result, Self::LoadError>; - - /// Persist changes to the `Db`. - fn persist( - db: &mut Db, - changeset: &::ChangeSet, - ) -> Result<(), Self::PersistError>; -} - -type FutureResult<'a, T, E> = Pin> + Send + 'a>>; - -/// Trait that persists the type with an async `Db`. -pub trait PersistAsyncWith: Staged + Sized { - /// Parameters for [`PersistAsyncWith::create`]. - type CreateParams; - /// Parameters for [`PersistAsyncWith::load`]. - type LoadParams; - /// Error type of [`PersistAsyncWith::create`]. - type CreateError; - /// Error type of [`PersistAsyncWith::load`]. - type LoadError; - /// Error type of [`PersistAsyncWith::persist`]. - type PersistError; - - /// Initialize the `Db` and create `Self`. - fn create(db: &mut Db, params: Self::CreateParams) -> FutureResult; - - /// Initialize the `Db` and load a previously-persisted `Self`. - fn load(db: &mut Db, params: Self::LoadParams) -> FutureResult, Self::LoadError>; - - /// Persist changes to the `Db`. - fn persist<'a>( - db: &'a mut Db, - changeset: &'a ::ChangeSet, - ) -> FutureResult<'a, (), Self::PersistError>; -} - -/// Represents a persisted `T`. -#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] -pub struct Persisted { - inner: T, -} - -impl Persisted { - /// Create a new persisted `T`. - pub fn create(db: &mut Db, params: T::CreateParams) -> Result - where - T: PersistWith, - { - T::create(db, params).map(|inner| Self { inner }) - } - - /// Create a new persisted `T` with async `Db`. - pub async fn create_async( - db: &mut Db, - params: T::CreateParams, - ) -> Result - where - T: PersistAsyncWith, - { - T::create(db, params).await.map(|inner| Self { inner }) - } - - /// Construct a persisted `T` from `Db`. - pub fn load(db: &mut Db, params: T::LoadParams) -> Result, T::LoadError> - where - T: PersistWith, - { - Ok(T::load(db, params)?.map(|inner| Self { inner })) - } - - /// Construct a persisted `T` from an async `Db`. - pub async fn load_async( - db: &mut Db, - params: T::LoadParams, - ) -> Result, T::LoadError> - where - T: PersistAsyncWith, - { - Ok(T::load(db, params).await?.map(|inner| Self { inner })) - } - - /// Persist staged changes of `T` into `Db`. - /// - /// If the database errors, the staged changes will not be cleared. - pub fn persist(&mut self, db: &mut Db) -> Result - where - T: PersistWith, - { - let stage = T::staged(&mut self.inner); - if stage.is_empty() { - return Ok(false); - } - T::persist(db, &*stage)?; - stage.take(); - Ok(true) - } - - /// Persist staged changes of `T` into an async `Db`. - /// - /// If the database errors, the staged changes will not be cleared. - pub async fn persist_async<'a, Db>( - &'a mut self, - db: &'a mut Db, - ) -> Result - where - T: PersistAsyncWith, - { - let stage = T::staged(&mut self.inner); - if stage.is_empty() { - return Ok(false); - } - T::persist(db, &*stage).await?; - stage.take(); - Ok(true) - } -} - -impl Deref for Persisted { - type Target = T; - - fn deref(&self) -> &Self::Target { - &self.inner - } -} - -impl DerefMut for Persisted { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.inner - } -}