mirror of
https://github.com/leptos-rs/leptos
synced 2024-11-10 06:44:17 +00:00
add trait impls and encodings for SharedValue
This commit is contained in:
parent
bd1601e892
commit
e4d25608df
1 changed files with 130 additions and 3 deletions
|
@ -1,6 +1,15 @@
|
|||
use crate::serializers::{SerdeJson, SerializableData, Serializer};
|
||||
#[cfg(feature = "miniserde")]
|
||||
use crate::serializers::Miniserde;
|
||||
#[cfg(feature = "rkyv")]
|
||||
use crate::serializers::Rkyv;
|
||||
#[cfg(feature = "serde-lite")]
|
||||
use crate::serializers::SerdeLite;
|
||||
#[cfg(feature = "serde-wasm-bindgen")]
|
||||
use crate::serializers::SerdeWasmBindgen;
|
||||
use crate::serializers::{SerdeJson, SerializableData, Serializer, Str};
|
||||
use std::{
|
||||
fmt::Debug,
|
||||
fmt::{Debug, Display},
|
||||
hash::Hash,
|
||||
marker::PhantomData,
|
||||
ops::{Deref, DerefMut},
|
||||
};
|
||||
|
@ -11,6 +20,7 @@ use std::{
|
|||
/// If this constructed on the server, it serializes its value into the shared context. If it is
|
||||
/// constructed on the client during hydration, it reads its value from the shared context. If
|
||||
/// it it constructed on the client at any other time, it simply runs on the client.
|
||||
#[derive(Debug)]
|
||||
pub struct SharedValue<T, Ser = SerdeJson> {
|
||||
value: T,
|
||||
ser: PhantomData<Ser>,
|
||||
|
@ -22,13 +32,83 @@ impl<T, Ser> SharedValue<T, Ser> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> SharedValue<T, SerdeJson>
|
||||
where
|
||||
T: Debug + SerializableData<SerdeJson>,
|
||||
T::SerErr: Debug,
|
||||
T::DeErr: Debug,
|
||||
{
|
||||
pub fn new(initial: impl FnOnce() -> T) -> Self {
|
||||
SharedValue::new_with_encoding(initial)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> SharedValue<T, Str>
|
||||
where
|
||||
T: Debug + SerializableData<Str>,
|
||||
T::SerErr: Debug,
|
||||
T::DeErr: Debug,
|
||||
{
|
||||
pub fn new_str(initial: impl FnOnce() -> T) -> Self {
|
||||
SharedValue::new_with_encoding(initial)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-lite")]
|
||||
impl<T> SharedValue<T, SerdeLite>
|
||||
where
|
||||
T: Debug + SerializableData<SerdeLite>,
|
||||
T::SerErr: Debug,
|
||||
T::DeErr: Debug,
|
||||
{
|
||||
pub fn new(initial: impl FnOnce() -> T) -> Self {
|
||||
SharedValue::new_with_encoding(initial)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-wasm-bindgen")]
|
||||
impl<T> SharedValue<T, SerdeWasmBindgen>
|
||||
where
|
||||
T: Debug + SerializableData<SerdeWasmBindgen>,
|
||||
T::SerErr: Debug,
|
||||
T::DeErr: Debug,
|
||||
{
|
||||
pub fn new(initial: impl FnOnce() -> T) -> Self {
|
||||
SharedValue::new_with_encoding(initial)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "miniserde")]
|
||||
impl<T> SharedValue<T, Miniserde>
|
||||
where
|
||||
T: Debug + SerializableData<Miniserde>,
|
||||
T::SerErr: Debug,
|
||||
T::DeErr: Debug,
|
||||
{
|
||||
pub fn new(initial: impl FnOnce() -> T) -> Self {
|
||||
SharedValue::new_with_encoding(initial)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "rkyv")]
|
||||
impl<T> SharedValue<T, Rkyv>
|
||||
where
|
||||
T: Debug + SerializableData<Rkyv>,
|
||||
T::SerErr: Debug,
|
||||
T::DeErr: Debug,
|
||||
{
|
||||
pub fn new(initial: impl FnOnce() -> T) -> Self {
|
||||
SharedValue::new_with_encoding(initial)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, Ser> SharedValue<T, Ser>
|
||||
where
|
||||
T: Debug + SerializableData<Ser>,
|
||||
T::SerErr: Debug,
|
||||
Ser: Serializer,
|
||||
{
|
||||
pub fn new(initial: impl FnOnce() -> T) -> Self {
|
||||
pub fn new_with_encoding(initial: impl FnOnce() -> T) -> Self {
|
||||
let value: T;
|
||||
#[cfg(feature = "hydration")]
|
||||
{
|
||||
|
@ -93,3 +173,50 @@ impl<T, Ser> DerefMut for SharedValue<T, Ser> {
|
|||
&mut self.value
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, Ser> PartialEq for SharedValue<T, Ser>
|
||||
where
|
||||
T: PartialEq,
|
||||
{
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.value == other.value
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, Ser> Eq for SharedValue<T, Ser> where T: Eq {}
|
||||
|
||||
impl<T, Ser> Display for SharedValue<T, Ser>
|
||||
where
|
||||
T: Display,
|
||||
{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, Ser> Hash for SharedValue<T, Ser>
|
||||
where
|
||||
T: Hash,
|
||||
{
|
||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||
self.value.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, Ser> PartialOrd for SharedValue<T, Ser>
|
||||
where
|
||||
T: PartialOrd,
|
||||
{
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
self.value.partial_cmp(&other.value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, Ser> Ord for SharedValue<T, Ser>
|
||||
where
|
||||
T: Ord,
|
||||
{
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
self.value.cmp(&other.value)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue