Skip to content

Commit ce894fc

Browse files
committed
ToComputed impls for primitive types
1 parent 750c622 commit ce894fc

File tree

4 files changed

+71
-17
lines changed

4 files changed

+71
-17
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* `Driver::timezone_offset` (Gets browsers time zone offset in seconds)
1010
* `chrono::NaiveDate` support in `AutoJsJson`
1111
* `LazyCache::<T>::new_resource()` helper
12+
* `ToComputed` impls for primitive types
1213

1314
### Changed
1415

crates/vertigo/src/computed/computed_box.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -191,19 +191,3 @@ impl From<&str> for Computed<String> {
191191
Value::new(value.to_string()).to_computed()
192192
}
193193
}
194-
195-
pub trait ToComputed<T: Clone> {
196-
fn to_computed(&self) -> Computed<T>;
197-
}
198-
199-
impl<T: Clone + 'static> ToComputed<T> for Computed<T> {
200-
fn to_computed(&self) -> Computed<T> {
201-
self.clone()
202-
}
203-
}
204-
205-
impl<T: Clone + 'static> ToComputed<T> for &Computed<T> {
206-
fn to_computed(&self) -> Computed<T> {
207-
(*self).clone()
208-
}
209-
}

crates/vertigo/src/computed/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ mod graph_id;
77
mod graph_value;
88
mod reactive;
99
pub mod struct_mut;
10+
mod to_computed;
1011
mod value;
1112

1213
#[cfg(test)]
1314
mod tests;
1415

1516
pub use auto_map::AutoMap;
16-
pub use computed_box::{Computed, ToComputed};
17+
pub use computed_box::Computed;
1718
pub use dependencies::Dependencies;
1819
pub use drop_resource::DropResource;
1920
pub use graph_id::GraphId;
2021
pub use graph_value::GraphValue;
2122
pub use reactive::Reactive;
23+
pub use to_computed::ToComputed;
2224
pub use value::Value;
2325

2426
/// Allows to create `Computed<T1, T2, ...>` out of `Value<T1>`, `Value<T2>`, ...
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use super::Computed;
2+
3+
/// A trait allowing converting the type into computed.
4+
///
5+
/// ```rust
6+
/// use vertigo::{ToComputed, transaction};
7+
///
8+
/// let comp_1 = 5.to_computed();
9+
/// let comp_2 = 'x'.to_computed();
10+
/// let comp_3 = false.to_computed();
11+
///
12+
/// transaction(|context| {
13+
/// assert_eq!(comp_1.get(context), 5);
14+
/// assert_eq!(comp_2.get(context), 'x');
15+
/// assert_eq!(comp_3.get(context), false);
16+
/// });
17+
///
18+
/// ```
19+
pub trait ToComputed<T: Clone> {
20+
fn to_computed(&self) -> Computed<T>;
21+
}
22+
23+
impl<T: Clone + 'static> ToComputed<T> for Computed<T> {
24+
fn to_computed(&self) -> Computed<T> {
25+
self.clone()
26+
}
27+
}
28+
29+
impl<T: Clone + 'static> ToComputed<T> for &Computed<T> {
30+
fn to_computed(&self) -> Computed<T> {
31+
(*self).clone()
32+
}
33+
}
34+
35+
macro_rules! impl_to_computed {
36+
($typename: ty) => {
37+
impl ToComputed<$typename> for $typename {
38+
fn to_computed(&self) -> Computed<$typename> {
39+
let value = *self;
40+
Computed::from(move |_| value)
41+
}
42+
}
43+
};
44+
}
45+
46+
impl_to_computed!(i8);
47+
impl_to_computed!(i16);
48+
impl_to_computed!(i32);
49+
impl_to_computed!(i64);
50+
impl_to_computed!(i128);
51+
impl_to_computed!(isize);
52+
53+
impl_to_computed!(u8);
54+
impl_to_computed!(u16);
55+
impl_to_computed!(u32);
56+
impl_to_computed!(u64);
57+
impl_to_computed!(u128);
58+
impl_to_computed!(usize);
59+
60+
impl_to_computed!(f32);
61+
impl_to_computed!(f64);
62+
63+
impl_to_computed!(char);
64+
65+
impl_to_computed!(bool);
66+
67+
impl_to_computed!(());

0 commit comments

Comments
 (0)