Struct selectme::StaticSelect

source ·
pub struct StaticSelect<Bits, S, B, O> { /* private fields */ }
Expand description

The implementation used by the select! macro internally and returned by the inline! macro when the static; option is enabled.

See the select! and inline! macros for documentation on syntax and use.

Examples

use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;

use pin_project::pin_project;
use selectme::{Random, StaticSelect};
use tokio::time::{self, Sleep};

#[pin_project]
struct MyFuture {
    #[pin]
    select: StaticSelect<u8, (Sleep, Sleep), Random, Option<u32>>,
}

impl Future for MyFuture {
    type Output = Option<u32>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.project();
        this.select.poll_next(cx)
    }
}

let s1 = time::sleep(Duration::from_millis(100));
let s2 = time::sleep(Duration::from_millis(200));

let my_future = MyFuture {
    select: selectme::inline! {
        static;

        () = s1 => Some(1),
        _ = s2 => Some(2),
        else => None,
    }
};

assert_eq!(my_future.await, Some(1));

Implementations§

source§

impl<Bits, S, B, O> StaticSelect<Bits, S, B, O>where Bits: Number, B: Bias<Bits>,

source

pub async fn next(self: Pin<&mut Self>) -> O

Get the next element from this select when pinned.

Examples
use std::time::Duration;

use tokio::time;

#[selectme::main]
pub(crate) async fn main() {
    let s1 = time::sleep(Duration::from_millis(100));
    let s2 = time::sleep(Duration::from_millis(200));

    let output = selectme::inline! {
        static;

        () = s1 => Some(1),
        _ = s2 => Some(2),
        else => None,
    };

    tokio::pin!(output);

    let mut values = Vec::new();

    while let Some(output) = output.as_mut().next().await {
        values.push(output);
    }

    assert_eq!(values, &[1, 2]);
}
source

pub fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<O>

Poll for the next branch to resolve in this StaticSelect.

Trait Implementations§

source§

impl<Bits, S, B, O> Debug for StaticSelect<Bits, S, B, O>where Bits: Number, B: Debug, S: Debug,

source§

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

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

impl<Bits, S, B, O> Future for StaticSelect<Bits, S, B, O>where Bits: Number, B: Bias<Bits>,

§

type Output = O

The type of value produced on completion.
source§

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>

Attempt to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more

Auto Trait Implementations§

§

impl<Bits, S, B, O> RefUnwindSafe for StaticSelect<Bits, S, B, O>where B: RefUnwindSafe, Bits: RefUnwindSafe, S: RefUnwindSafe,

§

impl<Bits, S, B, O> Send for StaticSelect<Bits, S, B, O>where B: Send, Bits: Send, S: Send,

§

impl<Bits, S, B, O> Sync for StaticSelect<Bits, S, B, O>where B: Sync, Bits: Sync, S: Sync,

§

impl<Bits, S, B, O> Unpin for StaticSelect<Bits, S, B, O>where B: Unpin, Bits: Unpin, S: Unpin,

§

impl<Bits, S, B, O> UnwindSafe for StaticSelect<Bits, S, B, O>where B: UnwindSafe, Bits: UnwindSafe, S: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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<F> IntoFuture for Fwhere F: Future,

§

type Output = <F as Future>::Output

The output that the future will produce on completion.
§

type IntoFuture = F

Which kind of future are we turning this into?
source§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.