Struct std::stream::Stream

Overview

A stream produced by an async generator function.

Generator are async functions or closures which contain the yield expressions.

Examples

use std::stream::Stream;

let f = async |n| {
   yield n;
   yield n + 1;
};

let g = f(10);

assert!(g is Stream);

Methods

async fn next(self) -> Option

Get the next value produced by this stream through an asynchronous iterator-like protocol.

This function will resume execution until a value is produced through GeneratorState::Yielded(value), at which point it will return Some(value). Once GeneratorState::Complete is returned None will be returned.

Examples

use std::ops::GeneratorState;

async fn generate() {
   yield 1;
   yield 2;
}

let g = generate();

assert_eq!(g.next().await, Some(1));
assert_eq!(g.next().await, Some(2));
assert_eq!(g.next().await, None);
``
async fn resume(self, value: any) -> GeneratorState

Resumes the execution of this stream.

This function will resume execution of the stream or start execution if it hasn't already. This call will return back into the stream's last suspension point, resuming execution from the latest yield. The stream will continue executing until it either yields or returns, at which point this function will return.

Return value

The GeneratorState enum returned from this function indicates what state the stream is in upon returning. If the Yielded variant is returned then the stream has reached a suspension point and a value has been yielded out. Streams in this state are available for resumption at a later point.

If Complete is returned then the stream has completely finished with the value provided. It is invalid for the stream to be resumed again.

Panics

This function may panic if it is called after the Complete variant has been returned previously. While stream literals in the language are guaranteed to panic on resuming after Complete, this is not guaranteed for all implementations of the Stream.

Examples

use std::ops::GeneratorState;

async fn generate() {
   let n = yield 1;
   yield 2 + n;
}

let g = generate();

assert_eq!(g.resume(()).await, GeneratorState::Yielded(1));
assert_eq!(g.resume(1).await, GeneratorState::Yielded(3));
assert_eq!(g.resume(()).await, GeneratorState::Complete(()));
``

Trait Implementations

impl Clone for Stream
fn clone(value: any) -> any

Clone the specified value.

Examples

let a = 42;
let b = a;
let c = a.clone();

a += 1;

assert_eq!(a, 43);
assert_eq!(b, 42);
assert_eq!(c, 42);

Protocols

protocol DEBUG_FMT
format!("{:?}", value)

Debug print this stream

Examples

use std::ops::GeneratorState;

fn generate() {
   let n = yield 1;
   yield 2 + n;
}

let a = generate();

println!("{a:?}");
``
protocol CLONE
let $out = clone(value)

Clone a stream.

This clones the state of the stream too, allowing it to be resumed independently.

Examples

use std::ops::GeneratorState;

async fn generate() {
   let n = yield 1;
   yield 2 + n;
}

let a = generate();

assert_eq!(a.resume(()).await, GeneratorState::Yielded(1));
let b = a.clone();
assert_eq!(a.resume(2).await, GeneratorState::Yielded(4));
assert_eq!(b.resume(3).await, GeneratorState::Yielded(5));

assert_eq!(a.resume(()).await, GeneratorState::Complete(()));
assert_eq!(b.resume(()).await, GeneratorState::Complete(()));
``