From 2539042b71d70afd4d8f262783d441e768811ee9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 06:24:12 +0200 Subject: Remove `Drawable` and rename `State` to `Program` --- wgpu/src/widget/canvas/cache.rs | 37 +++++++------------------------------ wgpu/src/widget/canvas/drawable.rs | 36 ------------------------------------ wgpu/src/widget/canvas/program.rs | 22 ++++++++++++++++++++++ wgpu/src/widget/canvas/state.rs | 22 ---------------------- 4 files changed, 29 insertions(+), 88 deletions(-) delete mode 100644 wgpu/src/widget/canvas/drawable.rs create mode 100644 wgpu/src/widget/canvas/program.rs delete mode 100644 wgpu/src/widget/canvas/state.rs (limited to 'wgpu/src/widget/canvas') diff --git a/wgpu/src/widget/canvas/cache.rs b/wgpu/src/widget/canvas/cache.rs index 2beed0f7..03643f74 100644 --- a/wgpu/src/widget/canvas/cache.rs +++ b/wgpu/src/widget/canvas/cache.rs @@ -1,5 +1,5 @@ use crate::{ - canvas::{Drawable, Frame, Geometry}, + canvas::{Frame, Geometry}, Primitive, }; @@ -48,10 +48,11 @@ impl Cache { *self.state.borrow_mut() = State::Empty; } - pub fn draw(&self, new_bounds: Size, input: T) -> Geometry - where - T: Drawable + std::fmt::Debug, - { + pub fn draw( + &self, + new_bounds: Size, + draw_fn: impl Fn(&mut Frame), + ) -> Geometry { use std::ops::Deref; if let State::Filled { bounds, primitive } = self.state.borrow().deref() @@ -64,7 +65,7 @@ impl Cache { } let mut frame = Frame::new(new_bounds); - input.draw(&mut frame); + draw_fn(&mut frame); let primitive = { let geometry = frame.into_geometry(); @@ -79,30 +80,6 @@ impl Cache { Geometry::from_primitive(Primitive::Cached { cache: primitive }) } - - pub fn with<'a, T, Message>( - &'a self, - input: T, - ) -> impl crate::canvas::State + 'a - where - T: Drawable + std::fmt::Debug + 'a, - { - Bind { cache: self, input } - } -} - -struct Bind<'a, T> { - cache: &'a Cache, - input: T, -} - -impl<'a, T, Message> crate::canvas::State for Bind<'a, T> -where - T: Drawable + std::fmt::Debug + 'a, -{ - fn draw(&self, bounds: Size) -> Vec { - vec![self.cache.draw(bounds, &self.input)] - } } impl std::fmt::Debug for State { diff --git a/wgpu/src/widget/canvas/drawable.rs b/wgpu/src/widget/canvas/drawable.rs deleted file mode 100644 index 5209fa6f..00000000 --- a/wgpu/src/widget/canvas/drawable.rs +++ /dev/null @@ -1,36 +0,0 @@ -use crate::canvas::Frame; - -/// A type that can be drawn on a [`Frame`]. -/// -/// [`Frame`]: struct.Frame.html -pub trait Drawable { - /// Draws the [`Drawable`] on the given [`Frame`]. - /// - /// [`Drawable`]: trait.Drawable.html - /// [`Frame`]: struct.Frame.html - fn draw(&self, frame: &mut Frame); -} - -impl<'a> Drawable for dyn Fn(&mut Frame) + 'a { - fn draw(&self, frame: &mut Frame) { - self(frame) - } -} - -impl Drawable for &T -where - T: Drawable, -{ - fn draw(&self, frame: &mut Frame) { - T::draw(self, frame) - } -} - -impl Drawable for &[T] -where - T: Drawable, -{ - fn draw(&self, frame: &mut Frame) { - self.iter().for_each(|drawable| drawable.draw(frame)); - } -} diff --git a/wgpu/src/widget/canvas/program.rs b/wgpu/src/widget/canvas/program.rs new file mode 100644 index 00000000..8e35fdfb --- /dev/null +++ b/wgpu/src/widget/canvas/program.rs @@ -0,0 +1,22 @@ +use crate::canvas::{Event, Geometry, Size}; + +pub trait Program { + fn update(&mut self, _event: Event, _bounds: Size) -> Option { + None + } + + fn draw(&self, bounds: Size) -> Vec; +} + +impl Program for &mut T +where + T: Program, +{ + fn update(&mut self, event: Event, bounds: Size) -> Option { + T::update(self, event, bounds) + } + + fn draw(&self, bounds: Size) -> Vec { + T::draw(self, bounds) + } +} diff --git a/wgpu/src/widget/canvas/state.rs b/wgpu/src/widget/canvas/state.rs deleted file mode 100644 index ab433dd1..00000000 --- a/wgpu/src/widget/canvas/state.rs +++ /dev/null @@ -1,22 +0,0 @@ -use crate::canvas::{Event, Geometry, Size}; - -pub trait State { - fn update(&mut self, _event: Event, _bounds: Size) -> Option { - None - } - - fn draw(&self, bounds: Size) -> Vec; -} - -impl State for &mut T -where - T: State, -{ - fn update(&mut self, event: Event, bounds: Size) -> Option { - T::update(self, event, bounds) - } - - fn draw(&self, bounds: Size) -> Vec { - T::draw(self, bounds) - } -} -- cgit