File size: 7,107 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
use std::{
fmt,
pin::Pin,
sync::{Arc, Mutex},
task::{Context as TaskContext, Poll},
};
use anyhow::Result;
use futures::{Stream as StreamTrait, StreamExt, TryStreamExt};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// Streams allow for streaming values from source to sink.
///
/// A Stream implements both a reader (which implements the Stream trait), and a
/// writer (which can be sent to another thread). As new values are written, any
/// pending readers will be woken up to receive the new value.
#[derive(Clone, Debug)]
pub struct Stream<T: Clone + Send> {
inner: Arc<Mutex<StreamState<T>>>,
}
/// The StreamState actually holds the data of a Stream.
struct StreamState<T> {
source: Option<Pin<Box<dyn StreamTrait<Item = T> + Send>>>,
pulled: Vec<T>,
}
impl<T: Clone + Send> Stream<T> {
/// Constructs a new Stream, and immediately closes it with only the passed
/// values.
pub fn new_closed(pulled: Vec<T>) -> Self {
Self {
inner: Arc::new(Mutex::new(StreamState {
source: None,
pulled,
})),
}
}
/// Creates a new Stream, which will lazily pull from the source stream.
pub fn new_open(
pulled: Vec<T>,
source: Box<dyn StreamTrait<Item = T> + Send + 'static>,
) -> Self {
Self {
inner: Arc::new(Mutex::new(StreamState {
source: Some(Box::into_pin(source)),
pulled,
})),
}
}
/// Returns a [StreamTrait] implementation to poll values out of our Stream.
pub fn read(&self) -> StreamRead<T> {
StreamRead {
source: self.clone(),
index: 0,
}
}
pub async fn into_single(&self) -> SingleValue<T> {
let mut stream = self.read();
let Some(first) = stream.next().await else {
return SingleValue::None;
};
if stream.next().await.is_some() {
return SingleValue::Multiple;
}
SingleValue::Single(first)
}
}
impl<T: Clone + Send, E: Clone + Send> Stream<Result<T, E>> {
/// Converts a TryStream into a single value when possible.
pub async fn try_into_single(&self) -> Result<SingleValue<T>, E> {
let mut stream = self.read();
let Some(first) = stream.try_next().await? else {
return Ok(SingleValue::None);
};
if stream.try_next().await?.is_some() {
return Ok(SingleValue::Multiple);
}
Ok(SingleValue::Single(first))
}
}
pub enum SingleValue<T> {
/// The Stream did not hold a value.
None,
/// The Stream held multiple values.
Multiple,
/// The held only a single value.
Single(T),
}
impl<T: fmt::Debug> fmt::Debug for SingleValue<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SingleValue::None => f.debug_struct("SingleValue::None").finish(),
SingleValue::Multiple => f.debug_struct("SingleValue::Multiple").finish(),
SingleValue::Single(v) => f.debug_tuple("SingleValue::Single").field(v).finish(),
}
}
}
impl<T: Clone + Send, S: StreamTrait<Item = T> + Send + Unpin + 'static> From<S> for Stream<T> {
fn from(source: S) -> Self {
Self::new_open(vec![], Box::new(source))
}
}
impl<T: Clone + Send> Default for Stream<T> {
fn default() -> Self {
Self::new_closed(vec![])
}
}
impl<T: Clone + PartialEq + Send> PartialEq for Stream<T> {
// A Stream is equal if it's the same internal pointer, or both streams are
// closed with equivalent values.
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.inner, &other.inner) || {
let left = self.inner.lock().unwrap();
let right = other.inner.lock().unwrap();
match (&*left, &*right) {
(
StreamState {
pulled: a,
source: None,
},
StreamState {
pulled: b,
source: None,
},
) => a == b,
_ => false,
}
}
}
}
impl<T: Clone + Eq + Send> Eq for Stream<T> {}
impl<T: Clone + Serialize + Send> Serialize for Stream<T> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
use serde::ser::Error;
let lock = self.inner.lock().map_err(Error::custom)?;
match &*lock {
StreamState {
pulled,
source: None,
} => pulled.serialize(serializer),
_ => Err(Error::custom("cannot serialize open stream")),
}
}
}
impl<'de, T: Clone + Send + Deserialize<'de>> Deserialize<'de> for Stream<T> {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let data = <Vec<T>>::deserialize(deserializer)?;
Ok(Stream::new_closed(data))
}
}
impl<T: Clone + fmt::Debug> fmt::Debug for StreamState<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("StreamState")
.field("pulled", &self.pulled)
.finish()
}
}
/// Implements [StreamTrait] over our Stream.
#[derive(Debug)]
pub struct StreamRead<T: Clone + Send> {
index: usize,
source: Stream<T>,
}
impl<T: Clone + Send> StreamTrait for StreamRead<T> {
type Item = T;
fn poll_next(self: Pin<&mut Self>, cx: &mut TaskContext<'_>) -> Poll<Option<Self::Item>> {
let this = self.get_mut();
let index = this.index;
let mut inner = this.source.inner.lock().unwrap();
if let Some(v) = inner.pulled.get(index) {
// If the current reader can be satisfied by a value we've already pulled, then
// just do that.
this.index += 1;
return Poll::Ready(Some(v.clone()));
};
let Some(source) = &mut inner.source else {
// If the source has been closed, there's nothing left to pull.
return Poll::Ready(None);
};
match source.poll_next_unpin(cx) {
// If the source stream is ready to give us a new value, we can immediately store that
// and return it to the caller. Any other readers will be able to read the value from
// the already-pulled data.
Poll::Ready(Some(v)) => {
this.index += 1;
inner.pulled.push(v.clone());
Poll::Ready(Some(v))
}
// If the source stream is finished, then we can transition to the closed state
// to drop the source stream.
Poll::Ready(None) => {
inner.source.take();
Poll::Ready(None)
}
// Else, we need to wait for the source stream to give us a new value. The
// source stream will be responsible for waking the TaskContext.
Poll::Pending => Poll::Pending,
}
}
}
|