Skip to content

Commit

Permalink
Maintenance: fix all the new clippy warnings (#666)
Browse files Browse the repository at this point in the history
* Relax trait bounds for map_{keyed, index}

* Remove useless import

* Fix clippy warnings
  • Loading branch information
lukechu10 authored Mar 22, 2024
1 parent cddfe95 commit 7140c12
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/sycamore-core/src/hydrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use std::cell::RefCell;

thread_local! {
static HYDRATION_CONTEXT: RefCell<Option<HydrationRegistry>> = RefCell::new(None);
static HYDRATION_CONTEXT: RefCell<Option<HydrationRegistry>> = const { RefCell::new(None) };
}

/// Run the closure inside a hydration context. If already inside a hydration context, creates a
Expand Down
26 changes: 14 additions & 12 deletions packages/sycamore-reactive/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ use crate::*;
/// * `key_fn` - A closure that returns an _unique_ key to each entry.
///
/// _Credits: Based on TypeScript implementation in <https://github.com/solidjs/solid>_
pub fn map_keyed<T, K, U: 'static>(
list: impl Accessor<Vec<T>> + Clone + 'static,
map_fn: impl Fn(T) -> U + 'static,
pub fn map_keyed<T, K, U>(
list: impl Accessor<Vec<T>> + 'static,
mut map_fn: impl FnMut(T) -> U + 'static,
key_fn: impl Fn(&T) -> K + 'static,
) -> ReadSignal<Vec<U>>
where
Expand Down Expand Up @@ -53,7 +53,7 @@ where
disposers.reserve(new_items.len());

for new_item in new_items.iter().cloned() {
let map_fn = &map_fn;
let map_fn = &mut map_fn;
let mapped = &mut mapped;
let new_disposer = create_child_scope(move || mapped.push(map_fn(new_item)));
disposers.push(Some(new_disposer));
Expand Down Expand Up @@ -97,7 +97,7 @@ where
"end and new_end are the last indexes where items[end - 1] != new_items[new_end - 1]"
);

// 0) Prepare a map of indices in newItems. Scan backwards so we encounter them in
// 0) Prepare a map of indices in new_items. Scan backwards so we encounter them in
// natural order.
let mut new_indices = HashMap::with_capacity(new_end - start);

Expand All @@ -106,20 +106,22 @@ where
let mut new_indices_next = vec![None; new_end - start];
for j in (start..new_end).rev() {
let item = &new_items[j];
let i = new_indices.get(&key_fn(item));
let key = key_fn(item);
let i = new_indices.get(&key);
new_indices_next[j - start] = i.copied();
new_indices.insert(key_fn(item), j);
new_indices.insert(key, j);
}

// 1) Step through old items and see if they can be found in new set; if so, mark
// them as moved.
for i in start..end {
let item = &items[i];
if let Some(j) = new_indices.get(&key_fn(item)).copied() {
let key = key_fn(item);
if let Some(j) = new_indices.get(&key).copied() {
// Moved. j is index of item in new_items.
mapped_tmp[j] = Some(mapped[i].clone());
disposers_tmp[j] = disposers[i].take();
new_indices_next[j - start].and_then(|j| new_indices.insert(key_fn(item), j));
new_indices_next[j - start].and_then(|j| new_indices.insert(key, j));
} else {
// Create new.
disposers[i].take().unwrap().dispose();
Expand Down Expand Up @@ -184,9 +186,9 @@ where
/// * `list` - The list to be mapped. The list must be a [`ReadSignal`] (obtained from a [`Signal`])
/// and therefore reactive.
/// * `map_fn` - A closure that maps from the input type to the output type.
pub fn map_indexed<T, U: 'static>(
list: impl Accessor<Vec<T>> + Clone + 'static,
map_fn: impl Fn(T) -> U + 'static,
pub fn map_indexed<T, U>(
list: impl Accessor<Vec<T>> + 'static,
mut map_fn: impl FnMut(T) -> U + 'static,
) -> ReadSignal<Vec<U>>
where
T: PartialEq + Clone + 'static,
Expand Down
3 changes: 2 additions & 1 deletion packages/sycamore-reactive/src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(crate) struct Root {

thread_local! {
/// The current reactive root.
static GLOBAL_ROOT: Cell<Option<&'static Root>> = Cell::new(None);
static GLOBAL_ROOT: Cell<Option<&'static Root>> = const { Cell::new(None) };
}

impl Root {
Expand Down Expand Up @@ -346,6 +346,7 @@ pub fn create_root(f: impl FnOnce()) -> RootHandle {
{
/// An unsafe wrapper around a raw pointer which we promise to never touch, effectively
/// making it thread-safe.
#[allow(dead_code)]
struct UnsafeSendPtr<T>(*const T);
/// We never ever touch the pointer inside so surely this is safe!
unsafe impl<T> Send for UnsafeSendPtr<T> {}
Expand Down
3 changes: 1 addition & 2 deletions packages/sycamore-router/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::rc::Rc;
use sycamore::prelude::*;
use sycamore::web::html::ev;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::{Element, HtmlAnchorElement, HtmlBaseElement, KeyboardEvent};

use crate::Route;
Expand All @@ -25,7 +24,7 @@ pub trait Integration {
}

thread_local! {
static PATHNAME: Cell<Option<Signal<String>>> = Cell::new(None);
static PATHNAME: Cell<Option<Signal<String>>> = const { Cell::new(None) };
}

/// A router integration that uses the
Expand Down
10 changes: 6 additions & 4 deletions packages/sycamore-web/src/dom_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use sycamore_core::generic_node::{
use sycamore_core::render::insert;
use sycamore_core::view::View;
use sycamore_reactive::*;
use wasm_bindgen::intern;
use wasm_bindgen::prelude::*;
use wasm_bindgen::{intern, JsCast};
use web_sys::{Comment, Element, Node, Text};

use crate::dom_node_template::{
Expand All @@ -23,13 +23,15 @@ use crate::{document, Html};

#[wasm_bindgen]
extern "C" {
/// Extend [`Node`] with an id field. This is used to make `Node` hashable.
#[wasm_bindgen(extends = Node)]
pub(super) type NodeWithId;
#[wasm_bindgen(method, getter, js_name = "$$$nodeId")]
pub fn node_id(this: &NodeWithId) -> Option<usize>;
pub(crate) fn node_id(this: &NodeWithId) -> Option<usize>;
#[wasm_bindgen(method, setter, js_name = "$$$nodeId")]
pub fn set_node_id(this: &NodeWithId, id: usize);
pub(crate) fn set_node_id(this: &NodeWithId, id: usize);

/// Extend [`Element`] with a failable `className` setter.
#[wasm_bindgen(extends = Element)]
type ElementTrySetClassName;
#[wasm_bindgen(method, catch, setter, js_name = "className")]
Expand All @@ -42,7 +44,7 @@ pub(super) struct NodeId(pub usize);

impl NodeId {
pub fn new_with_node(node: &Node) -> Self {
thread_local!(static NODE_ID_COUNTER: Cell<usize> = Cell::new(1)); // 0 is reserved for default value.
thread_local!(static NODE_ID_COUNTER: Cell<usize> = const { Cell::new(1) }); // 0 is reserved for default value.

let id = NODE_ID_COUNTER.with(|x| {
let tmp = x.get();
Expand Down
1 change: 0 additions & 1 deletion packages/sycamore-web/src/hydrate_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use sycamore_core::render::insert;
use sycamore_core::view::View;
use sycamore_reactive::*;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::Node;

use crate::dom_node::{DomNode, NodeId};
Expand Down
1 change: 0 additions & 1 deletion packages/sycamore-web/src/ssr_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::HashSet;
use std::hash::{Hash, Hasher};
use std::iter::FromIterator;
use std::rc::{Rc, Weak};

use indexmap::map::IndexMap;
Expand Down
1 change: 0 additions & 1 deletion packages/sycamore/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//! HTML.
use std::borrow::Cow;
use std::iter::FromIterator;
use std::rc::Rc;

use sycamore_core::event::{EventDescriptor, EventHandler};
Expand Down
1 change: 0 additions & 1 deletion packages/sycamore/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use sycamore_core::event::{EventDescriptor, EventHandler};
pub use sycamore_core::render;
use wasm_bindgen::JsValue;

use crate::generic_node::GenericNode;
use crate::prelude::*;
use crate::rt::Event;
use crate::web::html::ev;
Expand Down
1 change: 0 additions & 1 deletion packages/sycamore/src/web/portal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::any::Any;

use wasm_bindgen::prelude::*;

use crate::component::Children;
use crate::prelude::*;

/// Props for [`Portal`].
Expand Down

0 comments on commit 7140c12

Please sign in to comment.