implement readable and callable for all structs in the hooks crate
This commit is contained in:
1 parent
98158bdc6f
commit
ffc49530f6
13 files changed
+167
-491
No files matched your search
@@ -5,7 +5,6 @@ use generational_box::UnsyncStorage;
|
||||
use std::any::Any;
|
||||
use std::any::TypeId;
|
||||
use std::cell::RefCell;
|
||||
use std::mem::MaybeUninit;
|
||||
use std::ops::Deref;
|
||||
|
||||
use dioxus_core::prelude::*;
|
||||
@@ -268,31 +267,6 @@ impl<T: Copy, S: Storage<T>> Deref for CopyValue<T, S> {
|
||||
type Target = dyn Fn() -> T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
// https://github.com/dtolnay/case-studies/tree/master/callable-types
|
||||
|
||||
// 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() });
|
||||
|
||||
// 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);
|
||||
assert_eq!(size_of_closure, std::mem::size_of::<Self>());
|
||||
|
||||
// Then cast the lifetime of the closure to the lifetime of &self.
|
||||
fn cast_lifetime<'a, T>(_a: &T, b: &'a T) -> &'a T {
|
||||
b
|
||||
}
|
||||
let reference_to_closure = cast_lifetime(
|
||||
{
|
||||
// The real closure that we will never use.
|
||||
&uninit_closure
|
||||
},
|
||||
// We transmute self into a reference to the closure. This is safe because we know that the closure has the same memory layout as Self so &Closure == &Self.
|
||||
unsafe { std::mem::transmute(self) },
|
||||
);
|
||||
|
||||
// Cast the closure to a trait object.
|
||||
reference_to_closure as &Self::Target
|
||||
Readable::deref_impl(self)
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::{read::Readable, Memo, ReadableRef};
|
||||
use dioxus_core::prelude::{IntoAttributeValue, ScopeId};
|
||||
use dioxus_core::prelude::ScopeId;
|
||||
use generational_box::UnsyncStorage;
|
||||
use std::{mem::MaybeUninit, ops::Deref};
|
||||
use std::ops::Deref;
|
||||
|
||||
use crate::Signal;
|
||||
|
||||
@@ -68,15 +68,6 @@ impl<T: PartialEq + 'static> Readable for GlobalMemo<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: PartialEq + 'static> IntoAttributeValue for GlobalMemo<T>
|
||||
where
|
||||
T: Clone + IntoAttributeValue,
|
||||
{
|
||||
fn into_value(self) -> dioxus_core::AttributeValue {
|
||||
self.memo().into_value()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: PartialEq + 'static> PartialEq for GlobalMemo<T> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
std::ptr::eq(self, other)
|
||||
@@ -90,31 +81,6 @@ impl<T: PartialEq + Clone + 'static> Deref for GlobalMemo<T> {
|
||||
type Target = dyn Fn() -> T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
// https://github.com/dtolnay/case-studies/tree/master/callable-types
|
||||
|
||||
// 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();
|
||||
|
||||
// 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);
|
||||
assert_eq!(size_of_closure, std::mem::size_of::<Self>());
|
||||
|
||||
// Then cast the lifetime of the closure to the lifetime of &self.
|
||||
fn cast_lifetime<'a, T>(_a: &T, b: &'a T) -> &'a T {
|
||||
b
|
||||
}
|
||||
let reference_to_closure = cast_lifetime(
|
||||
{
|
||||
// The real closure that we will never use.
|
||||
&uninit_closure
|
||||
},
|
||||
// We transmute self into a reference to the closure. This is safe because we know that the closure has the same memory layout as Self so &Closure == &self.
|
||||
unsafe { std::mem::transmute(self) },
|
||||
);
|
||||
|
||||
// Cast the closure to a trait object.
|
||||
reference_to_closure as &Self::Target
|
||||
Readable::deref_impl(self)
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
use crate::write::Writable;
|
||||
use crate::{read::Readable, ReadableRef};
|
||||
use crate::{WritableRef, Write};
|
||||
use dioxus_core::prelude::{IntoAttributeValue, ScopeId};
|
||||
use dioxus_core::prelude::ScopeId;
|
||||
use generational_box::UnsyncStorage;
|
||||
use std::{mem::MaybeUninit, ops::Deref};
|
||||
use std::ops::Deref;
|
||||
|
||||
use super::get_global_context;
|
||||
use crate::Signal;
|
||||
@@ -118,15 +118,6 @@ impl<T: 'static> Writable for GlobalSignal<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> IntoAttributeValue for GlobalSignal<T>
|
||||
where
|
||||
T: Clone + IntoAttributeValue,
|
||||
{
|
||||
fn into_value(self) -> dioxus_core::AttributeValue {
|
||||
self.signal().into_value()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> PartialEq for GlobalSignal<T> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
std::ptr::eq(self, other)
|
||||
@@ -140,34 +131,6 @@ impl<T: Clone + 'static> Deref for GlobalSignal<T> {
|
||||
type Target = dyn Fn() -> T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
// https://github.com/dtolnay/case-studies/tree/master/callable-types
|
||||
|
||||
// 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 || {
|
||||
<GlobalSignal<T> as Readable>::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);
|
||||
assert_eq!(size_of_closure, std::mem::size_of::<Self>());
|
||||
|
||||
// Then cast the lifetime of the closure to the lifetime of &self.
|
||||
fn cast_lifetime<'a, T>(_a: &T, b: &'a T) -> &'a T {
|
||||
b
|
||||
}
|
||||
let reference_to_closure = cast_lifetime(
|
||||
{
|
||||
// The real closure that we will never use.
|
||||
&uninit_closure
|
||||
},
|
||||
// We transmute self into a reference to the closure. This is safe because we know that the closure has the same memory layout as Self so &Closure == &self.
|
||||
unsafe { std::mem::transmute(self) },
|
||||
);
|
||||
|
||||
// Cast the closure to a trait object.
|
||||
reference_to_closure as &Self::Target
|
||||
Readable::deref_impl(self)
|
||||
}
|
||||
}
|
||||
@@ -204,6 +204,15 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> IntoDynNode for Memo<T>
|
||||
where
|
||||
T: Clone + IntoDynNode + PartialEq,
|
||||
{
|
||||
fn into_dyn_node(self) -> dioxus_core::DynamicNode {
|
||||
self().into_dyn_node()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> PartialEq for Memo<T> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.inner == other.inner
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::{read::Readable, ReadableRef, Signal, SignalData};
|
||||
use dioxus_core::IntoDynNode;
|
||||
use std::ops::Deref;
|
||||
|
||||
use dioxus_core::{prelude::IntoAttributeValue, ScopeId};
|
||||
@@ -105,6 +106,15 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> IntoDynNode for ReadOnlySignal<T>
|
||||
where
|
||||
T: Clone + IntoDynNode,
|
||||
{
|
||||
fn into_dyn_node(self) -> dioxus_core::DynamicNode {
|
||||
self().into_dyn_node()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static, S: Storage<SignalData<T>>> PartialEq for ReadOnlySignal<T, S> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.inner == other.inner
|
||||
|
||||
@@ -3,6 +3,7 @@ use crate::{
|
||||
ReadableRef,
|
||||
};
|
||||
use crate::{Memo, WritableRef};
|
||||
use dioxus_core::IntoDynNode;
|
||||
use dioxus_core::{prelude::IntoAttributeValue, ScopeId};
|
||||
use generational_box::{AnyStorage, Storage, SyncStorage, UnsyncStorage};
|
||||
use std::{
|
||||
@@ -245,6 +246,15 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> IntoDynNode for Signal<T>
|
||||
where
|
||||
T: Clone + IntoDynNode,
|
||||
{
|
||||
fn into_dyn_node(self) -> dioxus_core::DynamicNode {
|
||||
self().into_dyn_node()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static, S: Storage<SignalData<T>>> PartialEq for Signal<T, S> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.inner == other.inner
|
||||
|
||||
Reference in new issue
Block a user