enum_dispatch

main
Shad Amethyst 2 years ago
parent f0a4f8b0f6
commit 30c92e6647
Signed by: amethyst
GPG Key ID: D970C8DD1D6DEE36

@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
dyn-clone = "1.0"
palette = "0.6"
enum_dispatch = "0.3"
[dev-dependencies]
colored = "2.0"

@ -4,16 +4,19 @@ use stackline::tile::*;
fn benchmark_step(c: &mut Criterion) {
c.bench_function("Pane::step", |b| {
let mut pane = Pane::empty(3, 3).unwrap();
let mut pane = Pane::empty(4, 4).unwrap();
pane.set_tile((0, 0), Diode::new(Direction::Right));
pane.set_tile((2, 0), Diode::new(Direction::Down));
pane.set_tile((2, 2), Diode::new(Direction::Left));
pane.set_tile((0, 2), Diode::new(Direction::Up));
pane.set_tile((1, 0), Wire::new(Orientation::Horizontal));
pane.set_tile((1, 2), Wire::new(Orientation::Horizontal));
pane.set_tile((0, 1), Wire::new(Orientation::Vertical));
pane.set_tile((2, 1), Wire::new(Orientation::Vertical));
pane.set_tile((3, 0), Diode::new(Direction::Down));
pane.set_tile((3, 3), Diode::new(Direction::Left));
pane.set_tile((0, 3), Diode::new(Direction::Up));
for n in 1..3 {
pane.set_tile((n, 0), Wire::new(Orientation::Horizontal));
pane.set_tile((n, 3), Wire::new(Orientation::Horizontal));
pane.set_tile((0, n), Wire::new(Orientation::Vertical));
pane.set_tile((3, n), Wire::new(Orientation::Vertical));
}
pane.set_signal((0, 0), stackline::signal!(
(0, 0),
@ -21,6 +24,24 @@ fn benchmark_step(c: &mut Criterion) {
[]
));
pane.set_signal((3, 0), stackline::signal!(
(3, 0),
Direction::Down,
[]
));
pane.set_signal((3, 3), stackline::signal!(
(3, 3),
Direction::Left,
[]
));
pane.set_signal((0, 3), stackline::signal!(
(0, 3),
Direction::Up,
[]
));
b.iter(|| pane.step());
});
}

@ -102,10 +102,12 @@ impl Default for FullTile {
}
}
impl<T: Tile + 'static> From<T> for FullTile {
impl<T: Tile + 'static> From<T> for FullTile
where AnyTile: From<T>
{
#[inline]
fn from(tile: T) -> Self {
Self::new(Some(AnyTile::new(tile)))
Self::new(Some(AnyTile::from(tile)))
}
}

@ -1,5 +1,6 @@
use super::*;
use dyn_clone::{clone_box, DynClone};
use enum_dispatch::enum_dispatch;
mod wire;
pub use wire::*;
@ -7,6 +8,17 @@ pub use wire::*;
mod full;
pub use full::*;
// TODO: implement a build.rs to auto-generate AnyTile
#[derive(Clone, Debug)]
#[enum_dispatch]
pub enum AnyTile {
Wire(Wire),
Diode(Diode),
Resistor(Resistor),
}
#[enum_dispatch(AnyTile)]
pub trait Tile: DynClone + std::fmt::Debug {
/// Function to be called when the tile needs to be updated.
#[inline]
@ -31,37 +43,43 @@ pub trait Tile: DynClone + std::fmt::Debug {
}
}
#[derive(Debug)]
pub struct AnyTile(Box<dyn Tile>);
impl AnyTile {
#[inline]
pub fn new<T: Tile + 'static>(tile: T) -> Self {
Self(Box::new(tile))
}
#[inline]
pub fn update<'b>(&'b mut self, ctx: UpdateContext<'b>) {
self.0.update(ctx)
}
#[inline]
pub fn accepts_signal(&self, direction: Direction) -> bool {
self.0.accepts_signal(direction)
}
#[inline]
pub fn draw(&self, x: usize, y: usize, state: State, surface: &mut TextSurface) {
self.0.draw(x, y, state, surface);
}
}
impl Clone for AnyTile {
#[inline]
fn clone(&self) -> Self {
Self(clone_box(self.0.as_ref()))
}
}
// #[derive(Debug)]
// pub struct AnyTile(Box<dyn Tile>);
// impl AnyTile {
// #[inline]
// pub fn new<T: Tile + 'static>(tile: T) -> Self {
// Self(Box::new(tile))
// }
// #[inline]
// pub fn update<'b>(&'b mut self, ctx: UpdateContext<'b>) {
// self.0.update(ctx)
// }
// #[inline]
// pub fn accepts_signal(&self, direction: Direction) -> bool {
// self.0.accepts_signal(direction)
// }
// #[inline]
// pub fn draw(&self, x: usize, y: usize, state: State, surface: &mut TextSurface) {
// self.0.draw(x, y, state, surface);
// }
// }
// impl Clone for AnyTile {
// #[inline]
// fn clone(&self) -> Self {
// Self(clone_box(self.0.as_ref()))
// }
// }
// impl<T: Tile + 'static> From<T> for AnyTile {
// fn from(tile: T) -> AnyTile {
// AnyTile(Box::new(tile))
// }
// }
#[cfg(test)]
mod crate_macros {

Loading…
Cancel
Save