From 523074b1fb2e0218041b3eead4026ea4885bbb6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Thu, 4 Jul 2019 00:07:08 +0200 Subject: [PATCH 1/2] Improve `Task` consistency. - Rename `Task::new` to `Task::succeed` - Make `Task::new` lazy operation return a `coffee::Result` --- examples/counter.rs | 2 +- examples/gamepad.rs | 2 +- examples/mesh.rs | 2 +- examples/particles.rs | 4 ++-- examples/ui.rs | 2 +- src/graphics.rs | 2 +- src/lib.rs | 2 +- src/load/task.rs | 39 ++++++++++++++++++++++++++++++++------- src/ui.rs | 2 +- 9 files changed, 41 insertions(+), 16 deletions(-) diff --git a/examples/counter.rs b/examples/counter.rs index f8182ec..b520e5c 100644 --- a/examples/counter.rs +++ b/examples/counter.rs @@ -29,7 +29,7 @@ impl Game for Counter { type LoadingScreen = (); fn load(_window: &Window) -> Task { - Task::new(|| Counter { + Task::succeed(|| Counter { value: 0, increment_button: button::State::new(), decrement_button: button::State::new(), diff --git a/examples/gamepad.rs b/examples/gamepad.rs index 6a5b7ef..0159201 100644 --- a/examples/gamepad.rs +++ b/examples/gamepad.rs @@ -46,7 +46,7 @@ impl Game for GamepadExample { type LoadingScreen = (); fn load(_window: &Window) -> Task { - Task::new(|| GamepadExample { + Task::succeed(|| GamepadExample { last_event: "None".to_string(), }) } diff --git a/examples/mesh.rs b/examples/mesh.rs index 5a98416..272e5c4 100644 --- a/examples/mesh.rs +++ b/examples/mesh.rs @@ -55,7 +55,7 @@ impl Game for Example { type LoadingScreen = (); fn load(_window: &Window) -> Task { - Task::new(move || Example { + Task::succeed(move || Example { shape: ShapeOption::Rectangle, mode: ModeOption::Fill, color: Color::WHITE, diff --git a/examples/particles.rs b/examples/particles.rs index 3f77267..3fffb07 100644 --- a/examples/particles.rs +++ b/examples/particles.rs @@ -41,7 +41,7 @@ impl Particles { const CENTER_MASS: f32 = 200.0; fn generate(max_x: f32, max_y: f32) -> Task> { - Task::new(move || { + Task::succeed(move || { let rng = &mut rand::thread_rng(); (0..Self::AMOUNT) @@ -76,7 +76,7 @@ impl Game for Particles { Task::stage("Loading assets...", Self::load_palette()), Task::stage( "Showing off the loading screen for a bit...", - Task::new(|| thread::sleep(time::Duration::from_secs(2))), + Task::succeed(|| thread::sleep(time::Duration::from_secs(2))), ), ) .join() diff --git a/examples/ui.rs b/examples/ui.rs index d2d0bce..7582b45 100644 --- a/examples/ui.rs +++ b/examples/ui.rs @@ -28,7 +28,7 @@ impl Game for Tour { type LoadingScreen = (); fn load(_window: &Window) -> Task { - Task::new(|| Tour { + Task::succeed(|| Tour { steps: Steps::new(), back_button: button::State::new(), next_button: button::State::new(), diff --git a/src/graphics.rs b/src/graphics.rs index de8324b..349999b 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -52,7 +52,7 @@ //! # type LoadingScreen = (); //! # //! # fn load(window: &Window) -> Task { -//! # Task::new(|| MyGame) +//! # Task::succeed(|| MyGame) //! # } //! # //! // ... diff --git a/src/lib.rs b/src/lib.rs index b1f4680..81e34bf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,7 +54,7 @@ //! //! fn load(_window: &Window) -> Task { //! // Load your game assets here. Check out the `load` module! -//! Task::new(|| MyGame { /* ... */ }) +//! Task::succeed(|| MyGame { /* ... */ }) //! } //! //! fn draw(&mut self, frame: &mut Frame, _timer: &Timer) { diff --git a/src/load/task.rs b/src/load/task.rs index 987f156..1287df5 100644 --- a/src/load/task.rs +++ b/src/load/task.rs @@ -30,8 +30,8 @@ use crate::Result; /// /// ``` /// # use coffee::load::Task; -/// # let load_image = Task::new(|| ()); -/// # let load_texture_array = Task::new(|| ()); +/// # let load_image = Task::succeed(|| ()); +/// # let load_texture_array = Task::succeed(|| ()); /// # /// use coffee::load::Join; /// @@ -90,13 +90,13 @@ impl Task { /// } /// } /// - /// let generate_map = Task::new(Map::generate); + /// let generate_map = Task::new(|| Ok(Map::generate())); /// ``` /// /// [`Task`]: struct.Task.html pub fn new(f: F) -> Task where - F: 'static + Fn() -> T, + F: 'static + Fn() -> Result, { Task { total_work: 1, @@ -105,11 +105,36 @@ impl Task { worker.notify_progress(1); - Ok(result) + result }), } } + /// Creates a new [`Task`] from a lazy operation that cannot fail. + /// + /// ```rust + /// # use coffee::load::Task; + /// struct Map { + /// // ... + /// } + /// + /// impl Map { + /// pub fn generate() -> Map { + /// Map { /*...*/ } + /// } + /// } + /// + /// let generate_map = Task::succeed(Map::generate); + /// ``` + /// + /// [`Task`]: struct.Task.html + pub fn succeed(f: F) -> Task + where + F: 'static + Fn() -> T, + { + Task::new(move || Ok(f())) + } + /// Creates a new [`Task`] that uses a [`Gpu`]. /// /// You can use this to load and prepare graphical assets. @@ -165,13 +190,13 @@ impl Task { /// # } /// # struct TerrainAssets; /// # impl TerrainAssets { - /// # fn load() -> Task<()> { Task::new(|| ()) } + /// # fn load() -> Task<()> { Task::succeed(|| ()) } /// # } /// use coffee::load::Join; /// /// let load_game = /// ( - /// Task::stage("Generating map...", Task::new(Map::generate)), + /// Task::stage("Generating map...", Task::succeed(Map::generate)), /// Task::stage("Loading terrain...", TerrainAssets::load()) /// ) /// .join(); diff --git a/src/ui.rs b/src/ui.rs index 4404035..abae029 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -47,7 +47,7 @@ //! # type LoadingScreen = ProgressBar; //! # //! # fn load(_window: &Window) -> Task { -//! # Task::new(|| Counter { +//! # Task::succeed(|| Counter { //! # value: 0, //! # increment_button: button::State::new(), //! # decrement_button: button::State::new(), From 2193b53a443d38ba6c50b91d1f537190a13af869 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Thu, 4 Jul 2019 00:12:06 +0200 Subject: [PATCH 2/2] Update `CHANGELOG` --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 444e9be..99fb497 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,13 +5,20 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- `Task::succeed` replaces the old `Task::new`. [#66] + ### Changed - `Mesh::stroke` now takes an `f32` as `line_width` instead of a `u16`. +- `Task::new` now supports a lazy operation that can fail. [#66] ### Fixed - Incorrect buffer sizes in the `Mesh` pipeline. This caused vertices to entirely disappear when rendering big meshes, leading to a potential crash. +[#66]: https://github.com/hecrj/coffee/pull/66 + + ## [0.3.1] - 2019-06-20 ### Added - Documentation about the default coordinate system of a `Target`.