Make Application and Sandbox return a Result

This commit is contained in:
Héctor Ramón Jiménez 2020-09-08 00:35:17 +02:00
parent faa12382d4
commit c1f79b40cf
40 changed files with 166 additions and 58 deletions

View file

@ -1,7 +1,9 @@
//! Create interactive, native cross-platform applications.
use crate::conversion;
use crate::mouse;
use crate::{
conversion, mouse, Clipboard, Color, Command, Debug, Executor, Mode, Proxy,
Runtime, Settings, Size, Subscription,
Clipboard, Color, Command, Debug, Error, Executor, Mode, Proxy, Runtime,
Settings, Size, Subscription,
};
use iced_graphics::window;
use iced_graphics::Viewport;
@ -108,7 +110,8 @@ pub trait Application: Program {
pub fn run<A, E, C>(
settings: Settings<A::Flags>,
compositor_settings: C::Settings,
) where
) -> Result<(), Error>
where
A: Application + 'static,
E: Executor + 'static,
C: window::Compositor<Renderer = A::Renderer> + 'static,
@ -123,7 +126,7 @@ pub fn run<A, E, C>(
let event_loop = EventLoop::with_user_event();
let mut runtime = {
let executor = E::new().expect("Create executor");
let executor = E::new().map_err(Error::ExecutorCreationFailed)?;
let proxy = Proxy::new(event_loop.create_proxy());
Runtime::new(executor, proxy)
@ -145,7 +148,7 @@ pub fn run<A, E, C>(
.window
.into_builder(&title, mode, event_loop.primary_monitor())
.build(&event_loop)
.expect("Open window");
.map_err(Error::WindowCreationFailed)?;
let clipboard = Clipboard::new(&window);
// TODO: Encode cursor availability in the type-system
@ -160,7 +163,7 @@ pub fn run<A, E, C>(
);
let mut resized = false;
let (mut compositor, mut renderer) = C::new(compositor_settings);
let (mut compositor, mut renderer) = C::new(compositor_settings)?;
let surface = compositor.create_surface(&window);

27
winit/src/error.rs Normal file
View file

@ -0,0 +1,27 @@
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(winit::error::OsError),
/// A suitable graphics adapter or device could not be found.
#[error("a suitable graphics adapter or device could not be found")]
GraphicsAdapterNotFound,
}
impl From<iced_graphics::Error> for Error {
fn from(error: iced_graphics::Error) -> Error {
match error {
iced_graphics::Error::AdapterNotFound => {
Error::GraphicsAdapterNotFound
}
}
}
}

View file

@ -30,11 +30,13 @@ pub mod conversion;
pub mod settings;
mod clipboard;
mod error;
mod mode;
mod proxy;
pub use application::Application;
pub use clipboard::Clipboard;
pub use error::Error;
pub use mode::Mode;
pub use proxy::Proxy;
pub use settings::Settings;