-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
callback.rs
145 lines (126 loc) · 3.66 KB
/
callback.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//! This module contains data types for interacting with `Scope`s.
//!
//! ## Relevant examples
//! - [Counter](https://github.com/yewstack/yew/tree/master/examples/counter)
//! - [Timer](https://github.com/yewstack/yew/tree/master/examples/timer)
use std::fmt;
use std::rc::Rc;
use crate::html::ImplicitClone;
/// Universal callback wrapper.
///
/// An `Rc` wrapper is used to make it cloneable.
pub struct Callback<IN, OUT = ()> {
/// A callback which can be called multiple times
pub(crate) cb: Rc<dyn Fn(IN) -> OUT>,
}
impl<IN, OUT, F: Fn(IN) -> OUT + 'static> From<F> for Callback<IN, OUT> {
fn from(func: F) -> Self {
Callback { cb: Rc::new(func) }
}
}
impl<IN, OUT> Clone for Callback<IN, OUT> {
fn clone(&self) -> Self {
Self {
cb: self.cb.clone(),
}
}
}
#[allow(clippy::vtable_address_comparisons)]
impl<IN, OUT> PartialEq for Callback<IN, OUT> {
fn eq(&self, other: &Callback<IN, OUT>) -> bool {
let (Callback { cb }, Callback { cb: rhs_cb }) = (self, other);
Rc::ptr_eq(cb, rhs_cb)
}
}
impl<IN, OUT> fmt::Debug for Callback<IN, OUT> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Callback<_>")
}
}
impl<IN, OUT> Callback<IN, OUT> {
/// This method calls the callback's function.
pub fn emit(&self, value: IN) -> OUT {
(*self.cb)(value)
}
}
impl<IN> Callback<IN> {
/// Creates a "no-op" callback which can be used when it is not suitable to use an
/// `Option<Callback>`.
pub fn noop() -> Self {
Self::from(|_| ())
}
}
impl<IN> Default for Callback<IN> {
fn default() -> Self {
Self::noop()
}
}
impl<IN: 'static, OUT: 'static> Callback<IN, OUT> {
/// Creates a new callback from another callback and a function
/// That when emitted will call that function and will emit the original callback
pub fn reform<F, T>(&self, func: F) -> Callback<T, OUT>
where
F: Fn(T) -> IN + 'static,
{
let this = self.clone();
let func = move |input| {
let output = func(input);
this.emit(output)
};
Callback::from(func)
}
/// Creates a new callback from another callback and a function
/// That when emitted will call that function and will emit the original callback if it is
/// not `None`.
pub fn filter_reform<F, T>(&self, func: F) -> Callback<T, OUT>
where
F: Fn(T) -> Option<IN> + 'static,
{
let this = self.clone();
let func = move |input| {
if let Some(output) = func(input) {
this.emit(output);
}
};
Callback::from(func)
}
}
impl<IN, OUT> ImplicitClone for Callback<IN, OUT> {}
#[cfg(test)]
mod test {
use super::*;
/// emit the callback with the provided value
fn emit<I, T, F, R>(values: I, f: F) -> Vec<R>
where
I: IntoIterator<Item = T>,
F: FnOnce(Callback<R>) -> Callback<T>,
{
let mut result: Vec<T> = Vec::new();
for value in values {
f(Callback::from(|v| result.push(v))).emit(value);
}
result
}
#[test]
fn test_callback() {
assert_eq!(emit([true, false], |cb| cb), vec![true, false]);
}
#[test]
fn test_reform() {
assert_eq!(
emit([true, false], |cb| cb.reform(|v| !v)),
vec![false, true]
);
}
#[test]
fn test_filter_reform() {
assert_eq!(
emit([1, 2, 3], |cb| cb.filter_reform(|v| match v {
1 => Some(true),
2 => Some(false),
_ => None,
})),
vec![true, false]
);
}
}