Skip to content

Commit

Permalink
[unstable-rust] Use ATPIT in txn check types.
Browse files Browse the repository at this point in the history
  • Loading branch information
kpreid committed Aug 6, 2024
1 parent 23d0846 commit d532383
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 33 deletions.
12 changes: 8 additions & 4 deletions all-is-cubes/src/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ impl<H: BehaviorHost> BehaviorSetTransaction<H> {

impl<H: BehaviorHost> Transaction for BehaviorSetTransaction<H> {
type Target = BehaviorSet<H>;
type CommitCheck = CommitCheck;
type CommitCheck = impl fmt::Debug;
type Output = transaction::NoOutput;
type Mismatch = BehaviorTransactionMismatch;

Expand Down Expand Up @@ -619,9 +619,11 @@ impl<H: BehaviorHost> Transaction for BehaviorSetTransaction<H> {
fn commit(
&self,
target: &mut BehaviorSet<H>,
_: 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) => {
Expand Down Expand Up @@ -677,7 +679,7 @@ impl<H: BehaviorHost> Transaction for BehaviorSetTransaction<H> {
}

impl<H: BehaviorHost> transaction::Merge for BehaviorSetTransaction<H> {
type MergeCheck = MergeCheck;
type MergeCheck = impl fmt::Debug;
type Conflict = BehaviorTransactionConflict;

fn check_merge(&self, other: &Self) -> Result<Self::MergeCheck, Self::Conflict> {
Expand All @@ -692,7 +694,9 @@ impl<H: BehaviorHost> transaction::Merge for BehaviorSetTransaction<H> {
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);
}
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes/src/block/block_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Self::MergeCheck, Self::Conflict> {
Expand Down
12 changes: 2 additions & 10 deletions all-is-cubes/src/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,11 +725,7 @@ impl CharacterTransaction {
#[allow(clippy::type_complexity)]
impl Transaction for CharacterTransaction {
type Target = Character;
type CommitCheck = (
<BodyTransaction as Transaction>::CommitCheck,
<InventoryTransaction as Transaction>::CommitCheck,
<BehaviorSetTransaction<Character> as Transaction>::CommitCheck,
);
type CommitCheck = impl fmt::Debug;
type Output = transaction::NoOutput;
type Mismatch = CharacterTransactionMismatch;

Expand Down Expand Up @@ -781,11 +777,7 @@ impl Transaction for CharacterTransaction {
}

impl Merge for CharacterTransaction {
type MergeCheck = (
<BodyTransaction as Merge>::MergeCheck,
<InventoryTransaction as Merge>::MergeCheck,
<BehaviorSetTransaction<Character> as Merge>::MergeCheck,
);
type MergeCheck = impl fmt::Debug;
type Conflict = CharacterTransactionConflict;

fn check_merge(&self, other: &Self) -> Result<Self::MergeCheck, Self::Conflict> {
Expand Down
7 changes: 4 additions & 3 deletions all-is-cubes/src/inv/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -366,14 +367,14 @@ impl InventoryTransaction {

impl Transaction for InventoryTransaction {
type Target = Inventory;
type CommitCheck = Option<InventoryCheck>;
type CommitCheck = impl fmt::Debug;
type Output = InventoryChange;
type Mismatch = InventoryMismatch;

fn check(&self, inventory: &Inventory) -> Result<Self::CommitCheck, Self::Mismatch> {
// 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::<InventoryCheck>);
}

// The simplest bulletproof algorithm to ensure we're stacking everything right
Expand Down Expand Up @@ -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<Self::MergeCheck, Self::Conflict> {
Expand Down
1 change: 1 addition & 0 deletions all-is-cubes/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes/src/physics/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,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;

Expand All @@ -730,7 +730,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<Self::MergeCheck, Self::Conflict> {
Expand Down
5 changes: 3 additions & 2 deletions all-is-cubes/src/space/space_txn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl SpaceTransaction {

impl Transaction for SpaceTransaction {
type Target = Space;
type CommitCheck = <BehaviorSetTransaction<Space> as Transaction>::CommitCheck;
type CommitCheck = impl fmt::Debug;
type Output = NoOutput;
type Mismatch = SpaceTransactionMismatch;

Expand Down Expand Up @@ -298,7 +298,7 @@ impl Transaction for SpaceTransaction {
}

impl Merge for SpaceTransaction {
type MergeCheck = <BehaviorSetTransaction<Space> as Merge>::MergeCheck;
type MergeCheck = impl fmt::Debug;
type Conflict = SpaceTransactionConflict;

fn check_merge(&self, other: &Self) -> Result<Self::MergeCheck, Self::Conflict> {
Expand Down Expand Up @@ -566,6 +566,7 @@ impl CubeTransaction {
}

impl Merge for CubeTransaction {
/// Not opaque because [`SpaceTransaction`] uses it
type MergeCheck = CubeMergeCheck;
type Conflict = CubeConflict;

Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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.
Expand Down Expand Up @@ -144,7 +144,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.
///
Expand Down
29 changes: 21 additions & 8 deletions all-is-cubes/src/universe/universe_txn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ where
check: <O::Transaction as Transaction>::CommitCheck,
}

impl<O> fmt::Debug for TransactionInUniverseCheck<O>
where
O: Transactional + 'static,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.check.fmt(f)
}
}

impl<O> Merge for TransactionInUniverse<O>
where
O: Transactional + 'static,
Expand Down Expand Up @@ -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<Name, MemberMergeCheck>,
behaviors: behavior::MergeCheck,
behaviors: <behavior::BehaviorSetTransaction<Universe> as Merge>::MergeCheck,
}
#[doc(hidden)] // Almost certainly will never need to be used explicitly
#[derive(Debug)]
pub struct UniverseCommitCheck {
struct UniverseCommitCheck {
members: HbHashMap<Name, MemberCommitCheck>,
anonymous_insertions: Vec<MemberCommitCheck>,
behaviors: behavior::CommitCheck,
behaviors: <behavior::BehaviorSetTransaction<Universe> as Transaction>::CommitCheck,
}

/// Transaction precondition error type for [`UniverseTransaction`].
Expand Down Expand Up @@ -467,7 +476,7 @@ impl From<AnyTransaction> for UniverseTransaction {

impl Transaction for UniverseTransaction {
type Target = Universe;
type CommitCheck = UniverseCommitCheck;
type CommitCheck = impl fmt::Debug;
type Output = transaction::NoOutput;
type Mismatch = UniverseMismatch;

Expand Down Expand Up @@ -577,7 +586,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<Self::MergeCheck, Self::Conflict> {
Expand Down Expand Up @@ -605,10 +614,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,
Expand Down

0 comments on commit d532383

Please sign in to comment.