Fix cargo test and a number of little cleanup bugs
This commit is contained in:
49 files changed
+307
-309
No files matched your search
@@ -19,23 +19,23 @@ impl<R: Eq + Hash> Comparer<R> {
|
||||
///
|
||||
/// Generally, you shouldn't need to use this hook. Instead you can use [`crate::use_memo`]. If you have many values that you need to compare to a single value, this hook will change updates from O(n) to O(1) where n is the number of values you are comparing to.
|
||||
pub fn new(mut f: impl FnMut() -> R + 'static) -> Comparer<R> {
|
||||
let subscribers: CopyValue<FxHashMap<R, Signal<bool>>> =
|
||||
let mut subscribers: CopyValue<FxHashMap<R, Signal<bool>>> =
|
||||
CopyValue::new(FxHashMap::default());
|
||||
let previous = CopyValue::new(None);
|
||||
let mut previous = CopyValue::new(None);
|
||||
|
||||
Effect::new(move || {
|
||||
let subscribers = subscribers.read();
|
||||
let mut subscribers = subscribers.read();
|
||||
let mut previous = previous.write();
|
||||
|
||||
if let Some(previous) = previous.take() {
|
||||
if let Some(value) = subscribers.get(&previous) {
|
||||
*value.write() = false;
|
||||
if let Some(mut value) = subscribers.get(&previous).cloned() {
|
||||
value.set(false)
|
||||
}
|
||||
}
|
||||
|
||||
let current = f();
|
||||
|
||||
if let Some(value) = subscribers.get(¤t) {
|
||||
if let Some(mut value) = subscribers.get(¤t).cloned() {
|
||||
*value.write() = true;
|
||||
}
|
||||
|
||||
@@ -53,21 +53,21 @@ impl<R: Eq + Hash, S: Storage<SignalData<bool>>> Comparer<R, S> {
|
||||
pub fn new_maybe_sync(mut f: impl FnMut() -> R + 'static) -> Comparer<R> {
|
||||
let subscribers: CopyValue<FxHashMap<R, Signal<bool>>> =
|
||||
CopyValue::new(FxHashMap::default());
|
||||
let previous = CopyValue::new(None);
|
||||
let mut previous = CopyValue::new(None);
|
||||
|
||||
Effect::new(move || {
|
||||
let subscribers = subscribers.read();
|
||||
let mut previous = previous.write();
|
||||
|
||||
if let Some(previous) = previous.take() {
|
||||
if let Some(value) = subscribers.get(&previous) {
|
||||
if let Some(mut value) = subscribers.get(&previous).cloned() {
|
||||
*value.write() = false;
|
||||
}
|
||||
}
|
||||
|
||||
let current = f();
|
||||
|
||||
if let Some(value) = subscribers.get(¤t) {
|
||||
if let Some(mut value) = subscribers.get(¤t).cloned() {
|
||||
*value.write() = true;
|
||||
}
|
||||
|
||||
@@ -78,8 +78,8 @@ impl<R: Eq + Hash, S: Storage<SignalData<bool>>> Comparer<R, S> {
|
||||
}
|
||||
|
||||
/// Returns a signal which is true when the value is equal to the value passed to this function.
|
||||
pub fn equal(&self, value: R) -> ReadOnlySignal<bool, S> {
|
||||
let subscribers = self.subscribers.read();
|
||||
pub fn equal(&mut self, value: R) -> ReadOnlySignal<bool, S> {
|
||||
let mut subscribers = self.subscribers.write();
|
||||
|
||||
match subscribers.get(&value) {
|
||||
Some(&signal) => signal.into(),
|
||||
|
||||
@@ -3,7 +3,7 @@ use crate::write::Writable;
|
||||
use crate::Write;
|
||||
use dioxus_core::prelude::{IntoAttributeValue, ScopeId};
|
||||
use generational_box::{AnyStorage, GenerationalRef, UnsyncStorage};
|
||||
use std::{cell::Ref, mem::MaybeUninit, ops::Deref};
|
||||
use std::{cell::Ref, io::prelude::Read, mem::MaybeUninit, ops::Deref};
|
||||
|
||||
use super::get_global_context;
|
||||
use crate::{MappedSignal, Signal};
|
||||
@@ -43,6 +43,10 @@ impl<T: 'static> GlobalSignal<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write(&self) -> Write<T, UnsyncStorage> {
|
||||
self.signal().write()
|
||||
}
|
||||
|
||||
/// Get the scope the signal was created in.
|
||||
pub fn origin_scope(&self) -> ScopeId {
|
||||
ScopeId::ROOT
|
||||
@@ -143,8 +147,11 @@ impl<T: Clone + 'static> Deref for GlobalSignal<T> {
|
||||
|
||||
// First we create a closure that captures something with the Same in memory layout as Self (MaybeUninit<Self>).
|
||||
let uninit_callable = MaybeUninit::<Self>::uninit();
|
||||
|
||||
// Then move that value into the closure. We assume that the closure now has a in memory layout of Self.
|
||||
let uninit_closure = move || Self::read(unsafe { &*uninit_callable.as_ptr() }).clone();
|
||||
let uninit_closure = move || {
|
||||
<GlobalSignal<T> as Readable<T>>::read(unsafe { &*uninit_callable.as_ptr() }).clone()
|
||||
};
|
||||
|
||||
// Check that the size of the closure is the same as the size of Self in case the compiler changed the layout of the closure.
|
||||
let size_of_closure = std::mem::size_of_val(&uninit_closure);
|
||||
|
||||
@@ -22,7 +22,7 @@ fn into_signal_compiles() {
|
||||
|
||||
fn don_t_run() {
|
||||
takes_signal_string("hello world");
|
||||
takes_signal_string(ReadOnlySignal::new(String::from("hello world")));
|
||||
takes_signal_string(Signal::new(String::from("hello world")));
|
||||
takes_option_signal_string("hello world");
|
||||
}
|
||||
}
|
||||
@@ -64,7 +64,9 @@ fn current_owner<S: Storage<T>, T>() -> Owner<S> {
|
||||
let id = TypeId::of::<S>();
|
||||
let override_owner = if id == TypeId::of::<SyncStorage>() {
|
||||
SYNC_OWNER.with(|cell| {
|
||||
cell.borrow().clone().map(|owner| {
|
||||
let owner = cell.borrow();
|
||||
|
||||
owner.clone().map(|owner| {
|
||||
*(Box::new(owner) as Box<dyn Any>)
|
||||
.downcast::<Owner<S>>()
|
||||
.unwrap()
|
||||
@@ -268,7 +270,7 @@ impl<T: 'static, S: Storage<T>> Writable<T> for CopyValue<T, S> {
|
||||
self.value.try_write()
|
||||
}
|
||||
|
||||
fn write(&self) -> Self::Mut<T> {
|
||||
fn write(&mut self) -> Self::Mut<T> {
|
||||
self.value.write()
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ pub trait Writable<T: 'static>: Readable<T> {
|
||||
|
||||
/// Get a mutable reference to the value. If the value has been dropped, this will panic.
|
||||
#[track_caller]
|
||||
fn write(&self) -> Self::Mut<T> {
|
||||
fn write(&mut self) -> Self::Mut<T> {
|
||||
self.try_write().unwrap()
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ pub trait Writable<T: 'static>: Readable<T> {
|
||||
|
||||
/// Run a function with a mutable reference to the value. If the value has been dropped, this will panic.
|
||||
#[track_caller]
|
||||
fn with_mut<O>(&self, f: impl FnOnce(&mut T) -> O) -> O {
|
||||
fn with_mut<O>(&mut self, f: impl FnOnce(&mut T) -> O) -> O {
|
||||
f(&mut *self.write())
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ pub trait Writable<T: 'static>: Readable<T> {
|
||||
|
||||
/// Index into the inner value and return a reference to the result.
|
||||
#[track_caller]
|
||||
fn index_mut<I>(&self, index: I) -> Self::Mut<T::Output>
|
||||
fn index_mut<I>(&mut self, index: I) -> Self::Mut<T::Output>
|
||||
where
|
||||
T: std::ops::IndexMut<I>,
|
||||
{
|
||||
@@ -58,7 +58,7 @@ pub trait Writable<T: 'static>: Readable<T> {
|
||||
|
||||
/// Takes the value out of the Signal, leaving a Default in its place.
|
||||
#[track_caller]
|
||||
fn take(&self) -> T
|
||||
fn take(&mut self) -> T
|
||||
where
|
||||
T: Default,
|
||||
{
|
||||
@@ -67,7 +67,7 @@ pub trait Writable<T: 'static>: Readable<T> {
|
||||
|
||||
/// Replace the value in the Signal, returning the old value.
|
||||
#[track_caller]
|
||||
fn replace(&self, value: T) -> T {
|
||||
fn replace(&mut self, value: T) -> T {
|
||||
self.with_mut(|v| std::mem::replace(v, value))
|
||||
}
|
||||
}
|
||||
@@ -75,12 +75,12 @@ pub trait Writable<T: 'static>: Readable<T> {
|
||||
/// An extension trait for Writable<Option<T>> that provides some convenience methods.
|
||||
pub trait WritableOptionExt<T: 'static>: Writable<Option<T>> {
|
||||
/// Gets the value out of the Option, or inserts the given value if the Option is empty.
|
||||
fn get_or_insert(&self, default: T) -> Self::Mut<T> {
|
||||
fn get_or_insert(&mut self, default: T) -> Self::Mut<T> {
|
||||
self.get_or_insert_with(|| default)
|
||||
}
|
||||
|
||||
/// Gets the value out of the Option, or inserts the value returned by the given function if the Option is empty.
|
||||
fn get_or_insert_with(&self, default: impl FnOnce() -> T) -> Self::Mut<T> {
|
||||
fn get_or_insert_with(&mut self, default: impl FnOnce() -> T) -> Self::Mut<T> {
|
||||
let borrow = self.read();
|
||||
if borrow.is_none() {
|
||||
drop(borrow);
|
||||
@@ -93,7 +93,7 @@ pub trait WritableOptionExt<T: 'static>: Writable<Option<T>> {
|
||||
|
||||
/// Attempts to write the inner value of the Option.
|
||||
#[track_caller]
|
||||
fn as_mut(&self) -> Option<Self::Mut<T>> {
|
||||
fn as_mut(&mut self) -> Option<Self::Mut<T>> {
|
||||
Self::try_map_mut(self.write(), |v: &mut Option<T>| v.as_mut())
|
||||
}
|
||||
}
|
||||
@@ -169,7 +169,7 @@ pub trait WritableVecExt<T: 'static>: Writable<Vec<T>> {
|
||||
|
||||
/// Try to mutably get an element from the vector.
|
||||
#[track_caller]
|
||||
fn get_mut(&self, index: usize) -> Option<Self::Mut<T>> {
|
||||
fn get_mut(&mut self, index: usize) -> Option<Self::Mut<T>> {
|
||||
Self::try_map_mut(self.write(), |v: &mut Vec<T>| v.get_mut(index))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user