From e474edc9f671419e433a0c52161149b89bee08c8 Mon Sep 17 00:00:00 2001 From: Cedric BAIL Date: Wed, 11 Oct 2017 16:52:16 -0700 Subject: [PATCH] eina: add helper to return already known to fail future. --- src/lib/eina/eina_promise.c | 21 +++++++++++++++++++++ src/lib/eina/eina_promise.h | 27 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/lib/eina/eina_promise.c b/src/lib/eina/eina_promise.c index 1fbac60..649b6e9 100644 --- a/src/lib/eina/eina_promise.c +++ b/src/lib/eina/eina_promise.c @@ -815,6 +815,27 @@ eina_future_resolved(Eina_Future_Scheduler *scheduler, Eina_Value value) } EAPI Eina_Future * +eina_future_rejected(Eina_Future_Scheduler *scheduler, Eina_Error err) +{ + Eina_Promise *p; + Eina_Future *f; + + EINA_SAFETY_ON_NULL_GOTO(scheduler, error); + + p = eina_promise_new(scheduler, _dummy_cancel, NULL); + EINA_SAFETY_ON_NULL_GOTO(p, error); + + f = eina_future_new(p); + EINA_SAFETY_ON_NULL_GOTO(f, error); + + eina_promise_reject(p, err); + return f; + + error: + return NULL; +} + +EAPI Eina_Future * eina_future_then_from_desc(Eina_Future *prev, const Eina_Future_Desc desc) { EINA_FUTURE_CHECK_GOTO(prev, err_future); diff --git a/src/lib/eina/eina_promise.h b/src/lib/eina/eina_promise.h index 323dee3..0ffd487 100644 --- a/src/lib/eina/eina_promise.h +++ b/src/lib/eina/eina_promise.h @@ -786,6 +786,33 @@ EAPI Eina_Future *eina_future_new(Eina_Promise *p) EINA_ARG_NONNULL(1) EINA_WARN EAPI Eina_Future *eina_future_resolved(Eina_Future_Scheduler *scheduler, Eina_Value value) EINA_ARG_NONNULL(1); /** + * Creates a new future that is already rejected to a specified error. + * + * This function creates a new future with an already known error, + * that will be resolved and dispatched by the given @a scheduler as + * usual. + * + * This is a helper that behaves the same as eina_promise_new() + * followed by eina_future_new() and then eina_promise_rejected(). + * + * Futures can also be canceled using eina_future_cancel(), which will + * cause the whole chain to be cancelled alongside with any pending + * promise. + * + * @param scheduler The scheduler to use. + * @param err An Eina_Error value + * + * @return The future or @c NULL on error. + * + * @see eina_promise_new() + * @see eina_future_new() + * @see eina_promise_reject() + * @see eina_promise_resolve() + * @see eina_future_cancel() + */ +EAPI Eina_Future *eina_future_rejected(Eina_Future_Scheduler *scheduler, Eina_Error err); + +/** * Register an Eina_Future_Desc to be used when the future is resolve/rejected. * * With this function a callback and data is attached to the future and then -- 2.7.4