Struct elfo::stream::Stream

source ·
pub struct Stream<M = AnyMessage> { /* private fields */ }
Expand description

A source that emits messages from a provided stream or future.

Possible items of a stream (the M parameter):

  • Any instance of Message.
  • Result<impl Message, impl Message>.

Note: the new() constructor is reserved until AsyncIterator is stabilized.

All wrapped streams and futures are fused by the implementation.

Note: [Stream::is_terminated()] and [Stream::terminate()] cannot be called inside the stream, because it leads to a deadlock.

Tracing

You can always use scope::set_trace_id() to override the current trace.

Examples

Create a stream based on [futures::Stream]:

use elfo::stream::Stream;

#[message]
struct MyItem(u32);

let stream = futures::stream::iter(vec![MyItem(0), MyItem(1)]);
ctx.attach(Stream::from_futures03(stream));

while let Some(envelope) = ctx.recv().await {
    msg!(match envelope {
        MyItem => { /* ... */ },
    });
}

Perform a background request:

use elfo::stream::Stream;

#[message]
struct DataFetched(u32);

#[message]
struct FetchDataFailed(String);

async fn fetch_data() -> Result<DataFetched, FetchDataFailed> {
    // ...
}

while let Some(envelope) = ctx.recv().await {
    msg!(match envelope {
        SomeEvent => {
            ctx.attach(Stream::once(fetch_data()));
        },
        DataFetched => { /* ... */ },
        FetchDataFailed => { /* ... */ },
    });
}

Generate a stream (an alternative to async-stream):

use elfo::stream::Stream;

#[message]
struct SomeMessage(u32);

#[message]
struct AnotherMessage;

ctx.attach(Stream::generate(|mut e| async move {
    e.emit(SomeMessage(42)).await;
    e.emit(AnotherMessage).await;
}));

while let Some(envelope) = ctx.recv().await {
    msg!(match envelope {
        SomeMessage(no) | AnotherMessage => { /* ... */ },
    });
}

Implementations§

source§

impl<M> Stream<M>where M: StreamItem,

source

pub fn from_futures03<S>(stream: S) -> UnattachedSource<Stream<M>>where S: Stream<Item = M> + Send + 'static,

Creates an unattached source based on the provided [futures::Stream].

source

pub fn once<F>(future: F) -> UnattachedSource<Stream<M>>where F: Future<Output = M> + Send + 'static,

Creates an uattached source based on the provided future.

source§

impl Stream<AnyMessage>

source

pub fn generate<G, F>(generator: G) -> UnattachedSource<Stream<AnyMessage>>where G: FnOnce(Emitter) -> F, F: Future<Output = ()> + Send + 'static,

Generates a stream from the provided generator.

The generator receives Emitter as an argument and should return a future that will produce messages by using Emitter::emit.

Trait Implementations§

source§

impl<M> SourceHandle for Stream<M>where M: StreamItem,

source§

fn is_terminated(&self) -> bool

Returns true if the source has stopped producing messages.
source§

fn terminate(self)

Terminates the source. Drop is called immediately.

Auto Trait Implementations§

§

impl<M = AnyMessage> !RefUnwindSafe for Stream<M>

§

impl<M> Send for Stream<M>

§

impl<M = AnyMessage> !Sync for Stream<M>

§

impl<M = AnyMessage> !Unpin for Stream<M>

§

impl<M = AnyMessage> !UnwindSafe for Stream<M>

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,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.
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.
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