Add explicit error handling to image loading
This commit is contained in:
parent
7c11ccb046
commit
867fe819c0
17 changed files with 357 additions and 118 deletions
|
|
@ -3,13 +3,13 @@ use crate::core::image::Handle;
|
|||
use crate::futures::futures::channel::oneshot;
|
||||
use crate::task::{self, Task};
|
||||
|
||||
pub use crate::core::image::Allocation;
|
||||
pub use crate::core::image::{Allocation, Error};
|
||||
|
||||
/// An image action.
|
||||
#[derive(Debug)]
|
||||
pub enum Action {
|
||||
/// Allocates the given [`Handle`].
|
||||
Allocate(Handle, oneshot::Sender<Allocation>),
|
||||
Allocate(Handle, oneshot::Sender<Result<Allocation, Error>>),
|
||||
}
|
||||
|
||||
/// Allocates an image [`Handle`].
|
||||
|
|
@ -17,7 +17,7 @@ pub enum Action {
|
|||
/// When you obtain an [`Allocation`] explicitly, you get the guarantee
|
||||
/// that using a [`Handle`] will draw the corresponding image immediately
|
||||
/// in the next frame.
|
||||
pub fn allocate(handle: impl Into<Handle>) -> Task<Allocation> {
|
||||
pub fn allocate(handle: impl Into<Handle>) -> Task<Result<Allocation, Error>> {
|
||||
task::oneshot(|sender| {
|
||||
crate::Action::Image(Action::Allocate(handle.into(), sender))
|
||||
})
|
||||
|
|
|
|||
|
|
@ -390,6 +390,20 @@ impl<T, E> Task<Result<T, E>> {
|
|||
result.map_or_else(|error| Task::done(Err(error)), &f)
|
||||
})
|
||||
}
|
||||
|
||||
/// Maps the error type of this [`Task`] to a different one using the given
|
||||
/// function.
|
||||
pub fn map_err<E2>(
|
||||
self,
|
||||
f: impl Fn(E) -> E2 + MaybeSend + 'static,
|
||||
) -> Task<Result<T, E2>>
|
||||
where
|
||||
T: MaybeSend + 'static,
|
||||
E: MaybeSend + 'static,
|
||||
E2: MaybeSend + 'static,
|
||||
{
|
||||
self.map(move |result| result.map_err(&f))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Default for Task<T> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue