fix merge errors

This commit is contained in:
Evan Almloff committed 2024-01-02 14:55:46 -06:00
1 parent cddcd69200
commit 73b7f450a5
5 files changed
+45 -145

No files matched your search

-28
View File
@@ -1,28 +0,0 @@
use dioxus::prelude::*;
use dioxus_signals::*;
fn main() {
dioxus_desktop::launch(App);
}
#[component]
fn App(cx: Scope) -> Element {
let signal = use_signal(cx, || vec![String::from("Hello"), String::from("World")]);
render! {
button {
onclick: move |_| {
signal.write().push(String::from("Hello"));
},
"Add one"
}
for item in signal.iter_signals() {
Child { signal: item }
}
}
}
#[component]
fn Child(cx: Scope, signal: MappedSignal<String>) -> Element {
render! {"{signal:?}"}
}
-39
View File
@@ -1,39 +0,0 @@
use dioxus::prelude::*;
use dioxus_signals::*;
fn main() {
dioxus_desktop::launch(App);
}
#[component]
fn App(cx: Scope) -> Element {
let signal = use_signal(cx, || Some(String::from("Hello")));
render! {
button {
onclick: move |_| {
let new_value = (!signal.read().is_some()).then(|| String::from("Hello"));
signal.set(new_value);
},
"Swap"
}
button {
onclick: move |_| {
if let Some(value) = &mut *signal.write() {
value.push_str(" World");
}
},
"Change"
}
if let Some(item) = signal.as_mapped_ref() {
render! {
Child { signal: item }
}
}
}
}
#[component]
fn Child(cx: Scope, signal: MappedSignal<String>) -> Element {
render! {"{signal:?}"}
}
+9 -41
View File
@@ -1,6 +1,5 @@
use crate::rt::CopyValue; use crate::rt::CopyValue;
use crate::signal::{ReadOnlySignal, Signal, Write}; use crate::signal::{ReadOnlySignal, Signal, Write};
use crate::MappedSignal;
use crate::SignalData; use crate::SignalData;
use generational_box::Mappable; use generational_box::Mappable;
use generational_box::{MappableMut, Storage}; use generational_box::{MappableMut, Storage};
@@ -333,7 +332,7 @@ pub struct CopyValueIterator<T: 'static, S: Storage<Vec<T>>> {
} }
impl<T, S: Storage<Vec<T>>> Iterator for CopyValueIterator<T, S> { impl<T, S: Storage<Vec<T>>> Iterator for CopyValueIterator<T, S> {
type Item = S::Ref; type Item = <S::Ref as Mappable<Vec<T>>>::Mapped<T>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let index = self.index; let index = self.index;
@@ -343,9 +342,9 @@ impl<T, S: Storage<Vec<T>>> Iterator for CopyValueIterator<T, S> {
} }
impl<T: 'static, S: Storage<Vec<T>>> IntoIterator for CopyValue<Vec<T>, S> { impl<T: 'static, S: Storage<Vec<T>>> IntoIterator for CopyValue<Vec<T>, S> {
type IntoIter = S::Ref; type IntoIter = CopyValueIterator<T, S>;
type Item = GenerationalRef<T>; type Item = <S::Ref as Mappable<Vec<T>>>::Mapped<T>;
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
CopyValueIterator { CopyValueIterator {
@@ -378,7 +377,9 @@ pub struct SignalIterator<T: 'static, S: Storage<SignalData<Vec<T>>>> {
} }
impl<T, S: Storage<SignalData<Vec<T>>>> Iterator for SignalIterator<T, S> { impl<T, S: Storage<SignalData<Vec<T>>>> Iterator for SignalIterator<T, S> {
type Item = S::Ref; type Item = <<<S as Storage<SignalData<Vec<T>>>>::Ref as Mappable<SignalData<Vec<T>>>>::Mapped<
Vec<T>,
> as Mappable<Vec<T>>>::Mapped<T>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let index = self.index; let index = self.index;
@@ -390,7 +391,9 @@ impl<T, S: Storage<SignalData<Vec<T>>>> Iterator for SignalIterator<T, S> {
impl<T: 'static, S: Storage<SignalData<Vec<T>>>> IntoIterator for Signal<Vec<T>, S> { impl<T: 'static, S: Storage<SignalData<Vec<T>>>> IntoIterator for Signal<Vec<T>, S> {
type IntoIter = SignalIterator<T, S>; type IntoIter = SignalIterator<T, S>;
type Item = GenerationalRef<T>; type Item = <<<S as Storage<SignalData<Vec<T>>>>::Ref as Mappable<SignalData<Vec<T>>>>::Mapped<
Vec<T>,
> as Mappable<Vec<T>>>::Mapped<T>;
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
SignalIterator { SignalIterator {
@@ -400,23 +403,6 @@ impl<T: 'static, S: Storage<SignalData<Vec<T>>>> IntoIterator for Signal<Vec<T>,
} }
} }
/// An iterator over items in a `Signal<Vec<T>>` that yields [`MappedSignal`]s.
pub struct MappedSignalIterator<T: 'static, S: Storage<SignalData<Vec<T>>>> {
index: usize,
length: usize,
value: Signal<Vec<T>, S>,
}
impl<T, S: Storage<SignalData<Vec<T>>>> Iterator for MappedSignalIterator<T, S> {
type Item = MappedSignal<T, S>;
fn next(&mut self) -> Option<Self::Item> {
let index = self.index;
self.index += 1;
(index < self.length).then(|| self.value.map(move |v| v.get(index).unwrap()))
}
}
impl<T: 'static, S: Storage<SignalData<Vec<T>>>> Signal<Vec<T>, S> impl<T: 'static, S: Storage<SignalData<Vec<T>>>> Signal<Vec<T>, S>
where where
<<S as Storage<SignalData<std::vec::Vec<T>>>>::Mut as MappableMut< <<S as Storage<SignalData<std::vec::Vec<T>>>>::Mut as MappableMut<
@@ -439,15 +425,6 @@ where
> { > {
Write::filter_map(self.write(), |v| v.get_mut(index)) Write::filter_map(self.write(), |v| v.get_mut(index))
} }
/// Create an iterator of [`MappedSignal`]s over the inner vector.
pub fn iter_signals(&self) -> MappedSignalIterator<T> {
MappedSignalIterator {
index: 0,
length: self.read().len(),
value: *self,
}
}
} }
impl<T: 'static, S: Storage<SignalData<Option<T>>>> Signal<Option<T>, S> { impl<T: 'static, S: Storage<SignalData<Option<T>>>> Signal<Option<T>, S> {
@@ -455,13 +432,4 @@ impl<T: 'static, S: Storage<SignalData<Option<T>>>> Signal<Option<T>, S> {
pub fn as_mut(&self) -> Option<Write<T, <<<S as Storage<SignalData<Option<T>>>>::Mut as MappableMut<SignalData<Option<T>>>>::Mapped<Option<T>> as MappableMut<Option<T>>>::Mapped<T>, S, Option<T>>>{ pub fn as_mut(&self) -> Option<Write<T, <<<S as Storage<SignalData<Option<T>>>>::Mut as MappableMut<SignalData<Option<T>>>>::Mapped<Option<T>> as MappableMut<Option<T>>>::Mapped<T>, S, Option<T>>>{
Write::filter_map(self.write(), |v| v.as_mut()) Write::filter_map(self.write(), |v| v.as_mut())
} }
/// Try to create a [`MappedSignal`] over the inner value.
pub fn as_mapped_ref(&self) -> Option<MappedSignal<T>> {
if self.read().is_some() {
Some(self.map(|v| v.as_ref().unwrap()))
} else {
None
}
}
} }
+28 -36
View File
@@ -1,40 +1,54 @@
use crate::CopyValue; use crate::CopyValue;
use crate::Signal; use crate::Signal;
use crate::SignalData;
use dioxus_core::ScopeId; use dioxus_core::ScopeId;
use generational_box::GenerationalRef; use generational_box::Mappable;
use generational_box::Storage;
use std::fmt::Debug; use std::fmt::Debug;
use std::fmt::Display; use std::fmt::Display;
/// A read only signal that has been mapped to a new type. /// A read only signal that has been mapped to a new type.
pub struct MappedSignal<U: 'static + ?Sized> { pub struct MappedSignal<U: 'static + ?Sized> {
origin_scope: ScopeId, origin_scope: ScopeId,
mapping: CopyValue<Box<dyn Fn() -> GenerationalRef<U>>>, mapping: CopyValue<Box<dyn Fn() -> U>>,
} }
impl<U: ?Sized> MappedSignal<U> { impl MappedSignal<()> {
/// Create a new mapped signal. /// Create a new mapped signal.
pub fn new<T: 'static>(signal: Signal<T>, mapping: impl Fn(&T) -> &U + 'static) -> Self { pub fn new<T, S: Storage<SignalData<T>>, U>(
Self { signal: Signal<T, S>,
mapping: impl Fn(&T) -> &U + 'static,
) -> MappedSignal<
<<<S as generational_box::Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<
T,
> as generational_box::Mappable<T>>::Mapped<U>,
> {
MappedSignal {
origin_scope: signal.origin_scope(), origin_scope: signal.origin_scope(),
mapping: CopyValue::new(Box::new(move || { mapping: CopyValue::new(Box::new(move || {
GenerationalRef::map(signal.read(), |v| (mapping)(v)) <<<S as Storage<SignalData<T>>>::Ref as Mappable<SignalData<T>>>::Mapped<T>>::map(
signal.read(),
&mapping,
)
})), })),
} }
} }
}
impl<U> MappedSignal<U> {
/// Get the scope that the signal was created in. /// Get the scope that the signal was created in.
pub fn origin_scope(&self) -> ScopeId { pub fn origin_scope(&self) -> ScopeId {
self.origin_scope self.origin_scope
} }
/// Get the current value of the signal. This will subscribe the current scope to the signal. /// Get the current value of the signal. This will subscribe the current scope to the signal.
pub fn read(&self) -> GenerationalRef<U> { pub fn read(&self) -> U {
(self.mapping.read())() (self.mapping.read())()
} }
/// Run a closure with a reference to the signal's value. /// Run a closure with a reference to the signal's value.
pub fn with<O>(&self, f: impl FnOnce(&U) -> O) -> O { pub fn with<O>(&self, f: impl FnOnce(U) -> O) -> O {
f(&*self.read()) f(self.read())
} }
} }
@@ -59,42 +73,20 @@ impl<U> std::clone::Clone for MappedSignal<U> {
impl<U> Copy for MappedSignal<U> {} impl<U> Copy for MappedSignal<U> {}
impl<U: ?Sized + Display> Display for MappedSignal<U> { impl<U: Display> Display for MappedSignal<U> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.with(|v| Display::fmt(v, f)) self.with(|v| Display::fmt(&v, f))
} }
} }
impl<U: ?Sized + Debug> Debug for MappedSignal<U> { impl<U: Debug> Debug for MappedSignal<U> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.with(|v| Debug::fmt(v, f)) self.with(|v| Debug::fmt(&v, f))
}
}
impl<U> MappedSignal<Vec<U>> {
/// Read a value from the inner vector.
pub fn get(&self, index: usize) -> Option<GenerationalRef<U>> {
GenerationalRef::filter_map(self.read(), |v| v.get(index))
}
}
impl<U: Clone + 'static> MappedSignal<Option<U>> {
/// Unwraps the inner value and clones it.
pub fn unwrap(&self) -> U
where
U: Clone,
{
self.with(|v| v.clone()).unwrap()
}
/// Attemps to read the inner value of the Option.
pub fn as_ref(&self) -> Option<GenerationalRef<U>> {
GenerationalRef::filter_map(self.read(), |v| v.as_ref())
} }
} }
impl<T> std::ops::Deref for MappedSignal<T> { impl<T> std::ops::Deref for MappedSignal<T> {
type Target = dyn Fn() -> GenerationalRef<T>; type Target = dyn Fn() -> T;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
// https://github.com/dtolnay/case-studies/tree/master/callable-types // https://github.com/dtolnay/case-studies/tree/master/callable-types
+8 -1
View File
@@ -394,7 +394,14 @@ impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
} }
/// Map the signal to a new type. /// Map the signal to a new type.
pub fn map<O>(self, f: impl Fn(&T) -> &O + 'static) -> MappedSignal<O> { pub fn map<O>(
self,
f: impl Fn(&T) -> &O + 'static,
) -> MappedSignal<
<<<S as generational_box::Storage<SignalData<T>>>::Ref as generational_box::Mappable<
SignalData<T>,
>>::Mapped<T> as generational_box::Mappable<T>>::Mapped<O>,
> {
MappedSignal::new(self, f) MappedSignal::new(self, f)
} }