mirror of
https://github.com/leptos-rs/leptos
synced 2024-11-10 14:54:16 +00:00
Remove unnecessary Clone bounds
This commit is contained in:
parent
523569cd94
commit
1f34d667fa
10 changed files with 122 additions and 61 deletions
|
@ -15,7 +15,7 @@ where
|
|||
G: Fn(Scope, &T) -> Element,
|
||||
I: Fn(&T) -> K,
|
||||
K: Eq + Hash,
|
||||
T: Eq + Clone + 'static,
|
||||
T: Eq + 'static,
|
||||
{
|
||||
pub each: E,
|
||||
pub key: I,
|
||||
|
@ -34,7 +34,7 @@ where
|
|||
G: Fn(Scope, &T) -> Element + 'static,
|
||||
I: Fn(&T) -> K + 'static,
|
||||
K: Eq + Hash,
|
||||
T: Eq + Clone + Debug + 'static,
|
||||
T: Eq + Debug + 'static,
|
||||
{
|
||||
let map_fn = (props.children)().swap_remove(0);
|
||||
map_keyed(cx, props.each, map_fn, props.key)
|
||||
|
|
|
@ -23,7 +23,7 @@ pub fn map_keyed<T, U, K>(
|
|||
) -> Memo<Vec<U>>
|
||||
//-> impl FnMut() -> Vec<U>
|
||||
where
|
||||
T: PartialEq + Debug + Clone + 'static,
|
||||
T: PartialEq + Debug + 'static,
|
||||
K: Eq + Hash,
|
||||
U: PartialEq + Debug + Clone + 'static,
|
||||
{
|
||||
|
@ -33,8 +33,8 @@ where
|
|||
//let mapped: Vec<U> = Vec::new();
|
||||
|
||||
// Diff and update signal each time list is updated.
|
||||
create_memo(cx, move |mapped: Option<Vec<U>>| {
|
||||
let mut mapped = mapped.unwrap_or_default();
|
||||
create_memo(cx, move |mapped: Option<&Vec<U>>| {
|
||||
let mut mapped = mapped.cloned().unwrap_or_default();
|
||||
let items = prev_items.take().unwrap_or_default();
|
||||
let new_items = list();
|
||||
let new_items_len = new_items.len();
|
||||
|
|
|
@ -54,9 +54,9 @@ use std::fmt::Debug;
|
|||
/// });
|
||||
/// # }).dispose();
|
||||
/// ```
|
||||
pub fn create_memo<T>(cx: Scope, f: impl FnMut(Option<T>) -> T + 'static) -> Memo<T>
|
||||
pub fn create_memo<T>(cx: Scope, f: impl FnMut(Option<&T>) -> T + 'static) -> Memo<T>
|
||||
where
|
||||
T: PartialEq + Clone + Debug + 'static,
|
||||
T: PartialEq + Debug + 'static,
|
||||
{
|
||||
cx.runtime.create_memo(f)
|
||||
}
|
||||
|
@ -99,6 +99,10 @@ where
|
|||
self.0
|
||||
.try_with(|n| f(n.as_ref().expect("Memo is missing its initial value")))
|
||||
}
|
||||
|
||||
pub(crate) fn subscribe(&self) {
|
||||
self.0.subscribe()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "stable"))]
|
||||
|
|
|
@ -61,7 +61,7 @@ pub fn create_resource<S, T, Fu>(
|
|||
) -> Resource<S, T>
|
||||
where
|
||||
S: PartialEq + Debug + Clone + 'static,
|
||||
T: Debug + Clone + Serializable + 'static,
|
||||
T: Debug + Serializable + 'static,
|
||||
Fu: Future<Output = T> + 'static,
|
||||
{
|
||||
#[cfg(not(feature = "ssr"))]
|
||||
|
@ -93,7 +93,7 @@ pub fn create_resource_with_initial_value<S, T, Fu>(
|
|||
) -> Resource<S, T>
|
||||
where
|
||||
S: PartialEq + Debug + Clone + 'static,
|
||||
T: Debug + Clone + Serializable + 'static,
|
||||
T: Debug + Serializable + 'static,
|
||||
Fu: Future<Output = T> + 'static,
|
||||
{
|
||||
let resolved = initial_value.is_some();
|
||||
|
@ -171,7 +171,7 @@ pub fn create_local_resource<S, T, Fu>(
|
|||
) -> Resource<S, T>
|
||||
where
|
||||
S: PartialEq + Debug + Clone + 'static,
|
||||
T: Debug + Clone + 'static,
|
||||
T: Debug + 'static,
|
||||
Fu: Future<Output = T> + 'static,
|
||||
{
|
||||
let initial_value = None;
|
||||
|
@ -193,7 +193,7 @@ pub fn create_local_resource_with_initial_value<S, T, Fu>(
|
|||
) -> Resource<S, T>
|
||||
where
|
||||
S: PartialEq + Debug + Clone + 'static,
|
||||
T: Debug + Clone + 'static,
|
||||
T: Debug + 'static,
|
||||
Fu: Future<Output = T> + 'static,
|
||||
{
|
||||
let resolved = initial_value.is_some();
|
||||
|
@ -240,7 +240,7 @@ where
|
|||
fn load_resource<S, T>(_cx: Scope, _id: ResourceId, r: Rc<ResourceState<S, T>>)
|
||||
where
|
||||
S: PartialEq + Debug + Clone + 'static,
|
||||
T: Debug + Clone + 'static,
|
||||
T: Debug + 'static,
|
||||
{
|
||||
r.load(false)
|
||||
}
|
||||
|
@ -249,7 +249,7 @@ where
|
|||
fn load_resource<S, T>(cx: Scope, id: ResourceId, r: Rc<ResourceState<S, T>>)
|
||||
where
|
||||
S: PartialEq + Debug + Clone + 'static,
|
||||
T: Debug + Clone + Serializable + 'static,
|
||||
T: Debug + Serializable + 'static,
|
||||
{
|
||||
use wasm_bindgen::{JsCast, UnwrapThrowExt};
|
||||
|
||||
|
@ -265,7 +265,7 @@ where
|
|||
r.set_loading.update(|n| *n = false);
|
||||
|
||||
// for reactivity
|
||||
_ = r.source.try_with(|n| n.clone());
|
||||
r.source.subscribe();
|
||||
} else if context.pending_resources.remove(&id) {
|
||||
// We're still waiting for the resource, add a "resolver" closure so
|
||||
// that it will be set as soon as the server sends the serialized
|
||||
|
@ -299,7 +299,7 @@ where
|
|||
);
|
||||
|
||||
// for reactivity
|
||||
_ = r.source.get();
|
||||
r.source.subscribe()
|
||||
} else {
|
||||
// Server didn't mark the resource as pending, so load it on the
|
||||
// client
|
||||
|
@ -313,13 +313,21 @@ where
|
|||
impl<S, T> Resource<S, T>
|
||||
where
|
||||
S: Debug + Clone + 'static,
|
||||
T: Debug + Clone + 'static,
|
||||
T: Debug + 'static,
|
||||
{
|
||||
pub fn read(&self) -> Option<T>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
pub fn read(&self) -> Option<T> {
|
||||
self.runtime
|
||||
.resource(self.id, |resource: &ResourceState<S, T>| resource.read())
|
||||
}
|
||||
|
||||
pub fn with<U>(&self, f: impl FnOnce(&T) -> U) -> Option<U> {
|
||||
self.runtime
|
||||
.resource(self.id, |resource: &ResourceState<S, T>| resource.with(f))
|
||||
}
|
||||
|
||||
pub fn loading(&self) -> bool {
|
||||
self.runtime
|
||||
.resource(self.id, |resource: &ResourceState<S, T>| {
|
||||
|
@ -348,8 +356,8 @@ where
|
|||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct Resource<S, T>
|
||||
where
|
||||
S: Debug + Clone + 'static,
|
||||
T: Debug + Clone + 'static,
|
||||
S: Debug + 'static,
|
||||
T: Debug + 'static,
|
||||
{
|
||||
runtime: &'static Runtime,
|
||||
pub(crate) id: ResourceId,
|
||||
|
@ -421,7 +429,7 @@ where
|
|||
pub(crate) struct ResourceState<S, T>
|
||||
where
|
||||
S: 'static,
|
||||
T: Clone + Debug + 'static,
|
||||
T: Debug + 'static,
|
||||
{
|
||||
scope: Scope,
|
||||
value: ReadSignal<Option<T>>,
|
||||
|
@ -439,17 +447,28 @@ where
|
|||
impl<S, T> ResourceState<S, T>
|
||||
where
|
||||
S: Debug + Clone + 'static,
|
||||
T: Debug + Clone + 'static,
|
||||
T: Debug + 'static,
|
||||
{
|
||||
pub fn read(&self) -> Option<T> {
|
||||
pub fn read(&self) -> Option<T>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
self.with(T::clone)
|
||||
}
|
||||
|
||||
pub fn with<U>(&self, f: impl FnOnce(&T) -> U) -> Option<U> {
|
||||
let suspense_cx = use_context::<SuspenseContext>(self.scope);
|
||||
|
||||
let v = self.value.try_with(|n| n.clone()).ok()?;
|
||||
let v = self
|
||||
.value
|
||||
.try_with(|n| n.as_ref().map(|n| Some(f(n))))
|
||||
.ok()?
|
||||
.flatten();
|
||||
|
||||
let suspense_contexts = self.suspense_contexts.clone();
|
||||
let has_value = v.is_some();
|
||||
|
||||
let increment = move |_| {
|
||||
let increment = move |_: Option<()>| {
|
||||
if let Some(s) = &suspense_cx {
|
||||
let mut contexts = suspense_contexts.borrow_mut();
|
||||
if !contexts.contains(s) {
|
||||
|
@ -466,7 +485,6 @@ where
|
|||
};
|
||||
|
||||
create_isomorphic_effect(self.scope, increment);
|
||||
|
||||
v
|
||||
}
|
||||
|
||||
|
@ -532,7 +550,7 @@ where
|
|||
where
|
||||
T: Serializable,
|
||||
{
|
||||
let fut = (self.fetcher)(self.source.get());
|
||||
let fut = self.source.with(|s| (self.fetcher)(s.clone()));
|
||||
Box::pin(async move {
|
||||
let res = fut.await;
|
||||
(id, res.to_json().expect("could not serialize Resource"))
|
||||
|
@ -558,7 +576,7 @@ pub(crate) trait SerializableResource {
|
|||
impl<S, T> SerializableResource for ResourceState<S, T>
|
||||
where
|
||||
S: Debug + Clone,
|
||||
T: Clone + Debug + Serializable,
|
||||
T: Debug + Serializable,
|
||||
{
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
|
@ -580,8 +598,8 @@ pub(crate) trait UnserializableResource {
|
|||
|
||||
impl<S, T> UnserializableResource for ResourceState<S, T>
|
||||
where
|
||||
S: Debug + Clone,
|
||||
T: Clone + Debug,
|
||||
S: Debug,
|
||||
T: Debug,
|
||||
{
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
|
|
|
@ -129,19 +129,23 @@ impl Runtime {
|
|||
|
||||
pub(crate) fn create_memo<T>(
|
||||
&'static self,
|
||||
mut f: impl FnMut(Option<T>) -> T + 'static,
|
||||
mut f: impl FnMut(Option<&T>) -> T + 'static,
|
||||
) -> Memo<T>
|
||||
where
|
||||
T: Clone + PartialEq + Any + 'static,
|
||||
T: PartialEq + Any + 'static,
|
||||
{
|
||||
let (read, write) = self.create_signal(None);
|
||||
self.create_effect(move |prev| {
|
||||
let new = { f(prev.clone()) };
|
||||
|
||||
if prev.as_ref() != Some(&new) {
|
||||
write.update(|n| *n = Some(new.clone()));
|
||||
self.create_effect(move |_| {
|
||||
let (new, changed) = read.with_no_subscription(|p| {
|
||||
let new = f(p.as_ref());
|
||||
let changed = Some(&new) != p.as_ref();
|
||||
(new, changed)
|
||||
});
|
||||
|
||||
if changed {
|
||||
write.update(|n| *n = Some(new));
|
||||
}
|
||||
new
|
||||
});
|
||||
|
||||
Memo(read)
|
||||
|
@ -153,7 +157,7 @@ impl Runtime {
|
|||
) -> ResourceId
|
||||
where
|
||||
S: Debug + Clone + 'static,
|
||||
T: Debug + Clone + 'static,
|
||||
T: Debug + 'static,
|
||||
{
|
||||
self.resources
|
||||
.borrow_mut()
|
||||
|
@ -166,7 +170,7 @@ impl Runtime {
|
|||
) -> ResourceId
|
||||
where
|
||||
S: Debug + Clone + 'static,
|
||||
T: Debug + Clone + Serializable + 'static,
|
||||
T: Debug + Serializable + 'static,
|
||||
{
|
||||
self.resources
|
||||
.borrow_mut()
|
||||
|
@ -206,8 +210,8 @@ impl Runtime {
|
|||
f: impl FnOnce(&ResourceState<S, T>) -> U,
|
||||
) -> U
|
||||
where
|
||||
S: Debug + Clone + 'static,
|
||||
T: Debug + Clone + 'static,
|
||||
S: Debug + 'static,
|
||||
T: Debug + 'static,
|
||||
{
|
||||
let resources = self.resources.borrow();
|
||||
let res = resources.get(id);
|
||||
|
|
|
@ -128,6 +128,14 @@ where
|
|||
self.id.with(self.runtime, f)
|
||||
}
|
||||
|
||||
pub(crate) fn with_no_subscription<U>(&self, f: impl FnOnce(&T) -> U) -> U {
|
||||
self.id.with_no_subscription(self.runtime, f)
|
||||
}
|
||||
|
||||
pub(crate) fn subscribe(&self) {
|
||||
self.id.subscribe(self.runtime);
|
||||
}
|
||||
|
||||
/// Clones and returns the current value of the signal, and subscribes
|
||||
/// the running effect to this signal.
|
||||
///
|
||||
|
@ -245,7 +253,7 @@ where
|
|||
|
||||
impl<T> WriteSignal<T>
|
||||
where
|
||||
T: Clone + 'static,
|
||||
T: 'static,
|
||||
{
|
||||
/// Applies a function to the current value to mutate it in place
|
||||
/// and notifies subscribers that the signal has changed.
|
||||
|
@ -295,10 +303,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> Clone for WriteSignal<T>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
impl<T> Clone for WriteSignal<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
runtime: self.runtime,
|
||||
|
@ -308,7 +313,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> Copy for WriteSignal<T> where T: Clone {}
|
||||
impl<T> Copy for WriteSignal<T> {}
|
||||
|
||||
#[cfg(not(feature = "stable"))]
|
||||
impl<T> FnOnce<(T,)> for WriteSignal<T>
|
||||
|
@ -438,6 +443,14 @@ where
|
|||
self.id.with(self.runtime, f)
|
||||
}
|
||||
|
||||
pub(crate) fn with_no_subscription<U>(&self, f: impl FnOnce(&T) -> U) -> U {
|
||||
self.id.with_no_subscription(self.runtime, f)
|
||||
}
|
||||
|
||||
pub(crate) fn subscribe(&self) {
|
||||
self.id.subscribe(self.runtime);
|
||||
}
|
||||
|
||||
/// Clones and returns the current value of the signal, and subscribes
|
||||
/// the running effect to this signal.
|
||||
/// ```
|
||||
|
@ -567,14 +580,7 @@ pub(crate) enum SignalError {
|
|||
}
|
||||
|
||||
impl SignalId {
|
||||
pub(crate) fn try_with<T, U>(
|
||||
&self,
|
||||
runtime: &Runtime,
|
||||
f: impl FnOnce(&T) -> U,
|
||||
) -> Result<U, SignalError>
|
||||
where
|
||||
T: 'static,
|
||||
{
|
||||
pub(crate) fn subscribe(&self, runtime: &Runtime) {
|
||||
// add subscriber
|
||||
if let Some(observer) = runtime.observer.get() {
|
||||
let mut subs = runtime.signal_subscribers.borrow_mut();
|
||||
|
@ -582,7 +588,16 @@ impl SignalId {
|
|||
subs.or_default().borrow_mut().insert(observer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn try_with_no_subscription<T, U>(
|
||||
&self,
|
||||
runtime: &Runtime,
|
||||
f: impl FnOnce(&T) -> U,
|
||||
) -> Result<U, SignalError>
|
||||
where
|
||||
T: 'static,
|
||||
{
|
||||
// get the value
|
||||
let value = {
|
||||
let signals = runtime.signals.borrow();
|
||||
|
@ -601,6 +616,26 @@ impl SignalId {
|
|||
Ok(f(value))
|
||||
}
|
||||
|
||||
pub(crate) fn try_with<T, U>(
|
||||
&self,
|
||||
runtime: &Runtime,
|
||||
f: impl FnOnce(&T) -> U,
|
||||
) -> Result<U, SignalError>
|
||||
where
|
||||
T: 'static,
|
||||
{
|
||||
self.subscribe(runtime);
|
||||
|
||||
self.try_with_no_subscription(runtime, f)
|
||||
}
|
||||
|
||||
pub(crate) fn with_no_subscription<T, U>(&self, runtime: &Runtime, f: impl FnOnce(&T) -> U) -> U
|
||||
where
|
||||
T: 'static,
|
||||
{
|
||||
self.try_with_no_subscription(runtime, f).unwrap()
|
||||
}
|
||||
|
||||
pub(crate) fn with<T, U>(&self, runtime: &Runtime, f: impl FnOnce(&T) -> U) -> U
|
||||
where
|
||||
T: 'static,
|
||||
|
|
|
@ -46,7 +46,7 @@ pub fn Routes(cx: Scope, props: RoutesProps) -> impl IntoChild {
|
|||
|
||||
let route_states: Memo<RouterState> = create_memo(cx, {
|
||||
let root_equal = root_equal.clone();
|
||||
move |prev: Option<RouterState>| {
|
||||
move |prev: Option<&RouterState>| {
|
||||
root_equal.set(true);
|
||||
next.borrow_mut().clear();
|
||||
|
||||
|
@ -154,7 +154,7 @@ pub fn Routes(cx: Scope, props: RoutesProps) -> impl IntoChild {
|
|||
if prev.is_none() || !root_equal.get() {
|
||||
root.as_ref().map(|route| route.outlet().into_child(cx))
|
||||
} else {
|
||||
prev.clone().unwrap()
|
||||
prev.cloned().unwrap()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
|
|
@ -15,7 +15,7 @@ impl Action {
|
|||
|
||||
impl<F, Fu> From<F> for Action
|
||||
where
|
||||
F: Fn(&Request) -> Fu + Clone + 'static,
|
||||
F: Fn(&Request) -> Fu + 'static,
|
||||
Fu: Future<Output = Response> + 'static,
|
||||
{
|
||||
fn from(f: F) -> Self {
|
||||
|
|
|
@ -5,12 +5,12 @@ use crate::{State, Url};
|
|||
use super::params::ParamsMap;
|
||||
|
||||
pub fn create_location(cx: Scope, path: ReadSignal<String>, state: ReadSignal<State>) -> Location {
|
||||
let url = create_memo(cx, move |prev: Option<Url>| {
|
||||
let url = create_memo(cx, move |prev: Option<&Url>| {
|
||||
path.with(|path| match Url::try_from(path.as_str()) {
|
||||
Ok(url) => url,
|
||||
Err(e) => {
|
||||
log::error!("[Leptos Router] Invalid path {path}\n\n{e:?}");
|
||||
prev.clone().unwrap()
|
||||
prev.cloned().unwrap()
|
||||
}
|
||||
})
|
||||
});
|
||||
|
|
|
@ -26,7 +26,7 @@ pub fn use_location(cx: Scope) -> Location {
|
|||
|
||||
pub fn use_params<T: Params>(cx: Scope) -> Memo<Result<T, RouterError>>
|
||||
where
|
||||
T: PartialEq + std::fmt::Debug + Clone,
|
||||
T: PartialEq + std::fmt::Debug,
|
||||
{
|
||||
let route = use_route(cx);
|
||||
create_memo(cx, move |_| route.params().with(T::from_map))
|
||||
|
@ -39,7 +39,7 @@ pub fn use_params_map(cx: Scope) -> Memo<ParamsMap> {
|
|||
|
||||
pub fn use_query<T: Params>(cx: Scope) -> Memo<Result<T, RouterError>>
|
||||
where
|
||||
T: PartialEq + std::fmt::Debug + Clone,
|
||||
T: PartialEq + std::fmt::Debug,
|
||||
{
|
||||
let router = use_router(cx);
|
||||
create_memo(cx, move |_| {
|
||||
|
|
Loading…
Reference in a new issue