-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
im-util: Introduce helpers for ObservableVector2
- Loading branch information
Showing
3 changed files
with
448 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//! Utilities around [`ObservableVector`]. | ||
|
||
use eyeball_im::{ObservableVector2, Vector, VectorSubscriber2}; | ||
|
||
mod filter; | ||
|
||
pub use self::filter::{Filter, FilterMap}; | ||
|
||
/// Extension trait for [`ObservableVector`]. | ||
pub trait Vector2Ext<T> | ||
where | ||
T: Clone + Send + Sync + 'static, | ||
{ | ||
/// Obtain a new subscriber that filters items by the given filter function. | ||
/// | ||
/// Returns a filtered version of the current vector, and a subscriber to | ||
/// get updates through. | ||
fn subscribe_filter<F>(&self, f: F) -> (Vector<T>, Filter<VectorSubscriber2<T>, F>) | ||
where | ||
F: Fn(&T) -> bool; | ||
|
||
/// Obtain a new subscriber that filters and maps items with the given | ||
/// function. | ||
/// | ||
/// Returns a filtered + mapped version of the current vector, and a | ||
/// subscriber to get updates through. | ||
fn subscribe_filter_map<U, F>(&self, f: F) -> (Vector<U>, FilterMap<VectorSubscriber2<T>, F>) | ||
where | ||
U: Clone, | ||
F: Fn(T) -> Option<U>; | ||
} | ||
|
||
impl<T> Vector2Ext<T> for ObservableVector2<T> | ||
where | ||
T: Clone + Send + Sync + 'static, | ||
{ | ||
fn subscribe_filter<F>(&self, f: F) -> (Vector<T>, Filter<VectorSubscriber2<T>, F>) | ||
where | ||
F: Fn(&T) -> bool, | ||
{ | ||
Filter::new((*self).clone(), self.subscribe(), f) | ||
} | ||
|
||
fn subscribe_filter_map<U, F>(&self, f: F) -> (Vector<U>, FilterMap<VectorSubscriber2<T>, F>) | ||
where | ||
U: Clone, | ||
F: Fn(T) -> Option<U>, | ||
{ | ||
FilterMap::new((*self).clone(), self.subscribe(), f) | ||
} | ||
} |
Oops, something went wrong.