diff --git a/packages/sycamore-core/src/hydrate.rs b/packages/sycamore-core/src/hydrate.rs index 0de70d25..dadff49a 100644 --- a/packages/sycamore-core/src/hydrate.rs +++ b/packages/sycamore-core/src/hydrate.rs @@ -6,7 +6,7 @@ use std::cell::RefCell; thread_local! { - static HYDRATION_CONTEXT: RefCell> = RefCell::new(None); + static HYDRATION_CONTEXT: RefCell> = const { RefCell::new(None) }; } /// Run the closure inside a hydration context. If already inside a hydration context, creates a diff --git a/packages/sycamore-reactive/src/iter.rs b/packages/sycamore-reactive/src/iter.rs index 2739d9fa..fb30bcf2 100644 --- a/packages/sycamore-reactive/src/iter.rs +++ b/packages/sycamore-reactive/src/iter.rs @@ -19,9 +19,9 @@ use crate::*; /// * `key_fn` - A closure that returns an _unique_ key to each entry. /// /// _Credits: Based on TypeScript implementation in _ -pub fn map_keyed( - list: impl Accessor> + Clone + 'static, - map_fn: impl Fn(T) -> U + 'static, +pub fn map_keyed( + list: impl Accessor> + 'static, + mut map_fn: impl FnMut(T) -> U + 'static, key_fn: impl Fn(&T) -> K + 'static, ) -> ReadSignal> where @@ -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)); @@ -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); @@ -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(); @@ -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( - list: impl Accessor> + Clone + 'static, - map_fn: impl Fn(T) -> U + 'static, +pub fn map_indexed( + list: impl Accessor> + 'static, + mut map_fn: impl FnMut(T) -> U + 'static, ) -> ReadSignal> where T: PartialEq + Clone + 'static, diff --git a/packages/sycamore-reactive/src/root.rs b/packages/sycamore-reactive/src/root.rs index 251473b3..cd8b8bc9 100644 --- a/packages/sycamore-reactive/src/root.rs +++ b/packages/sycamore-reactive/src/root.rs @@ -35,7 +35,7 @@ pub(crate) struct Root { thread_local! { /// The current reactive root. - static GLOBAL_ROOT: Cell> = Cell::new(None); + static GLOBAL_ROOT: Cell> = const { Cell::new(None) }; } impl Root { @@ -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(*const T); /// We never ever touch the pointer inside so surely this is safe! unsafe impl Send for UnsafeSendPtr {} diff --git a/packages/sycamore-router/src/router.rs b/packages/sycamore-router/src/router.rs index 1b8dcc1e..ccb1cb77 100644 --- a/packages/sycamore-router/src/router.rs +++ b/packages/sycamore-router/src/router.rs @@ -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; @@ -25,7 +24,7 @@ pub trait Integration { } thread_local! { - static PATHNAME: Cell>> = Cell::new(None); + static PATHNAME: Cell>> = const { Cell::new(None) }; } /// A router integration that uses the diff --git a/packages/sycamore-web/src/dom_node.rs b/packages/sycamore-web/src/dom_node.rs index b93f972d..7ae647ad 100644 --- a/packages/sycamore-web/src/dom_node.rs +++ b/packages/sycamore-web/src/dom_node.rs @@ -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::{ @@ -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; + pub(crate) fn node_id(this: &NodeWithId) -> Option; #[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")] @@ -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 = Cell::new(1)); // 0 is reserved for default value. + thread_local!(static NODE_ID_COUNTER: Cell = const { Cell::new(1) }); // 0 is reserved for default value. let id = NODE_ID_COUNTER.with(|x| { let tmp = x.get(); diff --git a/packages/sycamore-web/src/hydrate_node.rs b/packages/sycamore-web/src/hydrate_node.rs index 5923a1d7..6f2dd0e2 100644 --- a/packages/sycamore-web/src/hydrate_node.rs +++ b/packages/sycamore-web/src/hydrate_node.rs @@ -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}; diff --git a/packages/sycamore-web/src/ssr_node.rs b/packages/sycamore-web/src/ssr_node.rs index 6133a516..19f5d8ab 100644 --- a/packages/sycamore-web/src/ssr_node.rs +++ b/packages/sycamore-web/src/ssr_node.rs @@ -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; diff --git a/packages/sycamore/src/builder.rs b/packages/sycamore/src/builder.rs index 64f4cf5d..96fc8222 100644 --- a/packages/sycamore/src/builder.rs +++ b/packages/sycamore/src/builder.rs @@ -4,7 +4,6 @@ //! HTML. use std::borrow::Cow; -use std::iter::FromIterator; use std::rc::Rc; use sycamore_core::event::{EventDescriptor, EventHandler}; diff --git a/packages/sycamore/src/utils.rs b/packages/sycamore/src/utils.rs index 234d4f7b..db139ac4 100644 --- a/packages/sycamore/src/utils.rs +++ b/packages/sycamore/src/utils.rs @@ -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; diff --git a/packages/sycamore/src/web/portal.rs b/packages/sycamore/src/web/portal.rs index 2e3de38e..d1755d27 100644 --- a/packages/sycamore/src/web/portal.rs +++ b/packages/sycamore/src/web/portal.rs @@ -4,7 +4,6 @@ use std::any::Any; use wasm_bindgen::prelude::*; -use crate::component::Children; use crate::prelude::*; /// Props for [`Portal`].