eyeball_im_util/vector/limit.rs
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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
use arrayvec::ArrayVec;
use std::{
cmp::{min, Ordering},
mem,
pin::Pin,
task::{self, ready, Poll},
};
use super::{
VectorDiffContainer, VectorDiffContainerOps, VectorDiffContainerStreamElement,
VectorDiffContainerStreamLimitBuf, VectorObserver,
};
use eyeball_im::VectorDiff;
use futures_core::Stream;
use imbl::Vector;
use pin_project_lite::pin_project;
pin_project! {
/// A [`VectorDiff`] stream adapter that presents a limited view of the
/// underlying [`ObservableVector`]s items.
///
/// For example, let `S` be a `Stream<Item = VectorDiff>`. The [`Vector`]
/// represented by `S` can have any length, but one may want to virtually
/// _limit_ this `Vector` to a certain size. Then this `Limit` adapter is
/// appropriate.
///
/// An internal buffered vector is kept so that the adapter knows which
/// values can be added when the limit is increased, or when values are
/// removed and new values must be inserted. This fact is important if the
/// items of the `Vector` have a non-negligible size.
///
/// It's okay to have a limit larger than the length of the observed
/// `Vector`.
///
/// [`ObservableVector`]: eyeball_im::ObservableVector
#[project = LimitProj]
pub struct Limit<S, L>
where
S: Stream,
S::Item: VectorDiffContainer,
{
// The main stream to poll items from.
#[pin]
inner_stream: S,
// The limit stream to poll new limits from.
#[pin]
limit_stream: L,
// The buffered vector that is updated with the main stream's items.
// It's used to provide missing items, e.g. when the limit increases.
buffered_vector: Vector<VectorDiffContainerStreamElement<S>>,
// The current limit.
limit: usize,
// This adapter is not a basic filter: It can produce up to two items
// per item of the underlying stream.
//
// Thus, if the item type is just `VectorDiff<_>` (non-bached, can't
// just add diffs to a poll_next result), we need a buffer to store the
// possible extra item in. For example if the vector is [10, 11, 12]
// with a limit of 2 on top: if an item is popped at the front then 10
// is removed, but 12 has to be pushed back as it "enters" the "view".
// That second `PushBack` diff is buffered here.
ready_values: VectorDiffContainerStreamLimitBuf<S>,
}
}
impl<S> Limit<S, EmptyLimitStream>
where
S: Stream,
S::Item: VectorDiffContainer,
{
/// Create a new [`Limit`] with the given (unlimited) initial values,
/// stream of `VectorDiff` updates for those values, and a fixed limit.
///
/// Returns the truncated initial values as well as a stream of updates that
/// ensure that the resulting vector never exceeds the given limit.
pub fn new(
initial_values: Vector<VectorDiffContainerStreamElement<S>>,
inner_stream: S,
limit: usize,
) -> (Vector<VectorDiffContainerStreamElement<S>>, Self) {
Self::dynamic_with_initial_limit(initial_values, inner_stream, limit, EmptyLimitStream)
}
}
impl<S, L> Limit<S, L>
where
S: Stream,
S::Item: VectorDiffContainer,
L: Stream<Item = usize>,
{
/// Create a new [`Limit`] with the given (unlimited) initial values, stream
/// of `VectorDiff` updates for those values, and a stream of limits.
///
/// This is equivalent to `dynamic_with_initial_limit` where the
/// `initial_limit` is 0, except that it doesn't return the limited
/// vector as it would be empty anyways.
///
/// Note that the returned `Limit` won't produce anything until the first
/// limit is produced by the limit stream.
pub fn dynamic(
initial_values: Vector<VectorDiffContainerStreamElement<S>>,
inner_stream: S,
limit_stream: L,
) -> Self {
Self {
inner_stream,
limit_stream,
buffered_vector: initial_values,
limit: 0,
ready_values: Default::default(),
}
}
/// Create a new [`Limit`] with the given (unlimited) initial values, stream
/// of `VectorDiff` updates for those values, and an initial limit as well
/// as a stream of new limits.
pub fn dynamic_with_initial_limit(
mut initial_values: Vector<VectorDiffContainerStreamElement<S>>,
inner_stream: S,
initial_limit: usize,
limit_stream: L,
) -> (Vector<VectorDiffContainerStreamElement<S>>, Self) {
let buffered_vector = initial_values.clone();
if initial_limit < initial_values.len() {
initial_values.truncate(initial_limit);
}
let stream = Self {
inner_stream,
limit_stream,
buffered_vector,
limit: initial_limit,
ready_values: Default::default(),
};
(initial_values, stream)
}
}
impl<S, L> Stream for Limit<S, L>
where
S: Stream,
S::Item: VectorDiffContainer,
L: Stream<Item = usize>,
{
type Item = S::Item;
fn poll_next(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Option<Self::Item>> {
self.project().poll_next(cx)
}
}
impl<S, L> VectorObserver<VectorDiffContainerStreamElement<S>> for Limit<S, L>
where
S: Stream,
S::Item: VectorDiffContainer,
L: Stream<Item = usize>,
{
type Stream = Self;
fn into_parts(self) -> (Vector<VectorDiffContainerStreamElement<S>>, Self::Stream) {
(self.buffered_vector.clone(), self)
}
}
impl<S, L> LimitProj<'_, S, L>
where
S: Stream,
S::Item: VectorDiffContainer,
L: Stream<Item = usize>,
{
fn poll_next(&mut self, cx: &mut task::Context<'_>) -> Poll<Option<S::Item>> {
loop {
// First off, if any values are ready, return them.
if let Some(value) = S::Item::pop_from_limit_buf(self.ready_values) {
return Poll::Ready(Some(value));
}
// Poll a new limit from `limit_stream` before polling `inner_stream`.
while let Poll::Ready(Some(next_limit)) = self.limit_stream.as_mut().poll_next(cx) {
// We have new `VectorDiff`s after the limit has been updated.
// Return them.
if let Some(diffs) = self.update_limit(next_limit) {
return Poll::Ready(Some(diffs));
}
// If update_limit returned None, poll the limit stream again.
}
// Poll `VectorDiff`s from the `inner_stream`.
let Some(diffs) = ready!(self.inner_stream.as_mut().poll_next(cx)) else {
return Poll::Ready(None);
};
// Consume and apply the diffs if possible.
let ready = diffs.push_into_limit_buf(self.ready_values, |diff| {
let limit = *self.limit;
let prev_len = self.buffered_vector.len();
// Update the `buffered_vector`. It's a replica of the original observed
// `Vector`. We need to maintain it in order to be able to produce valid
// `VectorDiff`s when items are missing.
update_buffered_vector(&diff, self.buffered_vector);
handle_diff(diff, limit, prev_len, self.buffered_vector)
});
if let Some(diff) = ready {
return Poll::Ready(Some(diff));
}
// Else loop and poll the streams again.
}
}
/// Update the limit if necessary.
///
/// * If the buffered vector is empty, it returns `None`.
/// * If the limit increases, a `VectorDiff::Append` is produced if any
/// items exist.
/// * If the limit decreases below the length of the vector, a
/// `VectorDiff::Truncate` is produced.
///
/// It's OK to have a `new_limit` larger than the length of the `Vector`.
/// The `new_limit` won't be capped.
fn update_limit(&mut self, new_limit: usize) -> Option<S::Item> {
// Let's update the limit.
let old_limit = mem::replace(self.limit, new_limit);
if self.buffered_vector.is_empty() {
// If empty, nothing to do.
return None;
}
match old_limit.cmp(&new_limit) {
// old < new
Ordering::Less => {
let missing_items = self
.buffered_vector
.iter()
.skip(old_limit)
.take(new_limit - old_limit)
.cloned()
.collect::<Vector<_>>();
if missing_items.is_empty() {
None
} else {
// Let's add the missing items.
Some(S::Item::from_item(VectorDiff::Append { values: missing_items }))
}
}
// old > new
Ordering::Greater => {
if self.buffered_vector.len() <= new_limit {
None
} else {
// Let's remove the extra items.
Some(S::Item::from_item(VectorDiff::Truncate { length: new_limit }))
}
}
// old == new
Ordering::Equal => {
// Nothing to do.
None
}
}
}
}
/// An empty stream with an item type of `usize`.
#[derive(Debug)]
#[non_exhaustive]
pub struct EmptyLimitStream;
impl Stream for EmptyLimitStream {
type Item = usize;
fn poll_next(self: Pin<&mut Self>, _cx: &mut task::Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Ready(None)
}
}
fn update_buffered_vector<T: Clone>(diff: &VectorDiff<T>, buffered_vector: &mut Vector<T>) {
match diff {
VectorDiff::Append { values } => buffered_vector.append(values.clone()),
VectorDiff::Clear => buffered_vector.clear(),
VectorDiff::PushFront { value } => buffered_vector.push_front(value.clone()),
VectorDiff::PushBack { value } => buffered_vector.push_back(value.clone()),
VectorDiff::PopFront => {
buffered_vector.pop_front();
}
VectorDiff::PopBack => {
buffered_vector.pop_back();
}
VectorDiff::Insert { index, value } => {
buffered_vector.insert(*index, value.clone());
}
VectorDiff::Set { index, value } => {
buffered_vector.set(*index, value.clone());
}
VectorDiff::Remove { index } => {
buffered_vector.remove(*index);
}
VectorDiff::Truncate { length } => buffered_vector.truncate(*length),
VectorDiff::Reset { values } => {
*buffered_vector = values.clone();
}
}
}
fn handle_diff<T: Clone>(
diff: VectorDiff<T>,
limit: usize,
prev_len: usize,
buffered_vector: &Vector<T>,
) -> ArrayVec<VectorDiff<T>, 2> {
// If the limit is zero, we have nothing to do.
if limit == 0 {
return ArrayVec::new();
}
let is_full = prev_len >= limit;
let mut res = ArrayVec::new();
match diff {
VectorDiff::Append { mut values } => {
if is_full {
// Ignore the diff.
} else {
// Truncate the `values` to fit inside the free space.
values.truncate(min(limit - prev_len, values.len()));
res.push(VectorDiff::Append { values });
}
}
VectorDiff::Clear => {
res.push(VectorDiff::Clear);
}
VectorDiff::PushFront { value } => {
if is_full {
// Create 1 free space.
res.push(VectorDiff::PopBack);
}
// There is space for this new item.
res.push(VectorDiff::PushFront { value });
}
VectorDiff::PushBack { value } => {
if is_full {
// Ignore the diff.
} else {
// There is space for this new item.
res.push(VectorDiff::PushBack { value });
}
}
VectorDiff::PopFront => {
res.push(VectorDiff::PopFront);
if let Some(diff) = buffered_vector.get(limit - 1) {
// There is a previously-truncated item, push back.
res.push(VectorDiff::PushBack { value: diff.clone() });
}
}
VectorDiff::PopBack => {
if prev_len > limit {
// Pop back outside the limit, ignore the diff.
} else {
res.push(VectorDiff::PopBack);
}
}
VectorDiff::Insert { index, value } => {
if index >= limit {
// Insert after `limit`, ignore the diff.
} else {
if is_full {
// Create 1 free space.
res.push(VectorDiff::PopBack);
}
// There is space for this new item.
res.push(VectorDiff::Insert { index, value });
}
}
VectorDiff::Set { index, value } => {
if index >= limit {
// Update after `limit`, ignore the diff.
} else {
res.push(VectorDiff::Set { index, value });
}
}
VectorDiff::Remove { index } => {
if index >= limit {
// Remove after `limit`, ignore the diff.
} else {
res.push(VectorDiff::Remove { index });
if let Some(diff) = buffered_vector.get(limit - 1) {
// There is a previously-truncated item, push back.
res.push(VectorDiff::PushBack { value: diff.clone() });
}
}
}
VectorDiff::Truncate { length: new_length } => {
if new_length >= limit {
// Truncate items after `limit`, ignore the diff.
} else {
res.push(VectorDiff::Truncate { length: new_length });
}
}
VectorDiff::Reset { values: mut new_values } => {
if new_values.len() > limit {
// There are too many values, truncate.
new_values.truncate(limit);
}
// There is space for these new items.
res.push(VectorDiff::Reset { values: new_values });
}
}
res
}