Skip to content

Commit

Permalink
SignalExt split to Clone and Copy functions
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-kolarik committed Aug 17, 2023
1 parent 3bbb3aa commit b94f1d6
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ repository = "https://github.com/martin-kolarik/futures-signals-ext"
homepage = "https://github.com/martin-kolarik/futures-signals-ext"

[features]
default = ["option", "spawn", "spawn_local"]
default = ["option", "spawn", "spawn-local"]
option = []
spawn = ["async-global-executor"]
spawn_local = ["async-global-executor"]
spawn-local = ["async-global-executor"]

[dependencies]
async-global-executor = { version = "^2.3", default-features = false, optional = true }
Expand Down
140 changes: 136 additions & 4 deletions src/futures_signals_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ pub trait MutableVecExt<A> {
fn inspect_mut(&self, f: impl FnOnce(&mut MutableVecLockMut<A>));

fn find_inspect_mut<P, F>(&self, predicate: P, f: F) -> Option<bool>
where
A: Copy,
P: FnMut(&A) -> bool,
F: FnOnce(&mut A) -> bool;

fn find_inspect_mut_cloned<P, F>(&self, predicate: P, f: F) -> Option<bool>
where
A: Clone,
P: FnMut(&A) -> bool,
Expand All @@ -92,34 +98,73 @@ pub trait MutableVecExt<A> {
F: FnMut(&A) -> Option<U>;

fn signal_vec_filter<P>(&self, p: P) -> impl SignalVec<Item = A>
where
A: Copy,
P: FnMut(&A) -> bool;

fn signal_vec_filter_cloned<P>(&self, p: P) -> impl SignalVec<Item = A>
where
A: Clone,
P: FnMut(&A) -> bool;

fn signal_for_item<P>(&self, p: P) -> impl Signal<Item = Option<A>>
where
A: Copy,
P: FnMut(&A) -> bool;

fn signal_for_item_cloned<P>(&self, p: P) -> impl Signal<Item = Option<A>>
where
A: Clone,
P: FnMut(&A) -> bool;

fn signal_for_item_map<P, F, U>(&self, p: P, f: F) -> impl Signal<Item = Option<U>>
where
A: Copy,
P: FnMut(&A) -> bool,
F: FnMut(&A) -> U;

fn signal_for_item_map_cloned<P, F, U>(&self, p: P, f: F) -> impl Signal<Item = Option<U>>
where
A: Clone,
P: FnMut(&A) -> bool,
F: FnMut(&A) -> U;

fn signal_vec_filter_signal<P, S>(&self, p: P) -> impl SignalVec<Item = A>
where
A: Copy,
P: FnMut(&A) -> S,
S: Signal<Item = bool>;

fn signal_vec_filter_signal_cloned<P, S>(&self, p: P) -> impl SignalVec<Item = A>
where
A: Clone,
P: FnMut(&A) -> S,
S: Signal<Item = bool>;

fn signal_for_item_signal<P, S>(&self, p: P) -> impl Signal<Item = Option<A>>
where
A: Copy,
P: FnMut(&A) -> S,
S: Signal<Item = bool>;

fn signal_for_item_signal_cloned<P, S>(&self, p: P) -> impl Signal<Item = Option<A>>
where
A: Clone,
P: FnMut(&A) -> S,
S: Signal<Item = bool>;

fn signal_for_item_signal_map<P, F, S, U>(&self, p: P, f: F) -> impl Signal<Item = Option<U>>
where
A: Copy,
P: FnMut(&A) -> S,
F: FnMut(&A) -> U + 'static,
S: Signal<Item = bool>;

fn signal_for_item_signal_map_cloned<P, F, S, U>(
&self,
p: P,
f: F,
) -> impl Signal<Item = Option<U>>
where
A: Clone,
P: FnMut(&A) -> S,
Expand All @@ -140,6 +185,32 @@ impl<A> MutableVecExt<A> for MutableVec<A> {
/// and cause MutableVec change. If F returns false, no change is induced neither
/// reported.
fn find_inspect_mut<P, F>(&self, predicate: P, f: F) -> Option<bool>
where
A: Copy,
P: FnMut(&A) -> bool,
F: FnOnce(&mut A) -> bool,
{
let mut lock = self.lock_mut();
if let Some((index, mut item)) = lock
.iter()
.position(predicate)
.and_then(|index| lock.get(index).map(|item| (index, *item)))
{
if f(&mut item) {
lock.set(index, item);
Some(true)
} else {
Some(false)
}
} else {
None
}
}

/// Return parameter of F (changed) drives if the value should be written back,
/// and cause MutableVec change. If F returns false, no change is induced neither
/// reported.
fn find_inspect_mut_cloned<P, F>(&self, predicate: P, f: F) -> Option<bool>
where
A: Clone,
P: FnMut(&A) -> bool,
Expand Down Expand Up @@ -185,6 +256,14 @@ impl<A> MutableVecExt<A> for MutableVec<A> {
}

fn signal_vec_filter<P>(&self, p: P) -> impl SignalVec<Item = A>
where
A: Copy,
P: FnMut(&A) -> bool,
{
self.signal_vec().filter(p)
}

fn signal_vec_filter_cloned<P>(&self, p: P) -> impl SignalVec<Item = A>
where
A: Clone,
P: FnMut(&A) -> bool,
Expand All @@ -194,24 +273,52 @@ impl<A> MutableVecExt<A> for MutableVec<A> {

#[inline]
fn signal_for_item<P>(&self, p: P) -> impl Signal<Item = Option<A>>
where
A: Copy,
P: FnMut(&A) -> bool,
{
self.signal_for_item_map(p, |i| *i)
}

#[inline]
fn signal_for_item_cloned<P>(&self, p: P) -> impl Signal<Item = Option<A>>
where
A: Clone,
P: FnMut(&A) -> bool,
{
self.signal_for_item_map(p, |i| i.clone())
self.signal_for_item_map_cloned(p, |i| i.clone())
}

fn signal_for_item_map<P, F, U>(&self, p: P, mut f: F) -> impl Signal<Item = Option<U>>
where
A: Clone,
A: Copy,
P: FnMut(&A) -> bool,
F: FnMut(&A) -> U,
{
self.signal_vec_filter(p)
.to_signal_map(move |e| e.first().map(&mut f))
}

fn signal_for_item_map_cloned<P, F, U>(&self, p: P, mut f: F) -> impl Signal<Item = Option<U>>
where
A: Clone,
P: FnMut(&A) -> bool,
F: FnMut(&A) -> U,
{
self.signal_vec_filter_cloned(p)
.to_signal_map(move |e| e.first().map(&mut f))
}

fn signal_vec_filter_signal<P, S>(&self, p: P) -> impl SignalVec<Item = A>
where
A: Copy,
P: FnMut(&A) -> S,
S: Signal<Item = bool>,
{
self.signal_vec().filter_signal_cloned(p)
}

fn signal_vec_filter_signal_cloned<P, S>(&self, p: P) -> impl SignalVec<Item = A>
where
A: Clone,
P: FnMut(&A) -> S,
Expand All @@ -222,12 +329,22 @@ impl<A> MutableVecExt<A> for MutableVec<A> {

#[inline]
fn signal_for_item_signal<P, S>(&self, p: P) -> impl Signal<Item = Option<A>>
where
A: Copy,
P: FnMut(&A) -> S,
S: Signal<Item = bool>,
{
self.signal_for_item_signal_map(p, |i| *i)
}

#[inline]
fn signal_for_item_signal_cloned<P, S>(&self, p: P) -> impl Signal<Item = Option<A>>
where
A: Clone,
P: FnMut(&A) -> S,
S: Signal<Item = bool>,
{
self.signal_for_item_signal_map(p, |i| i.clone())
self.signal_for_item_signal_map_cloned(p, |i| i.clone())
}

fn signal_for_item_signal_map<P, F, S, U>(
Expand All @@ -236,14 +353,29 @@ impl<A> MutableVecExt<A> for MutableVec<A> {
mut f: F,
) -> impl Signal<Item = Option<U>>
where
A: Clone,
A: Copy,
P: FnMut(&A) -> S,
F: FnMut(&A) -> U + 'static,
S: Signal<Item = bool>,
{
self.signal_vec_filter_signal(p)
.to_signal_map(move |items| items.first().map(&mut f))
}

fn signal_for_item_signal_map_cloned<P, F, S, U>(
&self,
p: P,
mut f: F,
) -> impl Signal<Item = Option<U>>
where
A: Clone,
P: FnMut(&A) -> S,
F: FnMut(&A) -> U + 'static,
S: Signal<Item = bool>,
{
self.signal_vec_filter_signal_cloned(p)
.to_signal_map(move |items| items.first().map(&mut f))
}
}

pub trait SignalExtMapBool {
Expand Down

0 comments on commit b94f1d6

Please sign in to comment.