mirror of
https://github.com/ricoriedel/wipe.git
synced 2024-11-23 00:16:38 +00:00
Replace dyn with generics
This commit is contained in:
parent
25da606322
commit
09d07018dc
@ -83,11 +83,11 @@ fn main() -> Result<(), Error> {
|
|||||||
let color = create_color(args.color[0]);
|
let color = create_color(args.color[0]);
|
||||||
let char = Box::new(SimpleCharSampler::new(args.chars));
|
let char = Box::new(SimpleCharSampler::new(args.chars));
|
||||||
|
|
||||||
let sampler = Box::new(ComposedSampler::new(animation, fill, color, char));
|
let sampler = ComposedSampler::new(animation, fill, color, char);
|
||||||
let surface = Box::new(WriteSurface::new(stdout(), width, height));
|
let surface =WriteSurface::new(stdout(), width, height);
|
||||||
|
|
||||||
let renderer = Box::new(SamplerRenderer::new(surface, sampler));
|
let renderer = SamplerRenderer::new(surface, sampler);
|
||||||
let timer = Box::new(SimpleTimer::new(Duration::from_millis(1000 / 60)));
|
let timer = SimpleTimer::new(Duration::from_millis(1000 / 60));
|
||||||
let runner = Runner::new(Duration::from_secs(2), timer, renderer);
|
let runner = Runner::new(Duration::from_secs(2), timer, renderer);
|
||||||
|
|
||||||
runner.run()
|
runner.run()
|
||||||
|
@ -9,19 +9,18 @@ pub trait Renderer {
|
|||||||
fn finish(&mut self) -> Result<(), Error>;
|
fn finish(&mut self) -> Result<(), Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SamplerRenderer {
|
pub struct SamplerRenderer<TSurface, TSampler> {
|
||||||
surface: Box<dyn Surface>,
|
surface: TSurface,
|
||||||
sampler: Box<dyn Sampler>,
|
sampler: TSampler,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SamplerRenderer {
|
impl<T1, T2> SamplerRenderer<T1, T2> {
|
||||||
pub fn new(surface: Box<dyn Surface>,
|
pub fn new(surface: T1, sampler: T2) -> Self {
|
||||||
sampler: Box<dyn Sampler>) -> Self {
|
|
||||||
Self { surface, sampler }
|
Self { surface, sampler }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Renderer for SamplerRenderer {
|
impl<T1: Surface, T2: Sampler> Renderer for SamplerRenderer<T1, T2> {
|
||||||
fn render(&mut self, step: f32) {
|
fn render(&mut self, step: f32) {
|
||||||
for x in 0..self.surface.width() {
|
for x in 0..self.surface.width() {
|
||||||
for y in 0..self.surface.height() {
|
for y in 0..self.surface.height() {
|
||||||
@ -56,8 +55,8 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn render() {
|
fn render() {
|
||||||
let mut surface = Box::new(MockSurface::new());
|
let mut surface = MockSurface::new();
|
||||||
let mut sampler = Box::new(MockSampler::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 == 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);
|
sampler.expect_sample().withf(|_, pos| pos.x == 1.0 && pos.y == 0.0).returning(|_,_| Sample::Keep);
|
||||||
@ -80,8 +79,8 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn present() {
|
fn present() {
|
||||||
let mut surface = Box::new(MockSurface::new());
|
let mut surface = MockSurface::new();
|
||||||
let sampler = Box::new(MockSampler::new());
|
let sampler = MockSampler::new();
|
||||||
|
|
||||||
surface.expect_present().once().returning(|| Ok(()));
|
surface.expect_present().once().returning(|| Ok(()));
|
||||||
|
|
||||||
@ -92,8 +91,8 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn finish() {
|
fn finish() {
|
||||||
let mut surface = Box::new(MockSurface::new());
|
let mut surface = MockSurface::new();
|
||||||
let sampler = Box::new(MockSampler::new());
|
let sampler = MockSampler::new();
|
||||||
|
|
||||||
surface.expect_finish().once().returning(|| Ok(()));
|
surface.expect_finish().once().returning(|| Ok(()));
|
||||||
|
|
||||||
|
@ -3,16 +3,16 @@ use anyhow::Error;
|
|||||||
use crate::Renderer;
|
use crate::Renderer;
|
||||||
use crate::timer::Timer;
|
use crate::timer::Timer;
|
||||||
|
|
||||||
pub struct Runner {
|
pub struct Runner<TTimer, TRenderer> {
|
||||||
timer: Box<dyn Timer>,
|
timer: TTimer,
|
||||||
ticks: u128,
|
ticks: u128,
|
||||||
renderer: Box<dyn Renderer>,
|
renderer: TRenderer,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Runner {
|
impl<T1: Timer, T2: Renderer> Runner<T1, T2> {
|
||||||
pub fn new(duration: Duration,
|
pub fn new(duration: Duration,
|
||||||
timer: Box<dyn Timer>,
|
timer: T1,
|
||||||
renderer: Box<dyn Renderer>) -> Self {
|
renderer: T2) -> Self {
|
||||||
let ticks = duration.as_nanos() / timer.delay().as_nanos();
|
let ticks = duration.as_nanos() / timer.delay().as_nanos();
|
||||||
|
|
||||||
Self { timer, ticks, renderer }
|
Self { timer, ticks, renderer }
|
||||||
|
Loading…
Reference in New Issue
Block a user