From a4ab2d9de0e6d9c5ba7845d9fff78e6b2cdb9fc6 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Sat, 5 Mar 2022 17:07:34 -0500 Subject: [PATCH] feat: enable addassign for usestate --- README.md | 11 ++++------- examples/readme.rs | 6 +++--- packages/hooks/src/usestate.rs | 26 +++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 2f54dfac6..d64ac9c84 100644 --- a/README.md +++ b/README.md @@ -55,17 +55,17 @@ Dioxus is a portable, performant, and ergonomic framework for building cross-pla ```rust fn app(cx: Scope) -> Element { - let count = use_state(&cx, || 0); + let mut count = use_state(&cx, || 0); cx.render(rsx! { h1 { "High-Five counter: {count}" } - button { onclick: move |_| count.set(count + 1), "Up high!" } - button { onclick: move |_| count.set(count - 1), "Down low!" } + button { onclick: move |_| count += 1, "Up high!" } + button { onclick: move |_| count -= 1, "Down low!" } }) } ``` -Dioxus can be used to deliver webapps, desktop apps, static sites, liveview apps, mobile apps (WIP), and more. At its core, Dioxus is entirely renderer agnostic and has great documentation for creating new renderers for any platform. +Dioxus can be used to deliver webapps, desktop apps, static sites, liveview apps, mobile apps, and more. Dioxus is entirely renderer agnostic and can be used as platform for any renderer. If you know React, then you already know Dioxus. @@ -100,13 +100,10 @@ If you know React, then you already know Dioxus. See the [awesome-dioxus](https://github.com/DioxusLabs/awesome-dioxus) page for a curated list of content in the Dioxus Ecosystem. - ## Why Dioxus and why Rust? TypeScript is a fantastic addition to JavaScript, but it's still fundamentally JavaScript. TS code runs slightly slower, has tons of configuration options, and not every package is properly typed. -In contrast, Dioxus is written in Rust - which is almost like "TypeScript on steroids". - By using Rust, we gain: - Static types for *every* library diff --git a/examples/readme.rs b/examples/readme.rs index f5e07d5ba..4f4250343 100644 --- a/examples/readme.rs +++ b/examples/readme.rs @@ -9,11 +9,11 @@ fn main() { } fn app(cx: Scope) -> Element { - let count = use_state(&cx, || 0); + let mut count = use_state(&cx, || 0); cx.render(rsx! { h1 { "High-Five counter: {count}" } - button { onclick: move |_| count.set(count + 1), "Up high!" } - button { onclick: move |_| count.set(count - 1), "Down low!" } + button { onclick: move |_| count += 1, "Up high!" } + button { onclick: move |_| count -= 1, "Down low!" } }) } diff --git a/packages/hooks/src/usestate.rs b/packages/hooks/src/usestate.rs index d9530ee8e..c84dea177 100644 --- a/packages/hooks/src/usestate.rs +++ b/packages/hooks/src/usestate.rs @@ -4,7 +4,7 @@ use dioxus_core::prelude::*; use std::{ cell::{RefCell, RefMut}, fmt::{Debug, Display}, - ops::Not, + ops::{Add, Div, Mul, Not, Sub}, rc::Rc, sync::Arc, }; @@ -390,6 +390,30 @@ impl std::ops::Mul for &UseState { } } +impl + Copy> std::ops::AddAssign for &UseState { + fn add_assign(&mut self, rhs: T) { + self.set((*self.current()) + rhs); + } +} + +impl + Copy> std::ops::SubAssign for &UseState { + fn sub_assign(&mut self, rhs: T) { + self.set((*self.current()) - rhs); + } +} + +impl + Copy> std::ops::MulAssign for &UseState { + fn mul_assign(&mut self, rhs: T) { + self.set((*self.current()) * rhs); + } +} + +impl + Copy> std::ops::DivAssign for &UseState { + fn div_assign(&mut self, rhs: T) { + self.set((*self.current()) / rhs); + } +} + #[test] fn api_makes_sense() { #[allow(unused)]