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 Dec 11, 2023
1 parent 8151d96 commit 74eb4d1
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 34 deletions.
4 changes: 2 additions & 2 deletions all-is-cubes/src/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ impl<H: BehaviorHost> BehaviorSetTransaction<H> {
}

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

#[allow(clippy::vtable_address_comparisons)] // The hazards should be okay for this use case
Expand Down Expand Up @@ -667,7 +667,7 @@ impl<H: BehaviorHost> Transaction<BehaviorSet<H>> 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 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 @@ -302,7 +302,7 @@ impl BlockDefTransaction {
}

impl Transaction<BlockDef> for BlockDefTransaction {
type CommitCheck = ();
type CommitCheck = impl fmt::Debug;
type Output = transaction::NoOutput;

fn check(
Expand Down Expand Up @@ -335,7 +335,7 @@ impl Transaction<BlockDef> 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 @@ -757,11 +757,7 @@ impl CharacterTransaction {

#[allow(clippy::type_complexity)]
impl Transaction<Character> for CharacterTransaction {
type CommitCheck = (
<BodyTransaction as Transaction<Body>>::CommitCheck,
<InventoryTransaction as Transaction<Inventory>>::CommitCheck,
<BehaviorSetTransaction<Character> as Transaction<BehaviorSet<Character>>>::CommitCheck,
);
type CommitCheck = impl fmt::Debug;
type Output = transaction::NoOutput;

fn check(&self, target: &Character) -> Result<Self::CommitCheck, PreconditionFailed> {
Expand Down Expand Up @@ -807,11 +803,7 @@ impl Transaction<Character> 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::collections::BTreeMap;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::num::NonZeroU16;
use core::fmt;

use crate::block::Block;
use crate::character::{Character, CharacterTransaction, Cursor};
Expand Down Expand Up @@ -342,13 +343,13 @@ impl InventoryTransaction {
}

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

fn check(&self, inventory: &Inventory) -> Result<Self::CommitCheck, PreconditionFailed> {
// 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 @@ -425,7 +426,7 @@ impl Transaction<Inventory> 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 @@ -696,7 +696,7 @@ impl transaction::Transactional for Body {
}

impl Transaction<Body> for BodyTransaction {
type CommitCheck = ();
type CommitCheck = impl fmt::Debug;
type Output = transaction::NoOutput;

fn check(&self, _body: &Body) -> Result<Self::CommitCheck, transaction::PreconditionFailed> {
Expand All @@ -716,7 +716,7 @@ impl Transaction<Body> 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
8 changes: 4 additions & 4 deletions all-is-cubes/src/space/space_txn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use alloc::sync::Arc;
use alloc::vec::Vec;
use core::{fmt, mem};

use crate::behavior::{self, BehaviorSet, BehaviorSetTransaction};
use crate::behavior::{self, BehaviorSetTransaction};
use crate::block::Block;
use crate::drawing::DrawingPlane;
use crate::fluff::Fluff;
Expand Down Expand Up @@ -169,8 +169,7 @@ impl SpaceTransaction {
}

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

fn check(&self, space: &Space) -> Result<Self::CommitCheck, PreconditionFailed> {
Expand Down Expand Up @@ -284,7 +283,7 @@ impl Transaction<Space> 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 @@ -498,6 +497,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 @@ -39,7 +39,7 @@ pub trait Transaction<T: ?Sized>: 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 @@ -127,7 +127,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
2 changes: 1 addition & 1 deletion all-is-cubes/src/universe/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ macro_rules! member_enums_and_impls {
) -> Result<Self::CommitCheck, transaction::PreconditionFailed> {
Ok(match self {
Self::Noop => Box::new(()),
$( Self::$member_type(t) => Box::new(t.check(&())?), )*
$( Self::$member_type(t) => Box::new(t.check(&())?) as ut::AnyTransactionCheck, )*
})
}

Expand Down
31 changes: 23 additions & 8 deletions all-is-cubes/src/universe/universe_txn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ where
check: <O::Transaction as Transaction<O>>::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 @@ -204,18 +213,20 @@ 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<
behavior::BehaviorSet<Universe>,
>>::CommitCheck,
}

/// Transaction conflict error type for [`UniverseTransaction`].
Expand Down Expand Up @@ -345,7 +356,7 @@ impl From<AnyTransaction> for UniverseTransaction {
}

impl Transaction<Universe> for UniverseTransaction {
type CommitCheck = UniverseCommitCheck;
type CommitCheck = impl fmt::Debug;
type Output = transaction::NoOutput;

fn check(&self, target: &Universe) -> Result<Self::CommitCheck, PreconditionFailed> {
Expand Down Expand Up @@ -438,7 +449,7 @@ impl Transaction<Universe> 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 @@ -466,10 +477,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 74eb4d1

Please sign in to comment.