pub struct StreamSender { /* private fields */ }Expand description
Sender half for streaming handlers.
Wraps a bounded tokio::sync::mpsc::Sender<String> and provides
ergonomic methods for sending typed items and checking cancellation.
The associated CancellationToken is automatically cancelled when
the StreamReceiver is dropped, enabling explicit cancellation checks
via tokio::select! in addition to is_closed().
§Example
async fn my_streaming_handler(args: MyArgs, tx: StreamSender) -> String {
let token = tx.cancellation_token();
loop {
tokio::select! {
_ = token.cancelled() => break,
item = next_item() => { tx.send(item).await.ok(); }
}
}
r#"{"done": true}"#.to_string()
}Note on Clone: Cloning a StreamSender shares the same underlying
channel and CancellationToken. Calling cancel() on any clone cancels
all of them.
Implementations§
Source§impl StreamSender
impl StreamSender
Sourcepub fn channel() -> (Self, StreamReceiver)
pub fn channel() -> (Self, StreamReceiver)
Create a new stream channel with the default capacity (64).
Returns (sender, receiver) pair. The CancellationToken is
automatically cancelled when the StreamReceiver is dropped.
Sourcepub fn with_capacity(capacity: usize) -> (Self, StreamReceiver)
pub fn with_capacity(capacity: usize) -> (Self, StreamReceiver)
Create a new stream channel with a custom capacity.
Returns (sender, receiver) pair.
Sourcepub fn cancellation_token(&self) -> CancellationToken
pub fn cancellation_token(&self) -> CancellationToken
Get the cancellation token for this stream.
The token is automatically cancelled when the StreamReceiver is
dropped, and can also be cancelled explicitly via cancel().
Use in tokio::select! for cooperative cancellation:
let token = tx.cancellation_token();
tokio::select! {
_ = token.cancelled() => { /* stream cancelled */ }
result = do_work() => { tx.send(result).await?; }
}Sourcepub fn cancel(&self)
pub fn cancel(&self)
Explicitly cancel the stream.
This cancels the CancellationToken, signalling handlers
that are using token.cancelled() in select!.
Sourcepub async fn send(&self, item: impl IntoStreamItem) -> Result<(), StreamError>
pub async fn send(&self, item: impl IntoStreamItem) -> Result<(), StreamError>
Send a stream item.
The item is converted to a JSON string via IntoStreamItem.
Returns StreamError::Closed if the receiver has been dropped,
or StreamError::Serialize if serialization fails.
Trait Implementations§
Source§impl Clone for StreamSender
impl Clone for StreamSender
Source§fn clone(&self) -> StreamSender
fn clone(&self) -> StreamSender
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for StreamSender
impl RefUnwindSafe for StreamSender
impl Send for StreamSender
impl Sync for StreamSender
impl Unpin for StreamSender
impl UnsafeUnpin for StreamSender
impl UnwindSafe for StreamSender
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request