Replace dyn with generics

This commit is contained in:
Nicolas 2022-04-09 17:12:34 +02:00
parent 25da606322
commit 09d07018dc
3 changed files with 22 additions and 23 deletions

View File

@ -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()

View File

@ -9,19 +9,18 @@ pub trait Renderer {
fn finish(&mut self) -> Result<(), Error>;
}
pub struct SamplerRenderer {
surface: Box<dyn Surface>,
sampler: Box<dyn Sampler>,
pub struct SamplerRenderer<TSurface, TSampler> {
surface: TSurface,
sampler: TSampler,
}
impl SamplerRenderer {
pub fn new(surface: Box<dyn Surface>,
sampler: Box<dyn Sampler>) -> Self {
impl<T1, T2> SamplerRenderer<T1, T2> {
pub fn new(surface: T1, sampler: T2) -> Self {
Self { surface, sampler }
}
}
impl Renderer for SamplerRenderer {
impl<T1: Surface, T2: Sampler> Renderer for SamplerRenderer<T1, T2> {
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(()));

View File

@ -3,16 +3,16 @@ use anyhow::Error;
use crate::Renderer;
use crate::timer::Timer;
pub struct Runner {
timer: Box<dyn Timer>,
pub struct Runner<TTimer, TRenderer> {
timer: TTimer,
ticks: u128,
renderer: Box<dyn Renderer>,
renderer: TRenderer,
}
impl Runner {
impl<T1: Timer, T2: Renderer> Runner<T1, T2> {
pub fn new(duration: Duration,
timer: Box<dyn Timer>,
renderer: Box<dyn Renderer>) -> Self {
timer: T1,
renderer: T2) -> Self {
let ticks = duration.as_nanos() / timer.delay().as_nanos();
Self { timer, ticks, renderer }