Skip to content

Commit

Permalink
Expose IntoUpdate trait
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jul 18, 2024
1 parent 62f2aff commit a2df2aa
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 40 deletions.
41 changes: 5 additions & 36 deletions dunge/src/el.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use {
context::Context,
state::State,
time::{Fps, Time},
update::Update,
update::{IntoUpdate, Update},
window::{self, View, WindowState},
},
std::{cell::Cell, error, fmt, ops, time::Duration},
Expand All @@ -29,7 +29,7 @@ pub type MouseButton = event::MouseButton;

pub(crate) fn run<U>(ws: WindowState<U::Event>, cx: Context, upd: U) -> Result<(), LoopError>
where
U: Update + 'static,
U: IntoUpdate + 'static,
{
#[cfg(not(target_arch = "wasm32"))]
{
Expand All @@ -45,7 +45,7 @@ where
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn run_local<U>(ws: WindowState<U::Event>, cx: Context, upd: U) -> Result<(), LoopError>
where
U: Update,
U: IntoUpdate,
{
let (view, lu) = ws.into_view_and_loop();
let mut handler = Handler::new(cx, view, upd);
Expand All @@ -56,7 +56,7 @@ where
#[cfg(target_arch = "wasm32")]
fn spawn<U>(ws: WindowState<U::Event>, cx: Context, upd: U) -> Result<(), LoopError>
where
U: Update + 'static,
U: IntoUpdate + 'static,
{
use winit::platform::web::EventLoopExtWebSys;

Expand Down Expand Up @@ -94,37 +94,6 @@ impl error::Error for LoopError {
}
}

trait IntoUpdate: Sized {
type Update: Update;

fn into_update(self, view: &View) -> Self::Update;
}

struct FromFn<F>(F);

impl<F, U> IntoUpdate for FromFn<F>
where
F: FnOnce(&View) -> U,
U: Update,
{
type Update = U;

fn into_update(self, view: &View) -> Self::Update {
self.0(view)
}
}

impl<U> IntoUpdate for U
where
U: Update,
{
type Update = Self;

fn into_update(self, _: &View) -> Self::Update {
self
}
}

enum Deferred<U>
where
U: IntoUpdate,
Expand Down Expand Up @@ -208,7 +177,7 @@ where

impl<U> ApplicationHandler<U::Event> for Handler<U>
where
U: Update,
U: IntoUpdate,
{
fn resumed(&mut self, el: &ActiveEventLoop) {
log::debug!("resumed");
Expand Down
2 changes: 1 addition & 1 deletion dunge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ pub use crate::window::from_element;
#[cfg(feature = "winit")]
pub use crate::{
el::{Buttons, Control, Flow, Key, KeyCode, LoopError, Mouse, MouseButton, SmolStr, Then},
update::{update, update_with_event, update_with_state, Update},
update::{from_view, update, update_with_event, update_with_state, IntoUpdate, Update},
};
53 changes: 53 additions & 0 deletions dunge/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
draw::Draw,
el::{Control, Flow},
state::Frame,
window::View,
};

/// The update stage.
Expand Down Expand Up @@ -180,3 +181,55 @@ where
evty: PhantomData,
}
}

/// Lazy instantiation of the value that implements the [`Update`] trait.
///
/// It is usually more convenient to create the update value after the [view](View)
/// has been initialized. This will let you know the current properties of the window
/// like [size](View::size) and [format](View::format).
pub trait IntoUpdate: Sized {
type Flow: Flow;
type Event: 'static;
type Update: Update<Flow = Self::Flow, Event = Self::Event>;

/// Creating a value from the [view](View).
fn into_update(self, view: &View) -> Self::Update;
}

impl<U> IntoUpdate for U
where
U: Update,
{
type Flow = U::Flow;
type Event = U::Event;
type Update = Self;

fn into_update(self, _: &View) -> Self::Update {
self
}
}

/// Creates an [update](Update) value using a function from the received [view](View).
pub fn from_view<F, U>(f: F) -> impl IntoUpdate<Flow = U::Flow, Event = U::Event>
where
F: FnOnce(&View) -> U,
U: Update,
{
struct FromFn<F>(F);

impl<F, U> IntoUpdate for FromFn<F>
where
F: FnOnce(&View) -> U,
U: Update,
{
type Flow = U::Flow;
type Event = U::Event;
type Update = U;

fn into_update(self, view: &View) -> Self::Update {
self.0(view)
}
}

FromFn(f)
}
6 changes: 3 additions & 3 deletions dunge/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use {
element::Element,
format::Format,
state::{State, Target},
update::Update,
update::IntoUpdate,
},
std::{error, fmt, sync::Arc},
wgpu::{
Expand Down Expand Up @@ -86,7 +86,7 @@ impl<V> WindowState<V> {
/// Runs an event loop.
pub fn run<U>(self, cx: Context, upd: U) -> Result<(), LoopError>
where
U: Update<Event = V> + 'static,
U: IntoUpdate<Event = V> + 'static,
{
el::run(self, cx, upd)
}
Expand All @@ -95,7 +95,7 @@ impl<V> WindowState<V> {
#[cfg(not(target_arch = "wasm32"))]
pub fn run_local<U>(self, cx: Context, upd: U) -> Result<(), LoopError>
where
U: Update<Event = V>,
U: IntoUpdate<Event = V>,
{
el::run_local(self, cx, upd)
}
Expand Down

0 comments on commit a2df2aa

Please sign in to comment.