Skip to content

Commit

Permalink
!fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Sep 13, 2023
1 parent af206ef commit 889e223
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 2 deletions.
12 changes: 11 additions & 1 deletion eyeball-im-util/src/vector/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ where
L: Stream<Item = usize>,
{
fn poll_next(&mut self, cx: &mut task::Context<'_>) -> Poll<Option<S::Item>> {
// First off, if any value is ready, let's return it.
if let Some(ready_value) = self.ready_values.pop_front() {
return Poll::Ready(Some(ready_value));
}

// Let's first poll a new limit from `limit_stream`.
// Let's poll a new limit from `limit_stream` before polling `inner_stream`.
let diff_after_limit_update = match self.limit_stream.as_mut().poll_next(cx) {
// There is a new limit!
Poll::Ready(Some(next_limit)) => self.update_limit(next_limit),
Expand Down Expand Up @@ -215,6 +216,15 @@ where
// Remove after `limit`, let's ignore the diff.
} else {
self.push_ready_value(VectorDiff::Remove { index });

if length > limit {
self.push_ready_value(VectorDiff::PushBack {
// SAFETY: It's safe to `unwrap` here as we are sure a value exists at
// index `limit - 1`. We are also sure that
// `limit > 1`.
value: self.buffered_vector.get(limit - 1).unwrap().clone(),
});
}
}
}
VectorDiff::Truncate { length: new_length } => {
Expand Down
105 changes: 104 additions & 1 deletion eyeball-im-util/tests/it/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ fn insert() {
let mut sub =
DynamicLimit::new(ob.clone(), ob.subscribe().into_stream(), Observable::subscribe(&limit));

// Add 4 values.
// Add 2 values.
ob.append(vector![10, 11]);

// Set limit to 3.
Expand Down Expand Up @@ -525,3 +525,106 @@ fn insert() {
drop(ob);
assert_closed!(sub);
}

#[test]
fn set() {
let mut ob = ObservableVector::<usize>::new();
let mut limit = Observable::new(0);
let mut sub =
DynamicLimit::new(ob.clone(), ob.subscribe().into_stream(), Observable::subscribe(&limit));

// Add 3 values.
ob.append(vector![10, 11, 12]);

// Set limit to 2.
Observable::set(&mut limit, 2);

// Observe 2 values.
assert_next_eq!(sub, VectorDiff::Append { values: vector![10, 11] });

// Set 1 value.
ob.set(0, 20);

// Observe 1 update.
assert_next_eq!(sub, VectorDiff::Set { index: 0, value: 20 });

// Set 1 value at the limit.
ob.set(1, 21);

// Observe 1 update.
assert_next_eq!(sub, VectorDiff::Set { index: 1, value: 21 });

// Set 1 value after the limit.
ob.set(2, 22);

// Observe nothing.
assert_pending!(sub);

// Check the content of the vector.
{
let expected = vector![20, 21, 22];
assert_eq!(*ob, expected);

Observable::set(&mut limit, 0);
assert_next_eq!(sub, VectorDiff::Truncate { length: 0 });

Observable::set(&mut limit, 42);
assert_next_eq!(sub, VectorDiff::Append { values: expected });
}

drop(ob);
assert_closed!(sub);
}

#[test]
fn remove() {
let mut ob = ObservableVector::<usize>::new();
let mut limit = Observable::new(0);
let mut sub =
DynamicLimit::new(ob.clone(), ob.subscribe().into_stream(), Observable::subscribe(&limit));

// Add 5 values.
ob.append(vector![10, 11, 12, 13, 14]);

// Set limit to 2.
Observable::set(&mut limit, 2);

// Observe 2 values.
assert_next_eq!(sub, VectorDiff::Append { values: vector![10, 11] });

// Remove 1 value after the limit.
ob.remove(3);

// Observe nothing.
assert_pending!(sub);

// Remove 1 value at the limit.
ob.remove(1);

// Observe 1 value being removed, and 1 new value being pushed back.
assert_next_eq!(sub, VectorDiff::Remove { index: 1 });
assert_next_eq!(sub, VectorDiff::PushBack { value: 12 });

// Remove 1 value.
ob.remove(0);

// Observe 1 value being removed, and 1 new value being pushed back.
assert_next_eq!(sub, VectorDiff::Remove { index: 0 });
assert_next_eq!(sub, VectorDiff::PushBack { value: 14 });
assert_pending!(sub);

// Check the content of the vector.
{
let expected = vector![12, 14];
assert_eq!(*ob, expected);

Observable::set(&mut limit, 0);
assert_next_eq!(sub, VectorDiff::Truncate { length: 0 });

Observable::set(&mut limit, 42);
assert_next_eq!(sub, VectorDiff::Append { values: expected });
}

drop(ob);
assert_closed!(sub);
}

0 comments on commit 889e223

Please sign in to comment.