Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Task consistency #66

Merged
merged 2 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion examples/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Game for Counter {
type LoadingScreen = ();

fn load(_window: &Window) -> Task<Counter> {
Task::new(|| Counter {
Task::succeed(|| Counter {
value: 0,
increment_button: button::State::new(),
decrement_button: button::State::new(),
Expand Down
2 changes: 1 addition & 1 deletion examples/gamepad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Game for GamepadExample {
type LoadingScreen = ();

fn load(_window: &Window) -> Task<GamepadExample> {
Task::new(|| GamepadExample {
Task::succeed(|| GamepadExample {
last_event: "None".to_string(),
})
}
Expand Down
2 changes: 1 addition & 1 deletion examples/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Game for Example {
type LoadingScreen = ();

fn load(_window: &Window) -> Task<Example> {
Task::new(move || Example {
Task::succeed(move || Example {
shape: ShapeOption::Rectangle,
mode: ModeOption::Fill,
color: Color::WHITE,
Expand Down
4 changes: 2 additions & 2 deletions examples/particles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Particles {
const CENTER_MASS: f32 = 200.0;

fn generate(max_x: f32, max_y: f32) -> Task<Vec<Particle>> {
Task::new(move || {
Task::succeed(move || {
let rng = &mut rand::thread_rng();

(0..Self::AMOUNT)
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion examples/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Game for Tour {
type LoadingScreen = ();

fn load(_window: &Window) -> Task<Tour> {
Task::new(|| Tour {
Task::succeed(|| Tour {
steps: Steps::new(),
back_button: button::State::new(),
next_button: button::State::new(),
Expand Down
2 changes: 1 addition & 1 deletion src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
//! # type LoadingScreen = ();
//! #
//! # fn load(window: &Window) -> Task<MyGame> {
//! # Task::new(|| MyGame)
//! # Task::succeed(|| MyGame)
//! # }
//! #
//! // ...
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
//!
//! fn load(_window: &Window) -> Task<MyGame> {
//! // 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) {
Expand Down
39 changes: 32 additions & 7 deletions src/load/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
///
Expand Down Expand Up @@ -90,13 +90,13 @@ impl<T> Task<T> {
/// }
/// }
///
/// let generate_map = Task::new(Map::generate);
/// let generate_map = Task::new(|| Ok(Map::generate()));
/// ```
///
/// [`Task`]: struct.Task.html
pub fn new<F>(f: F) -> Task<T>
where
F: 'static + Fn() -> T,
F: 'static + Fn() -> Result<T>,
{
Task {
total_work: 1,
Expand All @@ -105,11 +105,36 @@ impl<T> Task<T> {

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: F) -> Task<T>
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.
Expand Down Expand Up @@ -165,13 +190,13 @@ impl<T> Task<T> {
/// # }
/// # 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();
Expand Down
2 changes: 1 addition & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
//! # type LoadingScreen = ProgressBar;
//! #
//! # fn load(_window: &Window) -> Task<Counter> {
//! # Task::new(|| Counter {
//! # Task::succeed(|| Counter {
//! # value: 0,
//! # increment_button: button::State::new(),
//! # decrement_button: button::State::new(),
Expand Down