From 7b7571014b48e53415e8e9b4b7220995d4bb2dd6 Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Mon, 11 Dec 2023 09:45:58 -0800 Subject: [PATCH] [unstable-rust] Use ATPIT in txn check types. --- all-is-cubes/src/behavior.rs | 12 ++++++---- all-is-cubes/src/block/block_def.rs | 4 ++-- all-is-cubes/src/character.rs | 12 ++-------- all-is-cubes/src/inv/inventory.rs | 7 +++--- all-is-cubes/src/lib.rs | 1 + all-is-cubes/src/physics/body.rs | 4 ++-- all-is-cubes/src/space/space_txn.rs | 5 ++-- all-is-cubes/src/transaction.rs | 4 ++-- all-is-cubes/src/universe/universe_txn.rs | 29 ++++++++++++++++------- 9 files changed, 45 insertions(+), 33 deletions(-) diff --git a/all-is-cubes/src/behavior.rs b/all-is-cubes/src/behavior.rs index 804fa5229..a09450419 100644 --- a/all-is-cubes/src/behavior.rs +++ b/all-is-cubes/src/behavior.rs @@ -575,7 +575,7 @@ impl BehaviorSetTransaction { impl Transaction for BehaviorSetTransaction { type Target = BehaviorSet; - type CommitCheck = CommitCheck; + type CommitCheck = impl fmt::Debug; type Output = transaction::NoOutput; type Mismatch = BehaviorTransactionMismatch; @@ -619,9 +619,11 @@ impl Transaction for BehaviorSetTransaction { fn commit( &self, target: &mut BehaviorSet, - _: Self::CommitCheck, + check: Self::CommitCheck, _outputs: &mut dyn FnMut(Self::Output), ) -> Result<(), transaction::CommitError> { + let CommitCheck { _private: () } = check; + for (key, replacement) in &self.replace { match &replacement.new { Some(new) => { @@ -677,7 +679,7 @@ impl Transaction for BehaviorSetTransaction { } impl transaction::Merge for BehaviorSetTransaction { - type MergeCheck = MergeCheck; + type MergeCheck = impl fmt::Debug; type Conflict = BehaviorTransactionConflict; fn check_merge(&self, other: &Self) -> Result { @@ -692,7 +694,9 @@ impl transaction::Merge for BehaviorSetTransaction { Ok(MergeCheck { _private: () }) } - fn commit_merge(&mut self, other: Self, _: Self::MergeCheck) { + fn commit_merge(&mut self, other: Self, check: Self::MergeCheck) { + let MergeCheck { _private: () } = check; + self.replace.extend(other.replace); self.insert.extend(other.insert); } diff --git a/all-is-cubes/src/block/block_def.rs b/all-is-cubes/src/block/block_def.rs index 5bb435928..5e1f33939 100644 --- a/all-is-cubes/src/block/block_def.rs +++ b/all-is-cubes/src/block/block_def.rs @@ -317,7 +317,7 @@ impl BlockDefTransaction { impl Transaction for BlockDefTransaction { type Target = BlockDef; - type CommitCheck = (); + type CommitCheck = impl fmt::Debug; type Output = transaction::NoOutput; type Mismatch = BlockDefMismatch; @@ -345,7 +345,7 @@ impl Transaction for BlockDefTransaction { } impl transaction::Merge for BlockDefTransaction { - type MergeCheck = (); + type MergeCheck = impl fmt::Debug; type Conflict = BlockDefConflict; fn check_merge(&self, other: &Self) -> Result { diff --git a/all-is-cubes/src/character.rs b/all-is-cubes/src/character.rs index 68300b23a..eb3b964d2 100644 --- a/all-is-cubes/src/character.rs +++ b/all-is-cubes/src/character.rs @@ -725,11 +725,7 @@ impl CharacterTransaction { #[allow(clippy::type_complexity)] impl Transaction for CharacterTransaction { type Target = Character; - type CommitCheck = ( - ::CommitCheck, - ::CommitCheck, - as Transaction>::CommitCheck, - ); + type CommitCheck = impl fmt::Debug; type Output = transaction::NoOutput; type Mismatch = CharacterTransactionMismatch; @@ -781,11 +777,7 @@ impl Transaction for CharacterTransaction { } impl Merge for CharacterTransaction { - type MergeCheck = ( - ::MergeCheck, - ::MergeCheck, - as Merge>::MergeCheck, - ); + type MergeCheck = impl fmt::Debug; type Conflict = CharacterTransactionConflict; fn check_merge(&self, other: &Self) -> Result { diff --git a/all-is-cubes/src/inv/inventory.rs b/all-is-cubes/src/inv/inventory.rs index 5116f5b8f..7dfa1d6d7 100644 --- a/all-is-cubes/src/inv/inventory.rs +++ b/all-is-cubes/src/inv/inventory.rs @@ -5,6 +5,7 @@ use alloc::boxed::Box; use alloc::collections::BTreeMap; use alloc::sync::Arc; use alloc::vec::Vec; +use core::fmt; use core::num::NonZeroU16; use crate::block::Block; @@ -366,14 +367,14 @@ impl InventoryTransaction { impl Transaction for InventoryTransaction { type Target = Inventory; - type CommitCheck = Option; + type CommitCheck = impl fmt::Debug; type Output = InventoryChange; type Mismatch = InventoryMismatch; fn check(&self, inventory: &Inventory) -> Result { // Don't do the expensive copy if we have one already if self.replace.is_empty() && self.insert.is_empty() { - return Ok(None); + return Ok(None::); } // The simplest bulletproof algorithm to ensure we're stacking everything right @@ -441,7 +442,7 @@ impl Transaction for InventoryTransaction { } impl Merge for InventoryTransaction { - type MergeCheck = (); + type MergeCheck = impl fmt::Debug; type Conflict = InventoryConflict; fn check_merge(&self, other: &Self) -> Result { diff --git a/all-is-cubes/src/lib.rs b/all-is-cubes/src/lib.rs index eb3614f61..7819ae084 100644 --- a/all-is-cubes/src/lib.rs +++ b/all-is-cubes/src/lib.rs @@ -1,3 +1,4 @@ +#![feature(impl_trait_in_assoc_type)] #![feature(never_type)] //! All is Cubes is a game/engine for worlds made of cubical blocks, where the blocks diff --git a/all-is-cubes/src/physics/body.rs b/all-is-cubes/src/physics/body.rs index 44a676748..049b77e24 100644 --- a/all-is-cubes/src/physics/body.rs +++ b/all-is-cubes/src/physics/body.rs @@ -711,7 +711,7 @@ impl transaction::Transactional for Body { impl Transaction for BodyTransaction { type Target = Body; - type CommitCheck = (); + type CommitCheck = impl fmt::Debug; type Output = transaction::NoOutput; type Mismatch = BodyMismatch; @@ -732,7 +732,7 @@ impl Transaction for BodyTransaction { } impl transaction::Merge for BodyTransaction { - type MergeCheck = (); + type MergeCheck = impl fmt::Debug; type Conflict = core::convert::Infallible; fn check_merge(&self, _other: &Self) -> Result { diff --git a/all-is-cubes/src/space/space_txn.rs b/all-is-cubes/src/space/space_txn.rs index a51ad4238..b6775d130 100644 --- a/all-is-cubes/src/space/space_txn.rs +++ b/all-is-cubes/src/space/space_txn.rs @@ -173,7 +173,7 @@ impl SpaceTransaction { impl Transaction for SpaceTransaction { type Target = Space; - type CommitCheck = as Transaction>::CommitCheck; + type CommitCheck = impl fmt::Debug; type Output = NoOutput; type Mismatch = SpaceTransactionMismatch; @@ -298,7 +298,7 @@ impl Transaction for SpaceTransaction { } impl Merge for SpaceTransaction { - type MergeCheck = as Merge>::MergeCheck; + type MergeCheck = impl fmt::Debug; type Conflict = SpaceTransactionConflict; fn check_merge(&self, other: &Self) -> Result { @@ -564,6 +564,7 @@ impl CubeTransaction { } impl Merge for CubeTransaction { + /// Not opaque because [`SpaceTransaction`] uses it type MergeCheck = CubeMergeCheck; type Conflict = CubeConflict; diff --git a/all-is-cubes/src/transaction.rs b/all-is-cubes/src/transaction.rs index 6f7780683..ec37c391a 100644 --- a/all-is-cubes/src/transaction.rs +++ b/all-is-cubes/src/transaction.rs @@ -47,7 +47,7 @@ pub trait Transaction: Merge { /// This may be used to pass precalculated values to speed up the commit phase, /// or even lock guards or similar, but also makes it slightly harder to accidentally /// call `commit` without `check`. - type CommitCheck: 'static; + type CommitCheck: fmt::Debug + 'static; /// The results of a [`Transaction::commit()`] or [`Transaction::execute()`]. /// Each commit may produce any number of these messages. @@ -153,7 +153,7 @@ pub trait Merge: Sized { /// Type of a value passed from [`Merge::check_merge`] to [`Merge::commit_merge`]. /// This may be used to pass precalculated values to speed up the merge phase, /// but also makes it difficult to accidentally merge without checking. - type MergeCheck: 'static; + type MergeCheck: fmt::Debug + 'static; /// Error type giving the reason why a merge was not possible. /// diff --git a/all-is-cubes/src/universe/universe_txn.rs b/all-is-cubes/src/universe/universe_txn.rs index bfc9e380b..19322fe96 100644 --- a/all-is-cubes/src/universe/universe_txn.rs +++ b/all-is-cubes/src/universe/universe_txn.rs @@ -87,6 +87,15 @@ where check: ::CommitCheck, } +impl fmt::Debug for TransactionInUniverseCheck +where + O: Transactional + 'static, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.check.fmt(f) + } +} + impl Merge for TransactionInUniverse where O: Transactional + 'static, @@ -208,18 +217,18 @@ pub struct UniverseTransaction { } // TODO: Benchmark cheaper HashMaps / using BTreeMap here -#[doc(hidden)] // Almost certainly will never need to be used explicitly #[derive(Debug)] +#[doc(hidden)] // Almost certainly will never need to be used explicitly pub struct UniverseMergeCheck { members: HbHashMap, - behaviors: behavior::MergeCheck, + behaviors: as Merge>::MergeCheck, } #[doc(hidden)] // Almost certainly will never need to be used explicitly #[derive(Debug)] -pub struct UniverseCommitCheck { +struct UniverseCommitCheck { members: HbHashMap, anonymous_insertions: Vec, - behaviors: behavior::CommitCheck, + behaviors: as Transaction>::CommitCheck, } /// Transaction precondition error type for [`UniverseTransaction`]. @@ -466,7 +475,7 @@ impl From for UniverseTransaction { impl Transaction for UniverseTransaction { type Target = Universe; - type CommitCheck = UniverseCommitCheck; + type CommitCheck = impl fmt::Debug; type Output = transaction::NoOutput; type Mismatch = UniverseMismatch; @@ -576,7 +585,7 @@ impl Transaction for UniverseTransaction { } impl Merge for UniverseTransaction { - type MergeCheck = UniverseMergeCheck; + type MergeCheck = impl fmt::Debug; type Conflict = UniverseConflict; fn check_merge(&self, other: &Self) -> Result { @@ -604,10 +613,14 @@ impl Merge for UniverseTransaction { behaviors, universe_id, } = self; + let UniverseMergeCheck { + members: check_members, + behaviors: check_behaviors, + } = check; - members.commit_merge(other.members, check.members); + members.commit_merge(other.members, check_members); anonymous_insertions.extend(other.anonymous_insertions); - behaviors.commit_merge(other.behaviors, check.behaviors); + behaviors.commit_merge(other.behaviors, check_behaviors); transaction::merge_option( universe_id, other.universe_id,