Replace finish with drop

This commit is contained in:
Nicolas 2022-04-09 18:37:50 +02:00
parent 71e531a276
commit 8b551c3d1c
3 changed files with 7 additions and 43 deletions

View File

@ -9,7 +9,6 @@ use mockall::automock;
pub trait Renderer {
fn render(&mut self, step: f32);
fn present(&mut self) -> Result<(), Error>;
fn finish(&mut self) -> Result<(), Error>;
}
pub struct SamplerRenderer<TSurface, TSampler> {
@ -42,10 +41,6 @@ impl<T1: Surface, T2: Sampler> Renderer for SamplerRenderer<T1, T2> {
fn present(&mut self) -> Result<(), Error> {
self.surface.present()
}
fn finish(&mut self) -> Result<(), Error> {
self.surface.finish()
}
}
#[cfg(test)]
@ -91,16 +86,4 @@ mod test {
renderer.present().unwrap();
}
#[test]
fn finish() {
let mut surface = MockSurface::new();
let sampler = MockSampler::new();
surface.expect_finish().once().returning(|| Ok(()));
let mut renderer = SamplerRenderer::new(surface, sampler);
renderer.finish().unwrap();
}
}

View File

@ -26,7 +26,7 @@ impl<T1: Timer, T2: Renderer> Runner<T1, T2> {
self.renderer.present()?;
self.timer.sleep();
}
self.renderer.finish()
Ok(())
}
}
@ -59,8 +59,6 @@ mod test {
renderer.expect_present().once().in_sequence(seq).returning(|| Ok(()));
timer.expect_sleep().once().in_sequence(seq).return_const(());
renderer.expect_finish().once().in_sequence(seq).returning(|| Ok(()));
let runner = Runner::new(Duration::from_secs(4), timer, renderer);
runner.run().unwrap();

View File

@ -14,7 +14,6 @@ pub trait Surface {
fn draw(&mut self, x: usize, y: usize, char: char, color: Color);
fn clear(&mut self, x: usize, y: usize);
fn present(&mut self) -> Result<(), Error>;
fn finish(&mut self) -> Result<(), Error>;
}
pub struct WriteSurface<T: Write> {
@ -89,10 +88,13 @@ impl<T: Write> Surface for WriteSurface<T> {
self.out.flush()?;
Ok(())
}
}
fn finish(&mut self) -> Result<(), Error> {
self.out.execute(Clear(ClearType::Purge))?;
Ok(())
impl<T: Write> Drop for WriteSurface<T> {
fn drop(&mut self) {
if let Err(e) = self.out.execute(Clear(ClearType::Purge)) {
println!("{}", e);
}
}
}
@ -159,25 +161,6 @@ mod test {
assert_eq!(8, renderer.height());
}
#[test]
fn finish() {
// Execute
let data = Data::new();
let mock = MockWrite::new(data.clone());
let mut renderer = WriteSurface::new(mock, 4, 4);
renderer.finish().unwrap();
// Recreate expectation
let expected = Data::new();
let mut stream = MockWrite::new(expected.clone());
stream.execute(Clear(ClearType::Purge)).unwrap();
// Compare
assert_eq!(expected, data);
}
#[test]
fn present() {
// Execute