diff options
author | 2020-09-08 00:35:17 +0200 | |
---|---|---|
committer | 2020-09-08 00:44:59 +0200 | |
commit | c1f79b40cf704cafc807250b177fc7d3444fe54f (patch) | |
tree | 31c0cf2b8d166976dc819f020e0185f9d8c2bdcd /src | |
parent | faa12382d4d7a44ab60fa5bf2a5024a34dbb2e8d (diff) | |
download | iced-c1f79b40cf704cafc807250b177fc7d3444fe54f.tar.gz iced-c1f79b40cf704cafc807250b177fc7d3444fe54f.tar.bz2 iced-c1f79b40cf704cafc807250b177fc7d3444fe54f.zip |
Make `Application` and `Sandbox` return a `Result`
Diffstat (limited to 'src')
-rw-r--r-- | src/application.rs | 22 | ||||
-rw-r--r-- | src/error.rs | 34 | ||||
-rw-r--r-- | src/lib.rs | 4 | ||||
-rw-r--r-- | src/result.rs | 6 | ||||
-rw-r--r-- | src/sandbox.rs | 7 |
5 files changed, 61 insertions, 12 deletions
diff --git a/src/application.rs b/src/application.rs index 47b89dbd..d46cd2ac 100644 --- a/src/application.rs +++ b/src/application.rs @@ -1,6 +1,5 @@ -use crate::{ - window, Color, Command, Element, Executor, Settings, Subscription, -}; +use crate::window; +use crate::{Color, Command, Element, Executor, Settings, Subscription}; /// An interactive cross-platform application. /// @@ -59,7 +58,7 @@ use crate::{ /// ```no_run /// use iced::{executor, Application, Command, Element, Settings, Text}; /// -/// pub fn main() { +/// pub fn main() -> iced::Result { /// Hello::run(Settings::default()) /// } /// @@ -204,12 +203,13 @@ pub trait Application: Sized { /// Runs the [`Application`]. /// /// On native platforms, this method will take control of the current thread - /// and __will NOT return__. + /// and __will NOT return__ unless there is an [`Error`] during startup. /// /// It should probably be that last thing you call in your `main` function. /// /// [`Application`]: trait.Application.html - fn run(settings: Settings<Self::Flags>) + /// [`Error`]: enum.Error.html + fn run(settings: Settings<Self::Flags>) -> crate::Result where Self: 'static, { @@ -226,15 +226,19 @@ pub trait Application: Sized { ..crate::renderer::Settings::default() }; - crate::runtime::application::run::< + Ok(crate::runtime::application::run::< Instance<Self>, Self::Executor, crate::renderer::window::Compositor, - >(settings.into(), renderer_settings); + >(settings.into(), renderer_settings)?) } #[cfg(target_arch = "wasm32")] - <Instance<Self> as iced_web::Application>::run(settings.flags); + { + <Instance<Self> as iced_web::Application>::run(settings.flags); + + Ok(()) + } } } diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 00000000..31b87d17 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,34 @@ +use iced_futures::futures; + +/// An error that occurred while running an application. +#[derive(Debug, thiserror::Error)] +pub enum Error { + /// The futures executor could not be created. + #[error("the futures executor could not be created")] + ExecutorCreationFailed(futures::io::Error), + + /// The application window could not be created. + #[error("the application window could not be created")] + WindowCreationFailed(Box<dyn std::error::Error>), + + /// A suitable graphics adapter or device could not be found. + #[error("a suitable graphics adapter or device could not be found")] + GraphicsAdapterNotFound, +} + +#[cfg(not(target_arch = "wasm32"))] +impl From<iced_winit::Error> for Error { + fn from(error: iced_winit::Error) -> Error { + match error { + iced_winit::Error::ExecutorCreationFailed(error) => { + Error::ExecutorCreationFailed(error) + } + iced_winit::Error::WindowCreationFailed(error) => { + Error::WindowCreationFailed(Box::new(error)) + } + iced_winit::Error::GraphicsAdapterNotFound => { + Error::GraphicsAdapterNotFound + } + } + } +} @@ -181,6 +181,8 @@ #![cfg_attr(docsrs, feature(doc_cfg))] mod application; mod element; +mod error; +mod result; mod sandbox; pub mod executor; @@ -225,7 +227,9 @@ pub use widget::*; pub use application::Application; pub use element::Element; +pub use error::Error; pub use executor::Executor; +pub use result::Result; pub use sandbox::Sandbox; pub use settings::Settings; diff --git a/src/result.rs b/src/result.rs new file mode 100644 index 00000000..2f05a6a9 --- /dev/null +++ b/src/result.rs @@ -0,0 +1,6 @@ +use crate::Error; + +/// The result of running an [`Application`]. +/// +/// [`Application`]: trait.Application.html +pub type Result = std::result::Result<(), Error>; diff --git a/src/sandbox.rs b/src/sandbox.rs index 6a73eab0..c72b58d8 100644 --- a/src/sandbox.rs +++ b/src/sandbox.rs @@ -1,5 +1,6 @@ +use crate::executor; use crate::{ - executor, Application, Color, Command, Element, Settings, Subscription, + Application, Color, Command, Element, Error, Settings, Subscription, }; /// A sandboxed [`Application`]. @@ -64,7 +65,7 @@ use crate::{ /// ```no_run /// use iced::{Element, Sandbox, Settings, Text}; /// -/// pub fn main() { +/// pub fn main() -> iced::Result { /// Hello::run(Settings::default()) /// } /// @@ -159,7 +160,7 @@ pub trait Sandbox { /// It should probably be that last thing you call in your `main` function. /// /// [`Sandbox`]: trait.Sandbox.html - fn run(settings: Settings<()>) + fn run(settings: Settings<()>) -> Result<(), Error> where Self: 'static + Sized, { |