diff --git a/crates/yew-hooks/src/hooks/use_async.rs b/crates/yew-hooks/src/hooks/use_async.rs
index 9776867..f3ce130 100644
--- a/crates/yew-hooks/src/hooks/use_async.rs
+++ b/crates/yew-hooks/src/hooks/use_async.rs
@@ -14,13 +14,13 @@ pub struct UseAsyncOptions {
impl UseAsyncOptions {
/// Automatically run when mount
- pub fn enable_auto() -> Self {
+ pub const fn enable_auto() -> Self {
Self { auto: true }
}
}
/// State for an async future.
-#[derive(PartialEq)]
+#[derive(PartialEq, Eq)]
pub struct UseAsyncState {
pub loading: bool,
pub data: Option,
@@ -36,7 +36,7 @@ pub struct UseAsyncHandle {
impl UseAsyncHandle {
/// Start to resolve the async future to a final value.
pub fn run(&self) {
- (self.run)()
+ (self.run)();
}
/// Update `data` directly.
@@ -53,7 +53,7 @@ impl Deref for UseAsyncHandle {
type Target = UseAsyncState;
fn deref(&self) -> &Self::Target {
- &(*self.inner)
+ &self.inner
}
}
@@ -216,8 +216,8 @@ where
// Only set loading to true and leave previous data/error alone.
inner.set(UseAsyncState {
loading: true,
- data: (*inner).data.clone(),
- error: (*inner).error.clone(),
+ data: inner.data.clone(),
+ error: inner.error.clone(),
});
match future.await {
// Success with some data and clear previous error.
@@ -229,7 +229,7 @@ where
// Failed with some error and leave previous data alone.
Err(error) => inner.set(UseAsyncState {
loading: false,
- data: (*inner).data.clone(),
+ data: inner.data.clone(),
error: Some(error),
}),
}
diff --git a/crates/yew-hooks/src/hooks/use_before_unload.rs b/crates/yew-hooks/src/hooks/use_before_unload.rs
index ada9d8e..ff3ad26 100644
--- a/crates/yew-hooks/src/hooks/use_before_unload.rs
+++ b/crates/yew-hooks/src/hooks/use_before_unload.rs
@@ -36,5 +36,5 @@ pub fn use_before_unload(enabled: bool, msg: String) {
// WebKit-derived browsers don't follow the spec for the dialog box.
// We should return msg in the future for the event handler.
// https://developer.mozilla.org/en-US/docs/Web/API/BeforeUnloadEvent
- })
+ });
}
diff --git a/crates/yew-hooks/src/hooks/use_clipboard.rs b/crates/yew-hooks/src/hooks/use_clipboard.rs
index 4212234..b0de3e8 100644
--- a/crates/yew-hooks/src/hooks/use_clipboard.rs
+++ b/crates/yew-hooks/src/hooks/use_clipboard.rs
@@ -32,22 +32,22 @@ pub struct UseClipboardHandle {
impl UseClipboardHandle {
/// Read bytes from clipboard.
pub fn read(&self) {
- (self.read)()
+ (self.read)();
}
/// Read text from clipboard.
pub fn read_text(&self) {
- (self.read_text)()
+ (self.read_text)();
}
/// Write bytes with mime type to clipboard.
pub fn write(&self, data: Vec, mime_type: Option) {
- (self.write)(data, mime_type)
+ (self.write)(data, mime_type);
}
/// Write text to clipboard.
pub fn write_text(&self, data: String) {
- (self.write_text)(data)
+ (self.write_text)(data);
}
}
@@ -205,7 +205,7 @@ pub fn use_clipboard() -> UseClipboardHandle {
let resolve_closure = Closure::wrap(Box::new(move |_| {
bytes.set(Some(data.clone()));
bytes_mime_type.set(mime_type.clone());
- copied.set(true)
+ copied.set(true);
})
as Box);
let reject_closure = Closure::wrap(Box::new(move |_| {
@@ -233,15 +233,18 @@ pub fn use_clipboard() -> UseClipboardHandle {
let text = text.clone();
let text2 = text.clone();
let resolve_closure = Closure::wrap(Box::new(move |data: JsValue| {
- if let Some(data) = data.as_string() {
- if data.is_empty() {
+ data.as_string().map_or_else(
+ || {
text.set(None);
- } else {
- text.set(Some(data));
- }
- } else {
- text.set(None);
- }
+ },
+ |data| {
+ if data.is_empty() {
+ text.set(None);
+ } else {
+ text.set(Some(data));
+ }
+ },
+ );
}) as Box);
let reject_closure = Closure::wrap(Box::new(move |_| {
text2.set(None);
@@ -268,73 +271,92 @@ pub fn use_clipboard() -> UseClipboardHandle {
let items = Array::from(&items);
let bytes = bytes.clone();
for item in items.iter() {
- if let Ok(item) = item.dyn_into::() {
- for t in item.types().iter() {
- if let Some(t) = t.as_string() {
- let bytes = bytes.clone();
- let bytes2 = bytes.clone();
- let bytes_mime_type = bytes_mime_type.clone();
- let bytes_mime_type2 = bytes_mime_type.clone();
- let t2 = t.clone();
- let resolve_closure =
- Closure::wrap(Box::new(move |blob: JsValue| {
- if let Ok(blob) = blob.dyn_into::() {
- let bytes = bytes.clone();
- let bytes2 = bytes.clone();
- let bytes_mime_type = bytes_mime_type.clone();
- let bytes_mime_type2 = bytes_mime_type.clone();
- let t = t.clone();
- let resolve_closure = Closure::wrap(Box::new(
- move |buffer: JsValue| {
- if let Ok(buffer) =
- buffer.dyn_into::()
- {
- let data =
- Uint8Array::new(&buffer).to_vec();
- bytes.set(Some(data));
- bytes_mime_type.set(Some(t.clone()));
- } else {
+ item.dyn_into::().map_or_else(
+ |_| {
+ bytes.set(None);
+ bytes_mime_type.set(None);
+ },
+ |item| {
+ for t in item.types().iter() {
+ t.as_string().map_or_else(
+ || {
+ bytes.set(None);
+ bytes_mime_type.set(None);
+ },
+ |t| {
+ let bytes = bytes.clone();
+ let bytes2 = bytes.clone();
+ let bytes_mime_type = bytes_mime_type.clone();
+ let bytes_mime_type2 = bytes_mime_type.clone();
+ let t2 = t.clone();
+ let resolve_closure =
+ Closure::wrap(Box::new(move |blob: JsValue| {
+ blob.dyn_into::().map_or_else(
+ |_| {
bytes.set(None);
bytes_mime_type.set(None);
- }
- },
- )
+ },
+ |blob| {
+ let bytes = bytes.clone();
+ let bytes2 = bytes.clone();
+ let bytes_mime_type =
+ bytes_mime_type.clone();
+ let bytes_mime_type2 =
+ bytes_mime_type.clone();
+ let t = t.clone();
+ let resolve_closure = Closure::wrap(
+ Box::new(move |buffer: JsValue| {
+ buffer
+ .dyn_into::()
+ .map_or_else(
+ |_| {
+ bytes.set(None);
+ bytes_mime_type.set(None);
+ },
+ |buffer| {
+ let data = Uint8Array::new(
+ &buffer,
+ )
+ .to_vec();
+ bytes.set(Some(data));
+ bytes_mime_type
+ .set(Some(t.clone()));
+ },
+ );
+ })
+ as Box,
+ );
+ let reject_closure =
+ Closure::wrap(Box::new(move |_| {
+ bytes2.set(None);
+ bytes_mime_type2.set(None);
+ })
+ as Box);
+ let _ = blob.array_buffer().then2(
+ &resolve_closure,
+ &reject_closure,
+ );
+ resolve_closure.forget();
+ reject_closure.forget();
+ },
+ );
+ })
as Box);
- let reject_closure =
- Closure::wrap(Box::new(move |_| {
- bytes2.set(None);
- bytes_mime_type2.set(None);
- })
- as Box);
- let _ = blob
- .array_buffer()
- .then2(&resolve_closure, &reject_closure);
- resolve_closure.forget();
- reject_closure.forget();
- } else {
- bytes.set(None);
- bytes_mime_type.set(None);
- }
- })
- as Box);
- let reject_closure = Closure::wrap(Box::new(move |_| {
- bytes2.set(None);
- bytes_mime_type2.set(None);
- })
- as Box);
- let _ =
- item.get_type(&t2).then2(&resolve_closure, &reject_closure);
- resolve_closure.forget();
- reject_closure.forget();
- } else {
- bytes.set(None);
- bytes_mime_type.set(None);
+ let reject_closure = Closure::wrap(Box::new(move |_| {
+ bytes2.set(None);
+ bytes_mime_type2.set(None);
+ })
+ as Box);
+ let _ = item
+ .get_type(&t2)
+ .then2(&resolve_closure, &reject_closure);
+ resolve_closure.forget();
+ reject_closure.forget();
+ },
+ );
}
- }
- } else {
- bytes.set(None);
- bytes_mime_type.set(None);
- }
+ },
+ );
}
}) as Box);
let reject_closure = Closure::wrap(Box::new(move |_| {
@@ -352,8 +374,8 @@ pub fn use_clipboard() -> UseClipboardHandle {
text,
bytes,
bytes_mime_type,
- is_supported,
copied,
+ is_supported,
write_text,
write,
read_text,
diff --git a/crates/yew-hooks/src/hooks/use_counter.rs b/crates/yew-hooks/src/hooks/use_counter.rs
index f21ca95..353d940 100644
--- a/crates/yew-hooks/src/hooks/use_counter.rs
+++ b/crates/yew-hooks/src/hooks/use_counter.rs
@@ -53,32 +53,32 @@ pub struct UseCounterHandle {
impl UseCounterHandle {
/// Increase by `1`.
pub fn increase(&self) {
- self.inner.dispatch(CounterAction::Increase)
+ self.inner.dispatch(CounterAction::Increase);
}
/// Increase by `delta`.
pub fn increase_by(&self, delta: i32) {
- self.inner.dispatch(CounterAction::IncreaseBy(delta))
+ self.inner.dispatch(CounterAction::IncreaseBy(delta));
}
/// Decrease by `1`.
pub fn decrease(&self) {
- self.inner.dispatch(CounterAction::Decrease)
+ self.inner.dispatch(CounterAction::Decrease);
}
/// Decrease by `delta`.
pub fn decrease_by(&self, delta: i32) {
- self.inner.dispatch(CounterAction::DecreaseBy(delta))
+ self.inner.dispatch(CounterAction::DecreaseBy(delta));
}
/// Set to `value`.
pub fn set(&self, value: i32) {
- self.inner.dispatch(CounterAction::Set(value))
+ self.inner.dispatch(CounterAction::Set(value));
}
/// Reset to initial value.
pub fn reset(&self) {
- self.inner.dispatch(CounterAction::Reset)
+ self.inner.dispatch(CounterAction::Reset);
}
}
diff --git a/crates/yew-hooks/src/hooks/use_debounce.rs b/crates/yew-hooks/src/hooks/use_debounce.rs
index 366f8f3..ae27f9b 100644
--- a/crates/yew-hooks/src/hooks/use_debounce.rs
+++ b/crates/yew-hooks/src/hooks/use_debounce.rs
@@ -10,12 +10,12 @@ pub struct UseDebounceHandle {
impl UseDebounceHandle {
/// Run the debounce.
pub fn run(&self) {
- self.inner.reset()
+ self.inner.reset();
}
/// Cancel the debounce.
pub fn cancel(&self) {
- self.inner.cancel()
+ self.inner.cancel();
}
}
diff --git a/crates/yew-hooks/src/hooks/use_debounce_state.rs b/crates/yew-hooks/src/hooks/use_debounce_state.rs
index 2c83537..56581a9 100644
--- a/crates/yew-hooks/src/hooks/use_debounce_state.rs
+++ b/crates/yew-hooks/src/hooks/use_debounce_state.rs
@@ -14,7 +14,7 @@ pub struct UseDebounceStateHandle {
impl UseDebounceStateHandle {
// Set the value.
pub fn set(&self, value: T) {
- (self.set)(value)
+ (self.set)(value);
}
}
@@ -22,7 +22,7 @@ impl Deref for UseDebounceStateHandle {
type Target = T;
fn deref(&self) -> &Self::Target {
- &(*self.inner)
+ &self.inner
}
}
diff --git a/crates/yew-hooks/src/hooks/use_default.rs b/crates/yew-hooks/src/hooks/use_default.rs
index 5f68317..8f17623 100644
--- a/crates/yew-hooks/src/hooks/use_default.rs
+++ b/crates/yew-hooks/src/hooks/use_default.rs
@@ -15,7 +15,7 @@ where
{
/// Replaces the value.
pub fn set(&self, value: Option) {
- self.inner.set(value)
+ self.inner.set(value);
}
}
diff --git a/crates/yew-hooks/src/hooks/use_effect_update.rs b/crates/yew-hooks/src/hooks/use_effect_update.rs
index 7b71d9e..228c2f1 100644
--- a/crates/yew-hooks/src/hooks/use_effect_update.rs
+++ b/crates/yew-hooks/src/hooks/use_effect_update.rs
@@ -36,10 +36,10 @@ where
let first = use_is_first_mount();
use_effect(move || {
- if !first {
- Box::new(callback())
- } else {
+ if first {
Box::new(|| ()) as Box
+ } else {
+ Box::new(callback())
}
});
}
@@ -59,10 +59,10 @@ pub fn use_effect_update_with_deps(
use_effect_with_deps(
move |deps| {
- if !first {
- Box::new(callback(deps))
- } else {
+ if first {
Box::new(|| ()) as Box
+ } else {
+ Box::new(callback(deps))
}
},
deps,
diff --git a/crates/yew-hooks/src/hooks/use_event.rs b/crates/yew-hooks/src/hooks/use_event.rs
index 9457ae1..27884e2 100644
--- a/crates/yew-hooks/src/hooks/use_event.rs
+++ b/crates/yew-hooks/src/hooks/use_event.rs
@@ -1,5 +1,4 @@
use std::borrow::Cow;
-use std::ops::Deref;
use gloo::events::{EventListener, EventListenerOptions};
use gloo::utils::window;
@@ -47,7 +46,7 @@ where
let window = window();
let node = node.get();
// If we cannot get the wrapped `Node`, then we use `Window` as the default target of the event.
- let target = node.as_deref().map_or(window.deref(), |t| t);
+ let target = node.as_deref().map_or(&*window, |t| t);
// We should only set passive event listeners for `touchstart` and `touchmove`.
// See here: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Improving_scrolling_performance_with_passive_listeners
diff --git a/crates/yew-hooks/src/hooks/use_favicon.rs b/crates/yew-hooks/src/hooks/use_favicon.rs
index 5a92899..fb0fb5f 100644
--- a/crates/yew-hooks/src/hooks/use_favicon.rs
+++ b/crates/yew-hooks/src/hooks/use_favicon.rs
@@ -50,5 +50,5 @@ pub fn use_favicon(href: String) {
|| ()
},
href,
- )
+ );
}
diff --git a/crates/yew-hooks/src/hooks/use_geolocation.rs b/crates/yew-hooks/src/hooks/use_geolocation.rs
index 9ab0e2b..45e446d 100644
--- a/crates/yew-hooks/src/hooks/use_geolocation.rs
+++ b/crates/yew-hooks/src/hooks/use_geolocation.rs
@@ -133,7 +133,7 @@ pub fn use_geolocation_with_options(options: UseGeolocationOptions) -> UseGeoloc
.navigator()
.geolocation()
.unwrap_throw()
- .clear_watch(watch_id)
+ .clear_watch(watch_id);
}
});
}
diff --git a/crates/yew-hooks/src/hooks/use_hash.rs b/crates/yew-hooks/src/hooks/use_hash.rs
index 67afcee..9aa653c 100644
--- a/crates/yew-hooks/src/hooks/use_hash.rs
+++ b/crates/yew-hooks/src/hooks/use_hash.rs
@@ -22,7 +22,7 @@ impl Deref for UseHashHandle {
type Target = String;
fn deref(&self) -> &Self::Target {
- &(*self.inner)
+ &self.inner
}
}
diff --git a/crates/yew-hooks/src/hooks/use_local_storage.rs b/crates/yew-hooks/src/hooks/use_local_storage.rs
index 7dba34c..95c72e7 100644
--- a/crates/yew-hooks/src/hooks/use_local_storage.rs
+++ b/crates/yew-hooks/src/hooks/use_local_storage.rs
@@ -36,7 +36,7 @@ impl Deref for UseLocalStorageHandle {
type Target = Option;
fn deref(&self) -> &Self::Target {
- &(*self.inner)
+ &self.inner
}
}
@@ -113,7 +113,7 @@ where
use_event_with_window("storage", move |e: StorageEvent| {
if let Some(k) = e.key() {
if k == *key {
- inner.set(LocalStorage::get(&*key).unwrap_or_default())
+ inner.set(LocalStorage::get(&*key).unwrap_or_default());
}
}
});
diff --git a/crates/yew-hooks/src/hooks/use_measure.rs b/crates/yew-hooks/src/hooks/use_measure.rs
index 7fdd25b..14f1476 100644
--- a/crates/yew-hooks/src/hooks/use_measure.rs
+++ b/crates/yew-hooks/src/hooks/use_measure.rs
@@ -62,7 +62,7 @@ pub fn use_measure(node: NodeRef) -> UseMeasureState {
use_effect_with_deps(
move |node| {
let closure = Closure::wrap(Box::new(move |entries: Vec| {
- for entry in entries.iter() {
+ for entry in &entries {
let rect = entry.content_rect();
state.set(UseMeasureState {
x: rect.x(),
diff --git a/crates/yew-hooks/src/hooks/use_media.rs b/crates/yew-hooks/src/hooks/use_media.rs
index 9c7c4bb..cef7e65 100644
--- a/crates/yew-hooks/src/hooks/use_media.rs
+++ b/crates/yew-hooks/src/hooks/use_media.rs
@@ -53,32 +53,32 @@ pub struct UseMediaHandle {
impl UseMediaHandle {
/// Play the media.
pub fn play(&self) {
- (self.play)()
+ (self.play)();
}
/// Pause the media.
pub fn pause(&self) {
- (self.pause)()
+ (self.pause)();
}
/// Mute the media.
pub fn mute(&self) {
- (self.mute)()
+ (self.mute)();
}
/// Unmute the media.
pub fn unmute(&self) {
- (self.unmute)()
+ (self.unmute)();
}
/// Set volume of the media.
pub fn set_volume(&self, value: f64) {
- (self.set_volume)(value)
+ (self.set_volume)(value);
}
/// Seek the media.
pub fn seek(&self, value: f64) {
- (self.seek)(value)
+ (self.seek)(value);
}
}
@@ -459,7 +459,7 @@ fn parse_time_ranges(ranges: TimeRanges) -> Vec<(f64, f64)> {
result.push((
ranges.start(index).unwrap_throw(),
ranges.end(index).unwrap_throw(),
- ))
+ ));
}
}
result
diff --git a/crates/yew-hooks/src/hooks/use_previous.rs b/crates/yew-hooks/src/hooks/use_previous.rs
index ef8fc6c..e102c19 100644
--- a/crates/yew-hooks/src/hooks/use_previous.rs
+++ b/crates/yew-hooks/src/hooks/use_previous.rs
@@ -19,7 +19,7 @@ impl Deref for UsePreviousHandle {
type Target = T;
fn deref(&self) -> &Self::Target {
- &(*self.inner)
+ &self.inner
}
}
diff --git a/crates/yew-hooks/src/hooks/use_raf.rs b/crates/yew-hooks/src/hooks/use_raf.rs
index fe7b362..86d05ee 100644
--- a/crates/yew-hooks/src/hooks/use_raf.rs
+++ b/crates/yew-hooks/src/hooks/use_raf.rs
@@ -52,10 +52,11 @@ pub fn use_raf(millis: u32, delay: u32) -> f64 {
if *start.borrow() <= 0f64 {
*start.borrow_mut() = time;
}
- let time =
- min_by(1f64, (time - *start.borrow()) / (millis as f64), |x, y| {
- x.partial_cmp(y).unwrap()
- });
+ let time = min_by(
+ 1f64,
+ (time - *start.borrow()) / f64::from(millis),
+ |x, y| x.partial_cmp(y).unwrap(),
+ );
elapsed.set(time);
// Schedule ourself for another requestAnimationFrame callback.
diff --git a/crates/yew-hooks/src/hooks/use_raf_state.rs b/crates/yew-hooks/src/hooks/use_raf_state.rs
index 24a055d..1aa9dad 100644
--- a/crates/yew-hooks/src/hooks/use_raf_state.rs
+++ b/crates/yew-hooks/src/hooks/use_raf_state.rs
@@ -29,7 +29,7 @@ impl Deref for UseRafStateHandle {
type Target = T;
fn deref(&self) -> &Self::Target {
- &(*self.inner)
+ &self.inner
}
}
diff --git a/crates/yew-hooks/src/hooks/use_session_storage.rs b/crates/yew-hooks/src/hooks/use_session_storage.rs
index e979980..59428f3 100644
--- a/crates/yew-hooks/src/hooks/use_session_storage.rs
+++ b/crates/yew-hooks/src/hooks/use_session_storage.rs
@@ -33,7 +33,7 @@ impl Deref for UseSessionStorageHandle {
type Target = Option;
fn deref(&self) -> &Self::Target {
- &(*self.inner)
+ &self.inner
}
}
diff --git a/crates/yew-hooks/src/hooks/use_size.rs b/crates/yew-hooks/src/hooks/use_size.rs
index 9542b53..dfc793f 100644
--- a/crates/yew-hooks/src/hooks/use_size.rs
+++ b/crates/yew-hooks/src/hooks/use_size.rs
@@ -38,7 +38,7 @@ pub fn use_size(node: NodeRef) -> (u32, u32) {
use_effect_with_deps(
move |node| {
let closure = Closure::wrap(Box::new(move |entries: Vec| {
- for entry in entries.iter() {
+ for entry in &entries {
let element = entry.target();
state.set((
element.client_width() as u32,
diff --git a/crates/yew-hooks/src/hooks/use_state_ptr_eq.rs b/crates/yew-hooks/src/hooks/use_state_ptr_eq.rs
index b423e86..1a2f973 100644
--- a/crates/yew-hooks/src/hooks/use_state_ptr_eq.rs
+++ b/crates/yew-hooks/src/hooks/use_state_ptr_eq.rs
@@ -31,7 +31,7 @@ pub struct UseStatePtrEqHandle {
impl UseStatePtrEqHandle {
/// Replaces the value
pub fn set(&self, value: T) {
- self.inner.dispatch(value)
+ self.inner.dispatch(value);
}
}
diff --git a/crates/yew-hooks/src/hooks/use_swipe.rs b/crates/yew-hooks/src/hooks/use_swipe.rs
index 5630f11..67f4485 100644
--- a/crates/yew-hooks/src/hooks/use_swipe.rs
+++ b/crates/yew-hooks/src/hooks/use_swipe.rs
@@ -6,7 +6,7 @@ use yew::prelude::*;
use super::{use_event, use_mut_latest};
/// Swipe direction.
-#[derive(PartialEq, Clone, Debug)]
+#[derive(PartialEq, Eq, Clone, Debug)]
pub enum UseSwipeDirection {
Up,
Right,
@@ -163,13 +163,13 @@ pub fn use_swipe_with_options(node: NodeRef, options: UseSwipeOptions) -> UseSwi
let diff_x = {
let coords_start = coords_start.clone();
let coords_end = coords_end.clone();
- Rc::new(move || ((*coords_start).0 - (*coords_end).0) as i32)
+ Rc::new(move || (coords_start.0 - coords_end.0) as i32)
};
let diff_y = {
let coords_start = coords_start.clone();
let coords_end = coords_end.clone();
- Rc::new(move || ((*coords_start).1 - (*coords_end).1) as i32)
+ Rc::new(move || (coords_start.1 - coords_end.1) as i32)
};
let ontouchend = {
diff --git a/crates/yew-hooks/src/hooks/use_throttle.rs b/crates/yew-hooks/src/hooks/use_throttle.rs
index ed0699f..5d87947 100644
--- a/crates/yew-hooks/src/hooks/use_throttle.rs
+++ b/crates/yew-hooks/src/hooks/use_throttle.rs
@@ -13,12 +13,12 @@ pub struct UseThrottleHandle {
impl UseThrottleHandle {
/// Run the throttle.
pub fn run(&self) {
- (self.run)()
+ (self.run)();
}
/// Cancel the throttle.
pub fn cancel(&self) {
- (self.cancel)()
+ (self.cancel)();
}
}
diff --git a/crates/yew-hooks/src/hooks/use_throttle_state.rs b/crates/yew-hooks/src/hooks/use_throttle_state.rs
index 0ed7d2b..88e9c13 100644
--- a/crates/yew-hooks/src/hooks/use_throttle_state.rs
+++ b/crates/yew-hooks/src/hooks/use_throttle_state.rs
@@ -14,7 +14,7 @@ pub struct UseThrottleStateHandle {
impl UseThrottleStateHandle {
// Set the value.
pub fn set(&self, value: T) {
- (self.set)(value)
+ (self.set)(value);
}
}
@@ -22,7 +22,7 @@ impl Deref for UseThrottleStateHandle {
type Target = T;
fn deref(&self) -> &Self::Target {
- &(*self.inner)
+ &self.inner
}
}
diff --git a/crates/yew-hooks/src/hooks/use_timeout.rs b/crates/yew-hooks/src/hooks/use_timeout.rs
index fb85bc0..384356c 100644
--- a/crates/yew-hooks/src/hooks/use_timeout.rs
+++ b/crates/yew-hooks/src/hooks/use_timeout.rs
@@ -14,12 +14,12 @@ pub struct UseTimeoutHandle {
impl UseTimeoutHandle {
/// Reset the timeout.
pub fn reset(&self) {
- (self.reset)()
+ (self.reset)();
}
/// Cancel the timeout.
pub fn cancel(&self) {
- (self.cancel)()
+ (self.cancel)();
}
}
diff --git a/crates/yew-hooks/src/hooks/use_title.rs b/crates/yew-hooks/src/hooks/use_title.rs
index 00c4090..4c8bc07 100644
--- a/crates/yew-hooks/src/hooks/use_title.rs
+++ b/crates/yew-hooks/src/hooks/use_title.rs
@@ -30,6 +30,6 @@ pub fn use_title(title: String) {
}
use_unmount(move || {
- gloo::utils::document().set_title(&*pre_title);
+ gloo::utils::document().set_title(&pre_title);
});
}
diff --git a/crates/yew-hooks/src/hooks/use_toggle.rs b/crates/yew-hooks/src/hooks/use_toggle.rs
index 8603ad7..32c7650 100644
--- a/crates/yew-hooks/src/hooks/use_toggle.rs
+++ b/crates/yew-hooks/src/hooks/use_toggle.rs
@@ -36,9 +36,8 @@ where
self.left.clone()
}
}
- ToggleAction::Reset => self.left.clone(),
ToggleAction::Set(value) => Rc::new(value),
- ToggleAction::SetLeft => self.left.clone(),
+ ToggleAction::Reset | ToggleAction::SetLeft => self.left.clone(),
ToggleAction::SetRight => self.right.clone(),
};
@@ -74,27 +73,27 @@ where
{
/// Toggle the value.
pub fn toggle(&self) {
- self.inner.dispatch(ToggleAction::Toggle)
+ self.inner.dispatch(ToggleAction::Toggle);
}
/// Set to a value.
pub fn set(&self, value: T) {
- self.inner.dispatch(ToggleAction::Set(value))
+ self.inner.dispatch(ToggleAction::Set(value));
}
/// Set to the left default value.
pub fn set_left(&self) {
- self.inner.dispatch(ToggleAction::SetLeft)
+ self.inner.dispatch(ToggleAction::SetLeft);
}
/// Set to the right other value.
pub fn set_right(&self) {
- self.inner.dispatch(ToggleAction::SetRight)
+ self.inner.dispatch(ToggleAction::SetRight);
}
/// Reset to the default value.
pub fn reset(&self) {
- self.inner.dispatch(ToggleAction::Reset)
+ self.inner.dispatch(ToggleAction::Reset);
}
}
@@ -129,10 +128,7 @@ where
}
}
-impl fmt::Debug for UseToggleHandle
-where
- T: PartialEq,
-{
+impl fmt::Debug for UseToggleHandle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("UseToggleHandle")
.field("value", &format!("{:?}", self.inner.value))
diff --git a/crates/yew-hooks/src/hooks/use_websocket.rs b/crates/yew-hooks/src/hooks/use_websocket.rs
index b7e1a59..30020fd 100644
--- a/crates/yew-hooks/src/hooks/use_websocket.rs
+++ b/crates/yew-hooks/src/hooks/use_websocket.rs
@@ -11,7 +11,7 @@ use super::{use_mut_latest, use_state_ptr_eq, use_unmount, UseStatePtrEqHandle};
pub use web_sys::CloseEvent;
/// The current state of the `WebSocket` connection.
-#[derive(Debug, PartialEq, Clone)]
+#[derive(Debug, PartialEq, Eq, Clone)]
pub enum UseWebSocketReadyState {
Connecting,
Open,
@@ -63,22 +63,22 @@ pub struct UseWebSocketHandle {
impl UseWebSocketHandle {
/// Connect `WebSocket` manually. If already connected, close the current one and reconnect.
pub fn open(&self) {
- (self.open)()
+ (self.open)();
}
/// Disconnect `WebSocket` manually.
pub fn close(&self) {
- (self.close)()
+ (self.close)();
}
/// Send text message to `WebSocket`.
pub fn send(&self, data: String) {
- (self.send)(data)
+ (self.send)(data);
}
/// Send binary message to `WebSocket`.
pub fn send_bytes(&self, data: Vec) {
- (self.send_bytes)(data)
+ (self.send_bytes)(data);
}
}
@@ -294,22 +294,24 @@ pub fn use_websocket_with_options(url: String, options: UseWebSocketOptions) ->
*reconnect_timer_ref.borrow_mut() = None;
{
- let web_socket: &mut Option = &mut *ws.borrow_mut();
+ let web_socket: &mut Option = &mut ws.borrow_mut();
if let Some(web_socket) = web_socket {
let _ = web_socket.close();
}
}
let web_socket = {
- if let Some(protocols) = &protocols {
- let array = protocols
- .iter()
- .map(|p| JsValue::from(p.clone()))
- .collect::();
- WebSocket::new_with_str_sequence(&url, &JsValue::from(&array)).unwrap_throw()
- } else {
- WebSocket::new(&url).unwrap_throw()
- }
+ protocols.as_ref().map_or_else(
+ || WebSocket::new(&url).unwrap_throw(),
+ |protocols| {
+ let array = protocols
+ .iter()
+ .map(|p| JsValue::from(p.clone()))
+ .collect::();
+ WebSocket::new_with_str_sequence(&url, &JsValue::from(&array))
+ .unwrap_throw()
+ },
+ )
};
web_socket.set_binary_type(BinaryType::Arraybuffer);
ready_state.set(UseWebSocketReadyState::Connecting);
@@ -346,28 +348,36 @@ pub fn use_websocket_with_options(url: String, options: UseWebSocketOptions) ->
return;
}
- if let Ok(array_buffer) = e.data().dyn_into::() {
- let array = js_sys::Uint8Array::new(&array_buffer);
- let array = array.to_vec();
- let onmessage_bytes_ref = onmessage_bytes_ref.current();
- let onmessage_bytes = &mut *onmessage_bytes_ref.borrow_mut();
- if let Some(onmessage_bytes) = onmessage_bytes {
- let array = array.clone();
- onmessage_bytes(array);
- }
- message_bytes.set(Some(array));
- } else if let Ok(txt) = e.data().dyn_into::() {
- let txt = String::from(&txt);
- let onmessage_ref = onmessage_ref.current();
- let onmessage = &mut *onmessage_ref.borrow_mut();
- if let Some(onmessage) = onmessage {
- let txt = txt.clone();
- onmessage(txt);
- }
- message.set(Some(txt));
- } else {
- unreachable!("message event, received Unknown: {:?}", e.data());
- }
+ e.data().dyn_into::().map_or_else(
+ |_| {
+ e.data().dyn_into::().map_or_else(
+ |_| {
+ unreachable!("message event, received Unknown: {:?}", e.data());
+ },
+ |txt| {
+ let txt = String::from(&txt);
+ let onmessage_ref = onmessage_ref.current();
+ let onmessage = &mut *onmessage_ref.borrow_mut();
+ if let Some(onmessage) = onmessage {
+ let txt = txt.clone();
+ onmessage(txt);
+ }
+ message.set(Some(txt));
+ },
+ );
+ },
+ |array_buffer| {
+ let array = js_sys::Uint8Array::new(&array_buffer);
+ let array = array.to_vec();
+ let onmessage_bytes_ref = onmessage_bytes_ref.current();
+ let onmessage_bytes = &mut *onmessage_bytes_ref.borrow_mut();
+ if let Some(onmessage_bytes) = onmessage_bytes {
+ let array = array.clone();
+ onmessage_bytes(array);
+ }
+ message_bytes.set(Some(array));
+ },
+ );
})
as Box);
web_socket.set_onmessage(Some(onmessage_closure.as_ref().unchecked_ref()));
@@ -432,7 +442,7 @@ pub fn use_websocket_with_options(url: String, options: UseWebSocketOptions) ->
let ws = ws.clone();
Rc::new(move |data: String| {
if *ready_state == UseWebSocketReadyState::Open {
- let web_socket: &mut Option = &mut *ws.borrow_mut();
+ let web_socket: &mut Option = &mut ws.borrow_mut();
if let Some(web_socket) = web_socket {
let _ = web_socket.send_with_str(&data);
}
@@ -445,7 +455,7 @@ pub fn use_websocket_with_options(url: String, options: UseWebSocketOptions) ->
let ws = ws.clone();
Rc::new(move |data: Vec| {
if *ready_state == UseWebSocketReadyState::Open {
- let web_socket: &mut Option = &mut *ws.borrow_mut();
+ let web_socket: &mut Option = &mut ws.borrow_mut();
if let Some(web_socket) = web_socket {
let _ = web_socket.send_with_u8_array(&data);
}
@@ -469,7 +479,7 @@ pub fn use_websocket_with_options(url: String, options: UseWebSocketOptions) ->
*reconnect_timer_ref.borrow_mut() = None;
*reconnect_times_ref.borrow_mut() = reconnect_limit;
- let web_socket: &mut Option = &mut *ws.borrow_mut();
+ let web_socket: &mut Option = &mut ws.borrow_mut();
if let Some(web_socket) = web_socket {
let _ = web_socket.close();
}
diff --git a/examples/yew-app/src/routes/hooks/use_async.rs b/examples/yew-app/src/routes/hooks/use_async.rs
index e5b34f9..5af972c 100644
--- a/examples/yew-app/src/routes/hooks/use_async.rs
+++ b/examples/yew-app/src/routes/hooks/use_async.rs
@@ -55,31 +55,23 @@ pub fn async_demo() -> Html {
}
{
- if let Some(repo) = &state.data {
- html! {
- <>
- { "Repo name: " }{ &repo.name }
- { "Repo full name: " }{ &repo.full_name }
- { "Repo description: " }{ &repo.description }
+ state.data.as_ref().map_or_else(|| html! {}, |repo| html! {
+ <>
+ { "Repo name: " }{ &repo.name }
+ { "Repo full name: " }{ &repo.full_name }
+ { "Repo description: " }{ &repo.description }
- { "Owner name: " }{ &repo.owner.login }
- { "Owner avatar: " }
- >
- }
- } else {
- html! {}
- }
+ { "Owner name: " }{ &repo.owner.login }
+ { "Owner avatar: " }
+ >
+ })
}
{
- if let Some(error) = &state.error {
- match error {
- Error::DeserializeError => html! { "DeserializeError" },
- Error::RequestError => html! { "RequestError" },
- }
- } else {
- html! {}
- }
+ state.error.as_ref().map_or_else(|| html! {}, |error| match error {
+ Error::DeserializeError => html! { "DeserializeError" },
+ Error::RequestError => html! { "RequestError" },
+ })
}
@@ -99,11 +91,7 @@ where
{
let response = reqwest::get(url).await;
if let Ok(data) = response {
- if let Ok(repo) = data.json::().await {
- Ok(repo)
- } else {
- Err(Error::DeserializeError)
- }
+ (data.json::().await).map_or(Err(Error::DeserializeError), |repo| Ok(repo))
} else {
Err(Error::RequestError)
}
diff --git a/examples/yew-app/src/routes/hooks/use_clipboard.rs b/examples/yew-app/src/routes/hooks/use_clipboard.rs
index 0d5cc8a..f584f1c 100644
--- a/examples/yew-app/src/routes/hooks/use_clipboard.rs
+++ b/examples/yew-app/src/routes/hooks/use_clipboard.rs
@@ -12,11 +12,7 @@ pub fn update() -> Html {
)
.await
{
- if let Ok(bytes) = response.bytes().await {
- Ok(bytes.to_vec())
- } else {
- Err("Bytes error")
- }
+ (response.bytes().await).map_or(Err("Bytes error"), |bytes| Ok(bytes.to_vec()))
} else {
Err("Response err")
}
diff --git a/examples/yew-app/src/routes/hooks/use_debounce.rs b/examples/yew-app/src/routes/hooks/use_debounce.rs
index c4a4cf1..cb8e1a1 100644
--- a/examples/yew-app/src/routes/hooks/use_debounce.rs
+++ b/examples/yew-app/src/routes/hooks/use_debounce.rs
@@ -6,8 +6,8 @@ use yew_hooks::prelude::*;
#[function_component(UseDebounce)]
pub fn debounce() -> Html {
let status = use_state(|| "Typing stopped".to_string());
- let value = use_state(|| "".to_string());
- let debounced_value = use_state(|| "".to_string());
+ let value = use_state(String::new);
+ let debounced_value = use_state(String::new);
let debounce = {
let status = status.clone();
diff --git a/examples/yew-app/src/routes/hooks/use_debounce_effect.rs b/examples/yew-app/src/routes/hooks/use_debounce_effect.rs
index aacc1b2..7091835 100644
--- a/examples/yew-app/src/routes/hooks/use_debounce_effect.rs
+++ b/examples/yew-app/src/routes/hooks/use_debounce_effect.rs
@@ -6,8 +6,8 @@ use yew_hooks::prelude::*;
#[function_component(UseDebounceEffect)]
pub fn debounce_effect() -> Html {
let status = use_state(|| "Typing stopped".to_string());
- let value = use_state(|| "".to_string());
- let debounced_value = use_state(|| "".to_string());
+ let value = use_state(String::new);
+ let debounced_value = use_state(String::new);
{
let status = status.clone();
diff --git a/examples/yew-app/src/routes/hooks/use_debounce_state.rs b/examples/yew-app/src/routes/hooks/use_debounce_state.rs
index a61bb01..ceae325 100644
--- a/examples/yew-app/src/routes/hooks/use_debounce_state.rs
+++ b/examples/yew-app/src/routes/hooks/use_debounce_state.rs
@@ -5,8 +5,8 @@ use yew_hooks::prelude::*;
/// `use_debounce_state` demo
#[function_component(UseDebounceState)]
pub fn debounce_state() -> Html {
- let value = use_state(|| "".to_string());
- let debounced_value = use_debounce_state(|| "".to_string(), 2000);
+ let value = use_state(String::new);
+ let debounced_value = use_debounce_state(String::new, 2000);
let oninput = {
let value = value.clone();
diff --git a/examples/yew-app/src/routes/hooks/use_drag.rs b/examples/yew-app/src/routes/hooks/use_drag.rs
index 0d1506a..27ebf0b 100644
--- a/examples/yew-app/src/routes/hooks/use_drag.rs
+++ b/examples/yew-app/src/routes/hooks/use_drag.rs
@@ -53,11 +53,10 @@ pub fn drag() -> Html {
} else {
"background-color: #61dafb; border: 3px dashed white; margin-top: 20px;" }}>
{ " Text: " }
- {if let Some(text) = &*state.text {
- html! {{ text }
}
- } else {
- html! {}
- }}
+ {
+ (*state.text).as_ref().map_or_else(|| html! {},
+ |text| html! {{ text }
})
+ }
{ "Try to drop something to this area" }
diff --git a/examples/yew-app/src/routes/hooks/use_drop.rs b/examples/yew-app/src/routes/hooks/use_drop.rs
index 08ee057..e020350 100644
--- a/examples/yew-app/src/routes/hooks/use_drop.rs
+++ b/examples/yew-app/src/routes/hooks/use_drop.rs
@@ -27,25 +27,28 @@ pub fn drop() -> Html {
} else {
"background-color: #61dafb; border: 3px dashed white;" }}>
{ " Files: " }
- {if let Some(files) = &*state.files {
- html! {for files.iter().map(|file| {
- html! { { file.name() }
}
- })}
- } else {
- html! {}
- }}
+ {
+ (*state.files).as_ref().map_or_else(
+ || html! {},
+ |files| {
+ html! {for files.iter().map(|file| {
+ html! { { file.name() }
}
+ })}
+ },
+ )
+ }
{ " Text: " }
- {if let Some(text) = &*state.text {
- html! {{ text }
}
- } else {
- html! {}
- }}
+ {
+ (*state.text)
+ .as_ref()
+ .map_or_else(|| html! {}, |text| html! {{ text }
})
+ }
{ " Uri: " }
- {if let Some(uri) = &*state.uri {
- html! {{ uri }
}
- } else {
- html! {}
- }}
+ {
+ (*state.uri)
+ .as_ref()
+ .map_or_else(|| html! {}, |uri| html! {{ uri }
})
+ }
{ "Try to drag & drop or copy & paste something here, e.g. files, links or text" }
diff --git a/examples/yew-app/src/routes/hooks/use_local_storage.rs b/examples/yew-app/src/routes/hooks/use_local_storage.rs
index 05a67b1..0a7dbfd 100644
--- a/examples/yew-app/src/routes/hooks/use_local_storage.rs
+++ b/examples/yew-app/src/routes/hooks/use_local_storage.rs
@@ -19,7 +19,7 @@ pub fn local_storage() -> Html {
storage.set(User {
name: String::from("Jet Li"),
token: String::from("jwt_token"),
- })
+ });
})
};
let ondelete = {
@@ -32,8 +32,11 @@ pub fn local_storage() -> Html {
diff --git a/examples/yew-app/src/routes/hooks/use_session_storage.rs b/examples/yew-app/src/routes/hooks/use_session_storage.rs
index c77b5f0..14e3b04 100644
--- a/examples/yew-app/src/routes/hooks/use_session_storage.rs
+++ b/examples/yew-app/src/routes/hooks/use_session_storage.rs
@@ -24,11 +24,9 @@ pub fn session_storage() -> Html {
{ "Current value: " }
{
- if let Some(value) = &*storage {
- html! { value }
- } else {
- html! {}
- }
+ (*storage)
+ .as_ref()
+ .map_or_else(|| html! {}, |value| html! { value })
}
diff --git a/examples/yew-app/src/routes/hooks/use_state_ptr_eq.rs b/examples/yew-app/src/routes/hooks/use_state_ptr_eq.rs
index 30560e7..6329106 100644
--- a/examples/yew-app/src/routes/hooks/use_state_ptr_eq.rs
+++ b/examples/yew-app/src/routes/hooks/use_state_ptr_eq.rs
@@ -4,7 +4,7 @@ use yew_hooks::prelude::*;
/// `use_state_ptr_eq` demo
#[function_component(UseStatePtrEq)]
pub fn state_ptr_eq() -> Html {
- let state = use_state_ptr_eq(|| "".to_string());
+ let state = use_state_ptr_eq(String::new);
let history = use_list(vec![]);
let onclick = {
@@ -18,7 +18,7 @@ pub fn state_ptr_eq() -> Html {
// This effect will not run if use `use_state` or `use_state_eq`.
use_effect_with_deps(
move |message| {
- history.push((&**message).clone());
+ history.push((**message).clone());
|| ()
},
diff --git a/examples/yew-app/src/routes/hooks/use_throttle_effect.rs b/examples/yew-app/src/routes/hooks/use_throttle_effect.rs
index 577e498..149029d 100644
--- a/examples/yew-app/src/routes/hooks/use_throttle_effect.rs
+++ b/examples/yew-app/src/routes/hooks/use_throttle_effect.rs
@@ -14,7 +14,7 @@ pub fn throttle_effect() -> Html {
state.set(*state + 1);
},
2000,
- )
+ );
};
let onclick = { Callback::from(move |_| update()) };
diff --git a/examples/yew-app/src/routes/hooks/use_websocket.rs b/examples/yew-app/src/routes/hooks/use_websocket.rs
index ae7f780..d1beeec 100644
--- a/examples/yew-app/src/routes/hooks/use_websocket.rs
+++ b/examples/yew-app/src/routes/hooks/use_websocket.rs
@@ -37,7 +37,7 @@ pub fn web_socket() -> Html {
let ws = ws.clone();
let history = history.clone();
Callback::from(move |_| {
- let message = "Hello, world!\r\n".as_bytes().to_vec();
+ let message = b"Hello, world!\r\n".to_vec();
ws.send_bytes(message.clone());
history.push(format!("ws1 [send]: bytes {:?}", message));
})
diff --git a/examples/yew-app/src/routes/mod.rs b/examples/yew-app/src/routes/mod.rs
index 5fdd4dd..bbe9960 100644
--- a/examples/yew-app/src/routes/mod.rs
+++ b/examples/yew-app/src/routes/mod.rs
@@ -10,7 +10,7 @@ use home::Home;
use hooks::*;
/// App routes
-#[derive(Routable, Debug, Clone, PartialEq)]
+#[derive(Routable, Debug, Clone, PartialEq, Eq)]
pub enum AppRoute {
#[at("/about")]
About,