Skip to content

Commit

Permalink
Remove SpaceTransaction::set().
Browse files Browse the repository at this point in the history
The majority of its uses were replaced with `SpaceTransaction::filling()`.
  • Loading branch information
kpreid committed Nov 22, 2023
1 parent e3754eb commit 82d32f9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 59 deletions.
14 changes: 8 additions & 6 deletions all-is-cubes-ui/src/vui/widgets/toolbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use all_is_cubes::drawing::embedded_graphics::{
use all_is_cubes::inv::{Slot, TOOL_SELECTIONS};
use all_is_cubes::listen::{DirtyFlag, Gate, Listen as _, ListenableSource, Listener};
use all_is_cubes::math::{Cube, FaceMap, GridAab, GridCoordinate, GridPoint, GridVector, Gridgid};
use all_is_cubes::space::{Space, SpacePhysics, SpaceTransaction};
use all_is_cubes::space::{CubeTransaction, Space, SpacePhysics, SpaceTransaction};
use all_is_cubes::time::Duration;
use all_is_cubes::transaction::Merge as _;
use all_is_cubes::universe::{URef, Universe};
Expand Down Expand Up @@ -196,12 +196,14 @@ impl ToolbarController {
break;
}

let position = self.slot_position(index);
let cube = self.slot_position(index);
// Draw icon
txn.set(
position,
None,
Some(stack.icon(&self.definition.hud_blocks.icons).into_owned()),
txn.merge_from(
CubeTransaction::replacing(
None,
Some(stack.icon(&self.definition.hud_blocks.icons).into_owned()),
)
.at(cube),
)?;
}

Expand Down
54 changes: 1 addition & 53 deletions all-is-cubes/src/space/space_txn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,11 @@ impl SpaceTransaction {
/// transaction will fail.
/// If `new` is not [`None`], replaces the existing block with `new`.
///
/// TODO: This name is a poor name now that [`Self::set`] exists.
/// TODO: Consider replacing all uses of this with `CubeTransaction::replacing()`.
pub fn set_cube(cube: impl Into<Cube>, old: Option<Block>, new: Option<Block>) -> Self {
CubeTransaction::replacing(old, new).at(cube.into())
}

/// Expand this transaction to include modifying the given cube, or return an error if
/// that would conflict (by the same definition as transaction merging).
///
/// If `old` is not [`None`], requires that the existing block is that block or the
/// transaction will fail.
/// If `new` is not [`None`], replaces the existing block with `new`.
pub fn set(
&mut self,
cube: impl Into<Cube>,
old: Option<Block>,
new: Option<Block>,
) -> Result<(), SpaceTransactionConflict> {
let cube: Cube = cube.into();
self.at(cube)
.merge_from(CubeTransaction::replacing(old, new))
.map_err(|conflict| SpaceTransactionConflict::Cube { cube, conflict })
}

/// Provides an [`DrawTarget`](embedded_graphics::prelude::DrawTarget)
/// adapter for 2.5D drawing.
///
Expand Down Expand Up @@ -653,40 +635,6 @@ mod tests {
.unwrap_err();
}

#[test]
fn set_cube_mutate_equivalent_to_merge() {
let [b1, b2, b3] = make_some_blocks();

// A basic single-cube transaction is the same either way.
let mut t = SpaceTransaction::default();
t.set([0, 0, 0], Some(b1.clone()), Some(b2.clone()))
.unwrap();
assert_eq!(
t,
SpaceTransaction::set_cube([0, 0, 0], Some(b1.clone()), Some(b2.clone())),
);

// Two-cube transaction.
let prev = t.clone();
t.set([1, 0, 0], Some(b2.clone()), Some(b3.clone()))
.unwrap();
assert_eq!(
t,
prev.merge(SpaceTransaction::set_cube(
[1, 0, 0],
Some(b2.clone()),
Some(b3.clone())
))
.unwrap(),
);

// Conflict
let prev = t.clone();
t.set([0, 0, 0], Some(b1.clone()), Some(b3.clone()))
.expect_err("should have merge failure");
assert_eq!(t, prev,);
}

#[test]
fn merge_allows_independent() {
let [b1, b2, b3] = make_some_blocks();
Expand Down

0 comments on commit 82d32f9

Please sign in to comment.