From 09d07018dcf75cd458358f0b4abee61fda208603 Mon Sep 17 00:00:00 2001 From: Nicolas <> Date: Sat, 9 Apr 2022 17:12:34 +0200 Subject: [PATCH] Replace dyn with generics --- src/main.rs | 8 ++++---- src/render.rs | 25 ++++++++++++------------- src/runner.rs | 12 ++++++------ 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1396e74..f4df036 100644 --- a/src/main.rs +++ b/src/main.rs @@ -83,11 +83,11 @@ fn main() -> Result<(), Error> { let color = create_color(args.color[0]); let char = Box::new(SimpleCharSampler::new(args.chars)); - let sampler = Box::new(ComposedSampler::new(animation, fill, color, char)); - let surface = Box::new(WriteSurface::new(stdout(), width, height)); + let sampler = ComposedSampler::new(animation, fill, color, char); + let surface =WriteSurface::new(stdout(), width, height); - let renderer = Box::new(SamplerRenderer::new(surface, sampler)); - let timer = Box::new(SimpleTimer::new(Duration::from_millis(1000 / 60))); + let renderer = SamplerRenderer::new(surface, sampler); + let timer = SimpleTimer::new(Duration::from_millis(1000 / 60)); let runner = Runner::new(Duration::from_secs(2), timer, renderer); runner.run() diff --git a/src/render.rs b/src/render.rs index 45329c9..6e7ae9b 100644 --- a/src/render.rs +++ b/src/render.rs @@ -9,19 +9,18 @@ pub trait Renderer { fn finish(&mut self) -> Result<(), Error>; } -pub struct SamplerRenderer { - surface: Box, - sampler: Box, +pub struct SamplerRenderer { + surface: TSurface, + sampler: TSampler, } -impl SamplerRenderer { - pub fn new(surface: Box, - sampler: Box) -> Self { +impl SamplerRenderer { + pub fn new(surface: T1, sampler: T2) -> Self { Self { surface, sampler } } } -impl Renderer for SamplerRenderer { +impl Renderer for SamplerRenderer { fn render(&mut self, step: f32) { for x in 0..self.surface.width() { for y in 0..self.surface.height() { @@ -56,8 +55,8 @@ mod test { #[test] fn render() { - let mut surface = Box::new(MockSurface::new()); - let mut sampler = Box::new(MockSampler::new()); + let mut surface = MockSurface::new(); + let mut sampler = MockSampler::new(); sampler.expect_sample().withf(|_, pos| pos.x == 0.0 && pos.y == 0.0).returning(|_,_| Sample::Clear); sampler.expect_sample().withf(|_, pos| pos.x == 1.0 && pos.y == 0.0).returning(|_,_| Sample::Keep); @@ -80,8 +79,8 @@ mod test { #[test] fn present() { - let mut surface = Box::new(MockSurface::new()); - let sampler = Box::new(MockSampler::new()); + let mut surface = MockSurface::new(); + let sampler = MockSampler::new(); surface.expect_present().once().returning(|| Ok(())); @@ -92,8 +91,8 @@ mod test { #[test] fn finish() { - let mut surface = Box::new(MockSurface::new()); - let sampler = Box::new(MockSampler::new()); + let mut surface = MockSurface::new(); + let sampler = MockSampler::new(); surface.expect_finish().once().returning(|| Ok(())); diff --git a/src/runner.rs b/src/runner.rs index 142b010..6ff01be 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -3,16 +3,16 @@ use anyhow::Error; use crate::Renderer; use crate::timer::Timer; -pub struct Runner { - timer: Box, +pub struct Runner { + timer: TTimer, ticks: u128, - renderer: Box, + renderer: TRenderer, } -impl Runner { +impl Runner { pub fn new(duration: Duration, - timer: Box, - renderer: Box) -> Self { + timer: T1, + renderer: T2) -> Self { let ticks = duration.as_nanos() / timer.delay().as_nanos(); Self { timer, ticks, renderer }