noir_compute/operator/sink/mod.rs
1//! Utility traits and structures related to the sink operators.
2//!
3//! The actual operators can be found in [`Stream`](crate::Stream) and
4//! [`KeyedStream`](crate::KeyedStream).
5
6use std::sync::{Arc, Mutex};
7
8use crate::operator::Operator;
9
10pub(super) mod collect;
11pub(super) mod collect_channel;
12pub(super) mod collect_count;
13pub(super) mod collect_vec;
14pub(super) mod for_each;
15
16/// This trait marks all the operators that can be used as sinks.
17pub(crate) trait Sink: Operator<Out = ()> {}
18
19pub(crate) type StreamOutputRef<Out> = Arc<Mutex<Option<Out>>>;
20
21/// The result of a stream after the execution.
22///
23/// This will eventually hold the value _after_ the environment has been fully executed. To access
24/// the content of the output you have to call [`StreamOutput::get`].
25pub struct StreamOutput<Out> {
26 result: StreamOutputRef<Out>,
27}
28
29impl<Out> From<StreamOutputRef<Out>> for StreamOutput<Out> {
30 fn from(value: StreamOutputRef<Out>) -> Self {
31 Self { result: value }
32 }
33}
34
35impl<Out> StreamOutput<Out> {
36 /// Obtain the content of the output.
37 ///
38 /// This will consume the result and return the owned content. If the content has already been
39 /// extracted, or if the content is not ready yet, this will return `None`.
40 pub fn get(self) -> Option<Out> {
41 self.result
42 .try_lock()
43 .expect("Cannot lock output result")
44 .take()
45 }
46}