Enum MaybeFuture

Source
pub enum MaybeFuture<'a, T, E> {
    Result(Result<T, E>),
    Future(BoxFuture<'a, Result<T, E>>),
}
Available on crate feature 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 future
  • T - The success type
  • E - The error type

§Variants

  • Result(Result<T, E>) - An immediate result
  • Future(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 to Result<T, E>
  • With async feature: Enum with Result and Future 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>

Source

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());
Source

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());
Source

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);
Source

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());
Source

pub fn result_mut(&mut self) -> Option<&mut Result<T, E>>

Returns a mutable reference to the result if this is an immediate result.

Source

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.

Source

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);
Source

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);
Source

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);
Source

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);
Source

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.

Source

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§

Source§

impl<'a, T: Debug, E: Debug> Debug for MaybeFuture<'a, T, E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, Fut, T, E> From<Pin<Box<Fut>>> for MaybeFuture<'a, T, E>
where Fut: Future<Output = Result<T, E>> + Send + 'a,

Source§

fn from(f: Pin<Box<Fut>>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T, E> From<Result<T, E>> for MaybeFuture<'a, T, E>

Source§

fn from(t: Result<T, E>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a, T, E> Freeze for MaybeFuture<'a, T, E>
where T: Freeze, E: Freeze,

§

impl<'a, T, E> !RefUnwindSafe for MaybeFuture<'a, T, E>

§

impl<'a, T, E> Send for MaybeFuture<'a, T, E>
where T: Send, E: Send,

§

impl<'a, T, E> !Sync for MaybeFuture<'a, T, E>

§

impl<'a, T, E> Unpin for MaybeFuture<'a, T, E>
where T: Unpin, E: Unpin,

§

impl<'a, T, E> !UnwindSafe for MaybeFuture<'a, T, E>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more