Selaa lähdekoodia

feat: enable addassign for usestate

Jonathan Kelley 3 vuotta sitten
vanhempi
commit
a4ab2d9de0
3 muutettua tiedostoa jossa 32 lisäystä ja 11 poistoa
  1. 4 7
      README.md
  2. 3 3
      examples/readme.rs
  3. 25 1
      packages/hooks/src/usestate.rs

+ 4 - 7
README.md

@@ -55,17 +55,17 @@ Dioxus is a portable, performant, and ergonomic framework for building cross-pla
 
 
 ```rust
 ```rust
 fn app(cx: Scope) -> Element {
 fn app(cx: Scope) -> Element {
-    let count = use_state(&cx, || 0);
+    let mut count = use_state(&cx, || 0);
 
 
     cx.render(rsx! {
     cx.render(rsx! {
         h1 { "High-Five counter: {count}" }
         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.
 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.
 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?
 ## 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.
 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:
 By using Rust, we gain:
 
 
 - Static types for *every* library
 - Static types for *every* library

+ 3 - 3
examples/readme.rs

@@ -9,11 +9,11 @@ fn main() {
 }
 }
 
 
 fn app(cx: Scope) -> Element {
 fn app(cx: Scope) -> Element {
-    let count = use_state(&cx, || 0);
+    let mut count = use_state(&cx, || 0);
 
 
     cx.render(rsx! {
     cx.render(rsx! {
         h1 { "High-Five counter: {count}" }
         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!" }
     })
     })
 }
 }

+ 25 - 1
packages/hooks/src/usestate.rs

@@ -4,7 +4,7 @@ use dioxus_core::prelude::*;
 use std::{
 use std::{
     cell::{RefCell, RefMut},
     cell::{RefCell, RefMut},
     fmt::{Debug, Display},
     fmt::{Debug, Display},
-    ops::Not,
+    ops::{Add, Div, Mul, Not, Sub},
     rc::Rc,
     rc::Rc,
     sync::Arc,
     sync::Arc,
 };
 };
@@ -390,6 +390,30 @@ impl<T: std::ops::Mul + Copy> std::ops::Mul<T> for &UseState<T> {
     }
     }
 }
 }
 
 
+impl<T: Add<Output = T> + Copy> std::ops::AddAssign<T> for &UseState<T> {
+    fn add_assign(&mut self, rhs: T) {
+        self.set((*self.current()) + rhs);
+    }
+}
+
+impl<T: Sub<Output = T> + Copy> std::ops::SubAssign<T> for &UseState<T> {
+    fn sub_assign(&mut self, rhs: T) {
+        self.set((*self.current()) - rhs);
+    }
+}
+
+impl<T: Mul<Output = T> + Copy> std::ops::MulAssign<T> for &UseState<T> {
+    fn mul_assign(&mut self, rhs: T) {
+        self.set((*self.current()) * rhs);
+    }
+}
+
+impl<T: Div<Output = T> + Copy> std::ops::DivAssign<T> for &UseState<T> {
+    fn div_assign(&mut self, rhs: T) {
+        self.set((*self.current()) / rhs);
+    }
+}
+
 #[test]
 #[test]
 fn api_makes_sense() {
 fn api_makes_sense() {
     #[allow(unused)]
     #[allow(unused)]