Skip to content

Commit

Permalink
Clippy fixes (#21)
Browse files Browse the repository at this point in the history
* Clippy fixes

* additional fixes

* Clippy pedantic

* Go back to wildcard use
  • Loading branch information
aknarts authored Nov 14, 2022
1 parent e4860a2 commit 77aa668
Show file tree
Hide file tree
Showing 41 changed files with 275 additions and 267 deletions.
14 changes: 7 additions & 7 deletions crates/yew-hooks/src/hooks/use_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, E> {
pub loading: bool,
pub data: Option<T>,
Expand All @@ -36,7 +36,7 @@ pub struct UseAsyncHandle<T, E> {
impl<T, E> UseAsyncHandle<T, E> {
/// Start to resolve the async future to a final value.
pub fn run(&self) {
(self.run)()
(self.run)();
}

/// Update `data` directly.
Expand All @@ -53,7 +53,7 @@ impl<T, E> Deref for UseAsyncHandle<T, E> {
type Target = UseAsyncState<T, E>;

fn deref(&self) -> &Self::Target {
&(*self.inner)
&self.inner
}
}

Expand Down Expand Up @@ -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.
Expand All @@ -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),
}),
}
Expand Down
2 changes: 1 addition & 1 deletion crates/yew-hooks/src/hooks/use_before_unload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
});
}
176 changes: 99 additions & 77 deletions crates/yew-hooks/src/hooks/use_clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>, mime_type: Option<String>) {
(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);
}
}

Expand Down Expand Up @@ -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<dyn FnMut(JsValue)>);
let reject_closure = Closure::wrap(Box::new(move |_| {
Expand Down Expand Up @@ -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<dyn FnMut(JsValue)>);
let reject_closure = Closure::wrap(Box::new(move |_| {
text2.set(None);
Expand All @@ -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::<ClipboardItem>() {
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::<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| {
if let Ok(buffer) =
buffer.dyn_into::<ArrayBuffer>()
{
let data =
Uint8Array::new(&buffer).to_vec();
bytes.set(Some(data));
bytes_mime_type.set(Some(t.clone()));
} else {
item.dyn_into::<ClipboardItem>().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::<Blob>().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::<ArrayBuffer>()
.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<dyn FnMut(JsValue)>,
);
let reject_closure =
Closure::wrap(Box::new(move |_| {
bytes2.set(None);
bytes_mime_type2.set(None);
})
as Box<dyn FnMut(JsValue)>);
let _ = blob.array_buffer().then2(
&resolve_closure,
&reject_closure,
);
resolve_closure.forget();
reject_closure.forget();
},
);
})
as Box<dyn FnMut(JsValue)>);
let reject_closure =
Closure::wrap(Box::new(move |_| {
bytes2.set(None);
bytes_mime_type2.set(None);
})
as Box<dyn FnMut(JsValue)>);
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<dyn FnMut(JsValue)>);
let reject_closure = Closure::wrap(Box::new(move |_| {
bytes2.set(None);
bytes_mime_type2.set(None);
})
as Box<dyn FnMut(JsValue)>);
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<dyn FnMut(JsValue)>);
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<dyn FnMut(JsValue)>);
let reject_closure = Closure::wrap(Box::new(move |_| {
Expand All @@ -352,8 +374,8 @@ pub fn use_clipboard() -> UseClipboardHandle {
text,
bytes,
bytes_mime_type,
is_supported,
copied,
is_supported,
write_text,
write,
read_text,
Expand Down
12 changes: 6 additions & 6 deletions crates/yew-hooks/src/hooks/use_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/yew-hooks/src/hooks/use_debounce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/yew-hooks/src/hooks/use_debounce_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ pub struct UseDebounceStateHandle<T> {
impl<T> UseDebounceStateHandle<T> {
// Set the value.
pub fn set(&self, value: T) {
(self.set)(value)
(self.set)(value);
}
}

impl<T> Deref for UseDebounceStateHandle<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&(*self.inner)
&self.inner
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/yew-hooks/src/hooks/use_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ where
{
/// Replaces the value.
pub fn set(&self, value: Option<T>) {
self.inner.set(value)
self.inner.set(value);
}
}

Expand Down
12 changes: 6 additions & 6 deletions crates/yew-hooks/src/hooks/use_effect_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn FnOnce()>
} else {
Box::new(callback())
}
});
}
Expand All @@ -59,10 +59,10 @@ pub fn use_effect_update_with_deps<Callback, Destructor, Dependents>(

use_effect_with_deps(
move |deps| {
if !first {
Box::new(callback(deps))
} else {
if first {
Box::new(|| ()) as Box<dyn FnOnce()>
} else {
Box::new(callback(deps))
}
},
deps,
Expand Down
Loading

0 comments on commit 77aa668

Please sign in to comment.