Fix documentation of application and daemon
This commit is contained in:
parent
68c4764494
commit
63e66b0320
2 changed files with 34 additions and 41 deletions
|
|
@ -40,7 +40,7 @@ use crate::{
|
||||||
|
|
||||||
use std::borrow::Cow;
|
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
|
/// # Example
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
|
|
@ -69,7 +69,7 @@ use std::borrow::Cow;
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn application<State, Message, Theme, Renderer>(
|
pub fn application<State, Message, Theme, Renderer>(
|
||||||
new: impl New<State, Message>,
|
boot: impl Boot<State, Message>,
|
||||||
update: impl Update<State, Message>,
|
update: impl Update<State, Message>,
|
||||||
view: impl for<'a> self::View<'a, State, Message, Theme, Renderer>,
|
view: impl for<'a> self::View<'a, State, Message, Theme, Renderer>,
|
||||||
) -> Application<impl Program<State = State, Message = Message, Theme = Theme>>
|
) -> Application<impl Program<State = State, Message = Message, Theme = Theme>>
|
||||||
|
|
@ -81,8 +81,8 @@ where
|
||||||
{
|
{
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
struct Instance<State, Message, Theme, Renderer, New, Update, View> {
|
struct Instance<State, Message, Theme, Renderer, Boot, Update, View> {
|
||||||
new: New,
|
boot: Boot,
|
||||||
update: Update,
|
update: Update,
|
||||||
view: View,
|
view: View,
|
||||||
_state: PhantomData<State>,
|
_state: PhantomData<State>,
|
||||||
|
|
@ -91,13 +91,13 @@ where
|
||||||
_renderer: PhantomData<Renderer>,
|
_renderer: PhantomData<Renderer>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<State, Message, Theme, Renderer, New, Update, View> Program
|
impl<State, Message, Theme, Renderer, Boot, Update, View> Program
|
||||||
for Instance<State, Message, Theme, Renderer, New, Update, View>
|
for Instance<State, Message, Theme, Renderer, Boot, Update, View>
|
||||||
where
|
where
|
||||||
Message: Send + std::fmt::Debug + 'static,
|
Message: Send + std::fmt::Debug + 'static,
|
||||||
Theme: Default + theme::Base,
|
Theme: Default + theme::Base,
|
||||||
Renderer: program::Renderer,
|
Renderer: program::Renderer,
|
||||||
New: self::New<State, Message>,
|
Boot: self::Boot<State, Message>,
|
||||||
Update: self::Update<State, Message>,
|
Update: self::Update<State, Message>,
|
||||||
View: for<'a> self::View<'a, State, Message, Theme, Renderer>,
|
View: for<'a> self::View<'a, State, Message, Theme, Renderer>,
|
||||||
{
|
{
|
||||||
|
|
@ -114,7 +114,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn boot(&self) -> (State, Task<Message>) {
|
fn boot(&self) -> (State, Task<Message>) {
|
||||||
self.new.new()
|
self.boot.boot()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(
|
fn update(
|
||||||
|
|
@ -136,7 +136,7 @@ where
|
||||||
|
|
||||||
Application {
|
Application {
|
||||||
raw: Instance {
|
raw: Instance {
|
||||||
new,
|
boot,
|
||||||
update,
|
update,
|
||||||
view,
|
view,
|
||||||
_state: PhantomData,
|
_state: PhantomData,
|
||||||
|
|
@ -165,12 +165,6 @@ pub struct Application<P: Program> {
|
||||||
|
|
||||||
impl<P: Program> Application<P> {
|
impl<P: Program> Application<P> {
|
||||||
/// Runs the [`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
|
pub fn run(self) -> Result
|
||||||
where
|
where
|
||||||
Self: 'static,
|
Self: 'static,
|
||||||
|
|
@ -398,37 +392,42 @@ impl<P: Program> Application<P> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The logic to initialize the `State` of some [`Application`].
|
/// The logic to initialize the `State` of some [`Application`].
|
||||||
pub trait New<State, Message> {
|
///
|
||||||
|
/// This trait is implemented for both `Fn() -> State` and
|
||||||
|
/// `Fn() -> (State, Task<Message>)`.
|
||||||
|
///
|
||||||
|
/// 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<State, Message> {
|
||||||
/// Initializes the [`Application`] state.
|
/// Initializes the [`Application`] state.
|
||||||
#[allow(clippy::new_ret_no_self)]
|
fn boot(&self) -> (State, Task<Message>);
|
||||||
#[allow(clippy::wrong_self_convention)]
|
|
||||||
fn new(&self) -> (State, Task<Message>);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, C, State, Message> New<State, Message> for T
|
impl<T, C, State, Message> Boot<State, Message> for T
|
||||||
where
|
where
|
||||||
T: Fn() -> C,
|
T: Fn() -> C,
|
||||||
C: IntoState<State, Message>,
|
C: IntoBoot<State, Message>,
|
||||||
{
|
{
|
||||||
fn new(&self) -> (State, Task<Message>) {
|
fn boot(&self) -> (State, Task<Message>) {
|
||||||
self().into_state()
|
self().into_boot()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// TODO
|
/// The initial state of some [`Application`].
|
||||||
pub trait IntoState<State, Message> {
|
pub trait IntoBoot<State, Message> {
|
||||||
/// TODO
|
/// Turns some type into the initial state of some [`Application`].
|
||||||
fn into_state(self) -> (State, Task<Message>);
|
fn into_boot(self) -> (State, Task<Message>);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<State, Message> IntoState<State, Message> for State {
|
impl<State, Message> IntoBoot<State, Message> for State {
|
||||||
fn into_state(self) -> (State, Task<Message>) {
|
fn into_boot(self) -> (State, Task<Message>) {
|
||||||
(self, Task::none())
|
(self, Task::none())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<State, Message> IntoState<State, Message> for (State, Task<Message>) {
|
impl<State, Message> IntoBoot<State, Message> for (State, Task<Message>) {
|
||||||
fn into_state(self) -> (State, Task<Message>) {
|
fn into_boot(self) -> (State, Task<Message>) {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ use crate::{Element, Executor, Font, Result, Settings, Subscription, Task};
|
||||||
|
|
||||||
use std::borrow::Cow;
|
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
|
/// 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.
|
/// instead until a [`Task`] from [`window::open`] is returned by its update logic.
|
||||||
|
|
@ -19,7 +19,7 @@ use std::borrow::Cow;
|
||||||
///
|
///
|
||||||
/// [`exit`]: crate::exit
|
/// [`exit`]: crate::exit
|
||||||
pub fn daemon<State, Message, Theme, Renderer>(
|
pub fn daemon<State, Message, Theme, Renderer>(
|
||||||
boot: impl application::New<State, Message>,
|
boot: impl application::Boot<State, Message>,
|
||||||
update: impl application::Update<State, Message>,
|
update: impl application::Update<State, Message>,
|
||||||
view: impl for<'a> self::View<'a, State, Message, Theme, Renderer>,
|
view: impl for<'a> self::View<'a, State, Message, Theme, Renderer>,
|
||||||
) -> Daemon<impl Program<State = State, Message = Message, Theme = Theme>>
|
) -> Daemon<impl Program<State = State, Message = Message, Theme = Theme>>
|
||||||
|
|
@ -47,7 +47,7 @@ where
|
||||||
Message: Send + std::fmt::Debug + 'static,
|
Message: Send + std::fmt::Debug + 'static,
|
||||||
Theme: Default + theme::Base,
|
Theme: Default + theme::Base,
|
||||||
Renderer: program::Renderer,
|
Renderer: program::Renderer,
|
||||||
Boot: application::New<State, Message>,
|
Boot: application::Boot<State, Message>,
|
||||||
Update: application::Update<State, Message>,
|
Update: application::Update<State, Message>,
|
||||||
View: for<'a> self::View<'a, State, Message, Theme, Renderer>,
|
View: for<'a> self::View<'a, State, Message, Theme, Renderer>,
|
||||||
{
|
{
|
||||||
|
|
@ -64,7 +64,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn boot(&self) -> (Self::State, Task<Self::Message>) {
|
fn boot(&self) -> (Self::State, Task<Self::Message>) {
|
||||||
self.boot.new()
|
self.boot.boot()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(
|
fn update(
|
||||||
|
|
@ -113,12 +113,6 @@ pub struct Daemon<P: Program> {
|
||||||
|
|
||||||
impl<P: Program> Daemon<P> {
|
impl<P: Program> Daemon<P> {
|
||||||
/// Runs the [`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
|
pub fn run(self) -> Result
|
||||||
where
|
where
|
||||||
Self: 'static,
|
Self: 'static,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue