Skip to content

Commit

Permalink
Remove unused Tool::ExternalAction.
Browse files Browse the repository at this point in the history
It's unused, will probably be replaced in its purpose with
`Tool::Custom`, and gets in the way of deriving on the `Tool`
enum.
  • Loading branch information
kpreid committed Mar 9, 2024
1 parent d272bf8 commit 51e990a
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 67 deletions.
55 changes: 0 additions & 55 deletions all-is-cubes/src/inv/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,31 +79,9 @@ pub enum Tool {
/// Icon for the tool.
icon: Block,
},

/// A tool which calls an arbitrary function.
/// TODO: This is not used and should perhaps be folded into `Custom`
ExternalAction {
// TODO: Rework this so that the external component gets to update the icon.
// (Perhaps that's "a block defined by an external source"?)
/// Function that will be called when the tool is activated.
function: EphemeralOpaque<dyn Fn(&ToolInput) + Send + Sync>,
/// Icon for the tool.
icon: Block,
},
}

impl Tool {
#[allow(dead_code)] // TODO: This is going to be used for UI interactions but got delayed, so hide the warning
pub(crate) fn external_action<F: Fn(&ToolInput) + Send + Sync + 'static>(
icon: Block,
function: F,
) -> Self {
Tool::ExternalAction {
icon,
function: EphemeralOpaque::new(Arc::new(function)),
}
}

/// Computes the effect of using the tool.
///
/// The effect consists of both mutations to `self` and a [`UniverseTransaction`].
Expand Down Expand Up @@ -259,14 +237,6 @@ impl Tool {
}
Ok((Some(self), txn))
}
Self::ExternalAction { ref function, .. } => {
if let Some(f) = function.try_ref() {
f(input);
Ok((Some(self), UniverseTransaction::default()))
} else {
Err(ToolError::NotUsable) // TODO: communicate permanent error
}
}
}
}

Expand Down Expand Up @@ -309,7 +279,6 @@ impl Tool {
Cow::Borrowed(&predefined[Icons::Jetpack { active: *active }])
}
Self::Custom { icon, op: _ } => Cow::Borrowed(icon),
Self::ExternalAction { icon, .. } => Cow::Borrowed(icon),
}
}

Expand All @@ -327,7 +296,6 @@ impl Tool {
Tool::PushPull => One,
Tool::Jetpack { .. } => One,
Tool::Custom { .. } => One, // TODO: let tool specify
Tool::ExternalAction { .. } => One,
}
}
}
Expand All @@ -347,9 +315,6 @@ impl VisitHandles for Tool {
op.visit_handles(visitor);
icon.visit_handles(visitor);
}
Tool::ExternalAction { function: _, icon } => {
icon.visit_handles(visitor);
}
}
}
}
Expand Down Expand Up @@ -988,24 +953,4 @@ mod tests {
.bind(tester.space_handle.clone())
);
}

#[test]
fn use_external_action() {
use core::sync::atomic::{AtomicU32, Ordering};

let called = Arc::new(AtomicU32::new(0));
let tool = Tool::external_action(AIR, {
let called = called.clone();
move |_: &ToolInput| {
called.fetch_add(1, Ordering::Relaxed);
}
});

let tester = ToolTester::new(|_space| {});
assert_eq!(
tester.equip_and_use_tool(tool),
Ok(UniverseTransaction::default())
);
assert_eq!(called.load(Ordering::Relaxed), 1);
}
}
10 changes: 1 addition & 9 deletions all-is-cubes/src/save/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ mod math {

mod inv {
use super::*;
use crate::inv::{EphemeralOpaque, Inventory, Slot, Tool};
use crate::inv::{Inventory, Slot, Tool};

impl Serialize for Inventory {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
Expand Down Expand Up @@ -641,10 +641,6 @@ mod inv {
op: op.clone(),
icon: icon.clone(),
},
Tool::ExternalAction {
function: _,
ref icon,
} => schema::ToolSer::ExternalActionV1 { icon: icon.clone() },
}
.serialize(serializer)
}
Expand All @@ -665,10 +661,6 @@ mod inv {
schema::ToolSer::PushPullV1 {} => Tool::PushPull,
schema::ToolSer::JetpackV1 { active } => Tool::Jetpack { active },
schema::ToolSer::CustomV1 { op, icon } => Tool::Custom { op, icon },
schema::ToolSer::ExternalActionV1 { icon } => Tool::ExternalAction {
function: EphemeralOpaque(None),
icon,
},
})
}
}
Expand Down
3 changes: 0 additions & 3 deletions all-is-cubes/src/save/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,6 @@ pub(crate) enum ToolSer {
op: crate::op::Operation,
icon: Block,
},
ExternalActionV1 {
icon: Block,
},
}

//------------------------------------------------------------------------------------------------//
Expand Down

0 comments on commit 51e990a

Please sign in to comment.