Add finish method

This commit is contained in:
Nicolas 2022-04-09 16:46:56 +02:00
parent e4116130d9
commit 6871f75d09
2 changed files with 24 additions and 7 deletions

View File

@ -6,6 +6,7 @@ use crate::Vector;
pub trait Renderer { pub trait Renderer {
fn render(&mut self, step: f32); fn render(&mut self, step: f32);
fn present(&mut self) -> Result<(), Error>; fn present(&mut self) -> Result<(), Error>;
fn finish(&mut self) -> Result<(), Error>;
} }
pub struct SamplerRenderer { pub struct SamplerRenderer {
@ -39,6 +40,10 @@ impl Renderer for SamplerRenderer {
fn present(&mut self) -> Result<(), Error> { fn present(&mut self) -> Result<(), Error> {
self.surface.present() self.surface.present()
} }
fn finish(&mut self) -> Result<(), Error> {
self.surface.finish()
}
} }
#[cfg(test)] #[cfg(test)]
@ -84,4 +89,16 @@ mod test {
renderer.present().unwrap(); renderer.present().unwrap();
} }
#[test]
fn finish() {
let mut surface = Box::new(MockSurface::new());
let sampler = Box::new(MockSampler::new());
surface.expect_finish().once().returning(|| Ok(()));
let mut renderer = SamplerRenderer::new(surface, sampler);
renderer.finish().unwrap();
}
} }

View File

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