Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement the
Sort
stream adapter.
`Sort` is `VectorDiff` stream adapter that presents a sorted view of the underlying `ObservableVector` items. ```rust use eyeball_im::{ObservableVector, VectorDiff}; use eyeball_im_util::vector::VectorObserverExt; use imbl::vector; use std::cmp::Ordering; use stream_assert::{assert_closed, assert_next_eq, assert_pending}; // A comparison function that is used to sort our // `ObservableVector` values. fn cmp<T>(left: &T, right: &T) -> Ordering where T: Ord, { left.cmp(right) } // Our vector. let mut ob = ObservableVector::<char>::new(); let (values, mut sub) = ob.subscribe().sort(cmp); // ^^^ // | our comparison function assert!(values.is_empty()); assert_pending!(sub); // Append multiple unsorted values. ob.append(vector!['d', 'b', 'e']); // We get a `VectorDiff::Append` with sorted values! assert_next_eq!(sub, VectorDiff::Append { values: vector!['b', 'd', 'e'] }); // Let's recap what we have. `ob` is our `ObservableVector`, // `sub` is the “sorted view”/“sorted stream” of `ob`: // | `ob` | d b e | // | `sub` | b d e | // Append other multiple values. ob.append(vector!['f', 'g', 'a', 'c']); // We get three `VectorDiff`s! assert_next_eq!(sub, VectorDiff::PushFront { value: 'a' }); assert_next_eq!(sub, VectorDiff::Insert { index: 2, value: 'c' }); assert_next_eq!(sub, VectorDiff::Append { values: vector!['f', 'g'] }); // Let's recap what we have: // | `ob` | d b e f g a c | // | `sub` | a b c d e f g | // ^ ^ ^^^ // | | | // | | with `VectorDiff::Append { .. }` // | with `VectorDiff::Insert { index: 2, .. }` // with `VectorDiff::PushFront { .. }` // Technically, `Sort` emits `VectorDiff` that mimics a sorted `Vector`. drop(ob); assert_closed!(sub); ```
- Loading branch information