Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToComputed impls for primitive types #308

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* `Driver::timezone_offset` (Gets browsers time zone offset in seconds)
* `chrono::NaiveDate` support in `AutoJsJson`
* `LazyCache::<T>::new_resource()` helper
* `ToComputed` impls for primitive types

### Changed

Expand Down
16 changes: 0 additions & 16 deletions crates/vertigo/src/computed/computed_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,3 @@ impl From<&str> for Computed<String> {
Value::new(value.to_string()).to_computed()
}
}

pub trait ToComputed<T: Clone> {
fn to_computed(&self) -> Computed<T>;
}

impl<T: Clone + 'static> ToComputed<T> for Computed<T> {
fn to_computed(&self) -> Computed<T> {
self.clone()
}
}

impl<T: Clone + 'static> ToComputed<T> for &Computed<T> {
fn to_computed(&self) -> Computed<T> {
(*self).clone()
}
}
4 changes: 3 additions & 1 deletion crates/vertigo/src/computed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ mod graph_id;
mod graph_value;
mod reactive;
pub mod struct_mut;
mod to_computed;
mod value;

#[cfg(test)]
mod tests;

pub use auto_map::AutoMap;
pub use computed_box::{Computed, ToComputed};
pub use computed_box::Computed;
pub use dependencies::Dependencies;
pub use drop_resource::DropResource;
pub use graph_id::GraphId;
pub use graph_value::GraphValue;
pub use reactive::Reactive;
pub use to_computed::ToComputed;
pub use value::Value;

/// Allows to create `Computed<T1, T2, ...>` out of `Value<T1>`, `Value<T2>`, ...
Expand Down
67 changes: 67 additions & 0 deletions crates/vertigo/src/computed/to_computed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use super::Computed;

/// A trait allowing converting the type into computed.
///
/// ```rust
/// use vertigo::{ToComputed, transaction};
///
/// let comp_1 = 5.to_computed();
/// let comp_2 = 'x'.to_computed();
/// let comp_3 = false.to_computed();
///
/// transaction(|context| {
/// assert_eq!(comp_1.get(context), 5);
/// assert_eq!(comp_2.get(context), 'x');
/// assert_eq!(comp_3.get(context), false);
/// });
///
/// ```
pub trait ToComputed<T: Clone> {
fn to_computed(&self) -> Computed<T>;
}

impl<T: Clone + 'static> ToComputed<T> for Computed<T> {
fn to_computed(&self) -> Computed<T> {
self.clone()
}
}

impl<T: Clone + 'static> ToComputed<T> for &Computed<T> {
fn to_computed(&self) -> Computed<T> {
(*self).clone()
}
}

macro_rules! impl_to_computed {
($typename: ty) => {
impl ToComputed<$typename> for $typename {
fn to_computed(&self) -> Computed<$typename> {
let value = *self;
Computed::from(move |_| value)
}
}
};
}

impl_to_computed!(i8);
impl_to_computed!(i16);
impl_to_computed!(i32);
impl_to_computed!(i64);
impl_to_computed!(i128);
impl_to_computed!(isize);

impl_to_computed!(u8);
impl_to_computed!(u16);
impl_to_computed!(u32);
impl_to_computed!(u64);
impl_to_computed!(u128);
impl_to_computed!(usize);

impl_to_computed!(f32);
impl_to_computed!(f64);

impl_to_computed!(char);

impl_to_computed!(bool);

impl_to_computed!(());
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl JsJson {
}

fn get_string_size(value: &str) -> u32 {
value.as_bytes().len() as u32
value.len() as u32
}

pub fn write_to(&self, buff: &mut MemoryBlockWrite) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl JsValue {
}

fn get_string_size(value: &str) -> u32 {
value.as_bytes().len() as u32
value.len() as u32
}

fn get_size(&self) -> u32 {
Expand Down
Loading