From d0b62eab3916bc91561b4756a53dea51d53a681e Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 13 Jan 2025 11:05:59 +0100 Subject: [PATCH 1/2] chore: Use `VectorDiff::apply` to replace `update_buffered_vector`. This patch removes `update_buffered_vector` and replaces it by `VectorDiff::apply`. Less code is better, it's basically duplication. --- eyeball-im-util/src/vector/limit.rs | 32 +++-------------------------- 1 file changed, 3 insertions(+), 29 deletions(-) diff --git a/eyeball-im-util/src/vector/limit.rs b/eyeball-im-util/src/vector/limit.rs index eeb13ad..f42e6c6 100644 --- a/eyeball-im-util/src/vector/limit.rs +++ b/eyeball-im-util/src/vector/limit.rs @@ -205,7 +205,9 @@ where // 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); + diff.clone().apply(self.buffered_vector); + + // Handle the `diff`. handle_diff(diff, limit, prev_len, self.buffered_vector) }); @@ -287,34 +289,6 @@ impl Stream for EmptyLimitStream { } } -fn update_buffered_vector(diff: &VectorDiff, buffered_vector: &mut Vector) { - 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( diff: VectorDiff, limit: usize, From 92aadd8bbebf2c17815aa9485ef07f57f8b5ee71 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 13 Jan 2025 11:11:57 +0100 Subject: [PATCH 2/2] feat: `VectorDiff::apply` uses `Vector::append` on `VectorDiff::Append`. Using `Vector::extend` will push back each value from `values` individually in a loop. However, this patch changes that to use `Vector::append` which appends all values at once. It's probably more optimised. --- eyeball-im/src/vector.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eyeball-im/src/vector.rs b/eyeball-im/src/vector.rs index 66778c6..29f129f 100644 --- a/eyeball-im/src/vector.rs +++ b/eyeball-im/src/vector.rs @@ -433,7 +433,7 @@ impl VectorDiff { pub fn apply(self, vec: &mut Vector) { match self { VectorDiff::Append { values } => { - vec.extend(values); + vec.append(values); } VectorDiff::Clear => { vec.clear();