pub enum MaybeFuture<'a, T, E> {
Result(Result<T, E>),
Future(BoxFuture<'a, Result<T, E>>),
}
async
only.Expand description
A type that can represent either an immediate result or a future.
When the async
feature is enabled, MaybeFuture
is an enum that can hold
either an immediate Result<T, E>
or a boxed future that will eventually
resolve to a Result<T, E>
. This allows the same API to handle both
synchronous and asynchronous operations seamlessly.
§Type Parameters
'a
- The lifetime of the futureT
- The success typeE
- The error type
§Variants
Result(Result<T, E>)
- An immediate resultFuture(BoxFuture<'a, Result<T, E>>)
- A future that will resolve to a result
§Feature-Dependent Behavior
This type has different definitions depending on feature flags:
- Without
async
feature: Type alias toResult<T, E>
- With
async
feature: Enum withResult
andFuture
variants (this definition)
See the module documentation for more details about feature-dependent behavior.
§Examples
§Working with immediate results
use cel_cxx::MaybeFuture;
let immediate: MaybeFuture<'_, i32, &str> = MaybeFuture::Result(Ok(42));
assert!(immediate.is_result());
assert_eq!(immediate.into_result().unwrap().unwrap(), 42);
§Working with futures
use cel_cxx::MaybeFuture;
use futures::future::BoxFuture;
let future_result: BoxFuture<'_, Result<i32, &str>> =
Box::pin(async { Ok(42) });
let maybe_future: MaybeFuture<'_, i32, &str> =
MaybeFuture::Future(future_result);
assert!(maybe_future.is_future());
let result = maybe_future.into_future().unwrap().await.unwrap();
assert_eq!(result, 42);
Variants§
Result(Result<T, E>)
An immediate result value.
Future(BoxFuture<'a, Result<T, E>>)
A future that will resolve to a result value.
Implementations§
Source§impl<'a, T, E> MaybeFuture<'a, T, E>
impl<'a, T, E> MaybeFuture<'a, T, E>
Sourcepub fn is_result(&self) -> bool
pub fn is_result(&self) -> bool
Returns true
if this is an immediate result.
§Examples
use cel_cxx::MaybeFuture;
let immediate: MaybeFuture<'_, i32, &str> = MaybeFuture::Result(Ok(42));
assert!(immediate.is_result());
Sourcepub fn is_future(&self) -> bool
pub fn is_future(&self) -> bool
Returns true
if this is a future.
§Examples
use cel_cxx::MaybeFuture;
use futures::future::BoxFuture;
let future: BoxFuture<'_, Result<i32, &str>> = Box::pin(async { Ok(42) });
let maybe_future: MaybeFuture<'_, i32, &str> = MaybeFuture::Future(future);
assert!(maybe_future.is_future());
Sourcepub fn result_ref(&self) -> Option<&Result<T, E>>
pub fn result_ref(&self) -> Option<&Result<T, E>>
Returns a reference to the result if this is an immediate result.
§Examples
use cel_cxx::MaybeFuture;
let immediate: MaybeFuture<'_, i32, &str> = MaybeFuture::Result(Ok(42));
assert_eq!(immediate.result_ref().unwrap().as_ref().unwrap(), &42);
Sourcepub fn future_ref(&self) -> Option<&BoxFuture<'a, Result<T, E>>>
pub fn future_ref(&self) -> Option<&BoxFuture<'a, Result<T, E>>>
Returns a reference to the future if this is a future.
§Examples
use cel_cxx::MaybeFuture;
use futures::future::BoxFuture;
let future: BoxFuture<'_, Result<i32, &str>> = Box::pin(async { Ok(42) });
let maybe_future: MaybeFuture<'_, i32, &str> = MaybeFuture::Future(future);
assert!(maybe_future.future_ref().is_some());
Sourcepub fn result_mut(&mut self) -> Option<&mut Result<T, E>>
pub fn result_mut(&mut self) -> Option<&mut Result<T, E>>
Returns a mutable reference to the result if this is an immediate result.
Sourcepub fn future_mut(&mut self) -> Option<&mut BoxFuture<'a, Result<T, E>>>
pub fn future_mut(&mut self) -> Option<&mut BoxFuture<'a, Result<T, E>>>
Returns a mutable reference to the future if this is a future.
Sourcepub fn into_result(self) -> Option<Result<T, E>>
pub fn into_result(self) -> Option<Result<T, E>>
Consumes this value and returns the result if it’s an immediate result.
§Examples
use cel_cxx::MaybeFuture;
let immediate: MaybeFuture<'_, i32, &str> = MaybeFuture::Result(Ok(42));
let result = immediate.into_result().unwrap();
assert_eq!(result.unwrap(), 42);
Sourcepub fn into_future(self) -> Option<BoxFuture<'a, Result<T, E>>>
pub fn into_future(self) -> Option<BoxFuture<'a, Result<T, E>>>
Consumes this value and returns the future if it’s a future.
§Examples
use cel_cxx::MaybeFuture;
use futures::future::BoxFuture;
let future: BoxFuture<'_, Result<i32, &str>> = Box::pin(async { Ok(42) });
let maybe_future: MaybeFuture<'_, i32, &str> = MaybeFuture::Future(future);
let extracted_future = maybe_future.into_future().unwrap();
let result = extracted_future.await.unwrap();
assert_eq!(result, 42);
Sourcepub fn expect_result(self, msg: &str) -> Result<T, E>
pub fn expect_result(self, msg: &str) -> Result<T, E>
Extracts the result, panicking if this is a future.
§Panics
Panics if this MaybeFuture
contains a future rather than an immediate result.
§Examples
use cel_cxx::MaybeFuture;
let immediate: MaybeFuture<'_, i32, &str> = MaybeFuture::Result(Ok(42));
let result = immediate.expect_result("Expected immediate result");
assert_eq!(result.unwrap(), 42);
Sourcepub fn expect_future(self, msg: &str) -> BoxFuture<'a, Result<T, E>>
pub fn expect_future(self, msg: &str) -> BoxFuture<'a, Result<T, E>>
Extracts the future, panicking if this is an immediate result.
§Panics
Panics if this MaybeFuture
contains an immediate result rather than a future.
§Examples
use cel_cxx::MaybeFuture;
use futures::future::BoxFuture;
let future: BoxFuture<'_, Result<i32, &str>> = Box::pin(async { Ok(42) });
let maybe_future: MaybeFuture<'_, i32, &str> = MaybeFuture::Future(future);
let extracted_future = maybe_future.expect_future("Expected future");
let result = extracted_future.await.unwrap();
assert_eq!(result, 42);
Sourcepub fn unwrap_result(self) -> Result<T, E>
pub fn unwrap_result(self) -> Result<T, E>
Extracts the result, panicking if this is a future.
This is equivalent to expect_result
but with a default panic message.
§Panics
Panics if this MaybeFuture
contains a future rather than an immediate result.
Sourcepub fn unwrap_future(self) -> BoxFuture<'a, Result<T, E>>
pub fn unwrap_future(self) -> BoxFuture<'a, Result<T, E>>
Extracts the future, panicking if this is an immediate result.
This is equivalent to expect_future
but with a default panic message.
§Panics
Panics if this MaybeFuture
contains an immediate result rather than a future.
Trait Implementations§
Auto Trait Implementations§
impl<'a, T, E> Freeze for MaybeFuture<'a, T, E>
impl<'a, T, E> !RefUnwindSafe for MaybeFuture<'a, T, E>
impl<'a, T, E> Send for MaybeFuture<'a, T, E>
impl<'a, T, E> !Sync for MaybeFuture<'a, T, E>
impl<'a, T, E> Unpin for MaybeFuture<'a, T, E>
impl<'a, T, E> !UnwindSafe for MaybeFuture<'a, T, E>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more