diff --git a/src/application.rs b/src/application.rs index da0fbd37..c74ba5c4 100644 --- a/src/application.rs +++ b/src/application.rs @@ -40,7 +40,7 @@ use crate::{ use std::borrow::Cow; -/// Creates an iced [`Application`] given its update and view logic. +/// Creates an iced [`Application`] given its boot, update, and view logic. /// /// # Example /// ```no_run @@ -69,7 +69,7 @@ use std::borrow::Cow; /// } /// ``` pub fn application( - new: impl New, + boot: impl Boot, update: impl Update, view: impl for<'a> self::View<'a, State, Message, Theme, Renderer>, ) -> Application> @@ -81,8 +81,8 @@ where { use std::marker::PhantomData; - struct Instance { - new: New, + struct Instance { + boot: Boot, update: Update, view: View, _state: PhantomData, @@ -91,13 +91,13 @@ where _renderer: PhantomData, } - impl Program - for Instance + impl Program + for Instance where Message: Send + std::fmt::Debug + 'static, Theme: Default + theme::Base, Renderer: program::Renderer, - New: self::New, + Boot: self::Boot, Update: self::Update, View: for<'a> self::View<'a, State, Message, Theme, Renderer>, { @@ -114,7 +114,7 @@ where } fn boot(&self) -> (State, Task) { - self.new.new() + self.boot.boot() } fn update( @@ -136,7 +136,7 @@ where Application { raw: Instance { - new, + boot, update, view, _state: PhantomData, @@ -165,12 +165,6 @@ pub struct Application { impl Application

{ /// Runs the [`Application`]. - /// - /// The state of the [`Application`] must implement [`Default`]. - /// If your state does not implement [`Default`], use [`run_with`] - /// instead. - /// - /// [`run_with`]: Self::run_with pub fn run(self) -> Result where Self: 'static, @@ -398,37 +392,42 @@ impl Application

{ } /// The logic to initialize the `State` of some [`Application`]. -pub trait New { +/// +/// This trait is implemented for both `Fn() -> State` and +/// `Fn() -> (State, Task)`. +/// +/// In practice, this means that [`application`] can both take +/// simple functions like `State::default` and more advanced ones +/// that return a [`Task`]. +pub trait Boot { /// Initializes the [`Application`] state. - #[allow(clippy::new_ret_no_self)] - #[allow(clippy::wrong_self_convention)] - fn new(&self) -> (State, Task); + fn boot(&self) -> (State, Task); } -impl New for T +impl Boot for T where T: Fn() -> C, - C: IntoState, + C: IntoBoot, { - fn new(&self) -> (State, Task) { - self().into_state() + fn boot(&self) -> (State, Task) { + self().into_boot() } } -/// TODO -pub trait IntoState { - /// TODO - fn into_state(self) -> (State, Task); +/// The initial state of some [`Application`]. +pub trait IntoBoot { + /// Turns some type into the initial state of some [`Application`]. + fn into_boot(self) -> (State, Task); } -impl IntoState for State { - fn into_state(self) -> (State, Task) { +impl IntoBoot for State { + fn into_boot(self) -> (State, Task) { (self, Task::none()) } } -impl IntoState for (State, Task) { - fn into_state(self) -> (State, Task) { +impl IntoBoot for (State, Task) { + fn into_boot(self) -> (State, Task) { self } } diff --git a/src/daemon.rs b/src/daemon.rs index 322cf23e..384e7582 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -8,7 +8,7 @@ use crate::{Element, Executor, Font, Result, Settings, Subscription, Task}; use std::borrow::Cow; -/// Creates an iced [`Daemon`] given its title, update, and view logic. +/// Creates an iced [`Daemon`] given its boot, update, and view logic. /// /// A [`Daemon`] will not open a window by default, but will run silently /// instead until a [`Task`] from [`window::open`] is returned by its update logic. @@ -19,7 +19,7 @@ use std::borrow::Cow; /// /// [`exit`]: crate::exit pub fn daemon( - boot: impl application::New, + boot: impl application::Boot, update: impl application::Update, view: impl for<'a> self::View<'a, State, Message, Theme, Renderer>, ) -> Daemon> @@ -47,7 +47,7 @@ where Message: Send + std::fmt::Debug + 'static, Theme: Default + theme::Base, Renderer: program::Renderer, - Boot: application::New, + Boot: application::Boot, Update: application::Update, View: for<'a> self::View<'a, State, Message, Theme, Renderer>, { @@ -64,7 +64,7 @@ where } fn boot(&self) -> (Self::State, Task) { - self.boot.new() + self.boot.boot() } fn update( @@ -113,12 +113,6 @@ pub struct Daemon { impl Daemon

{ /// Runs the [`Daemon`]. - /// - /// The state of the [`Daemon`] must implement [`Default`]. - /// If your state does not implement [`Default`], use [`run_with`] - /// instead. - /// - /// [`run_with`]: Self::run_with pub fn run(self) -> Result where Self: 'static,