From 1754ebe5a5f03f4e0c1d0d57fef1266beef357e1 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 8056fd16a..74e3319a4 100644 --- a/all-is-cubes/src/behavior.rs +++ b/all-is-cubes/src/behavior.rs @@ -581,7 +581,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; @@ -624,9 +624,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) => { @@ -682,7 +684,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 { @@ -697,7 +699,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 c86daec6f..6735af142 100644 --- a/all-is-cubes/src/block/block_def.rs +++ b/all-is-cubes/src/block/block_def.rs @@ -322,7 +322,7 @@ impl BlockDefTransaction { impl Transaction for BlockDefTransaction { type Target = BlockDef; - type CommitCheck = (); + type CommitCheck = impl fmt::Debug; type Output = transaction::NoOutput; type Mismatch = BlockDefMismatch; @@ -350,7 +350,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 5b135c4a4..a1fa1055f 100644 --- a/all-is-cubes/src/character.rs +++ b/all-is-cubes/src/character.rs @@ -721,11 +721,7 @@ impl CharacterTransaction { 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; @@ -777,11 +773,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 d82b22c29..e5fed5fac 100644 --- a/all-is-cubes/src/inv/inventory.rs +++ b/all-is-cubes/src/inv/inventory.rs @@ -10,6 +10,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; @@ -371,14 +372,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 @@ -446,7 +447,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 933f3020c..244c8b74d 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 dc53d2347..d989fd687 100644 --- a/all-is-cubes/src/physics/body.rs +++ b/all-is-cubes/src/physics/body.rs @@ -746,7 +746,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; @@ -767,7 +767,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 3c91b3c3f..46bd81df5 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 938f9ee55..4aa329f9b 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,