use std::fmt::Arguments;
use crate::innerlude::*;
#[rustversion::attr(
since(1.78.0),
diagnostic::on_unimplemented(
message = "`Props` is not implemented for `{Self}`",
label = "Props",
note = "Props is a trait that is automatically implemented for all structs that can be used as props for a component",
note = "If you manually created a new properties struct, you may have forgotten to add `#[derive(Props, PartialEq, Clone)]` to your struct",
)
)]
pub trait Properties: Clone + Sized + 'static {
type Builder;
fn builder() -> Self::Builder;
fn memoize(&mut self, other: &Self) -> bool;
fn into_vcomponent<M: 'static>(self, render_fn: impl ComponentFunction<Self, M>) -> VComponent {
let type_name = std::any::type_name_of_val(&render_fn);
VComponent::new(render_fn, self, type_name)
}
}
impl Properties for () {
type Builder = EmptyBuilder;
fn builder() -> Self::Builder {
EmptyBuilder {}
}
fn memoize(&mut self, _other: &Self) -> bool {
true
}
}
pub(crate) struct RootProps<P>(pub P);
impl<P> Clone for RootProps<P>
where
P: Clone,
{
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<P> Properties for RootProps<P>
where
P: Clone + 'static,
{
type Builder = P;
fn builder() -> Self::Builder {
unreachable!("Root props technically are never built")
}
fn memoize(&mut self, _other: &Self) -> bool {
true
}
}
pub struct EmptyBuilder;
impl EmptyBuilder {
pub fn build(self) {}
}
pub fn fc_to_builder<P, M>(_: impl ComponentFunction<P, M>) -> <P as Properties>::Builder
where
P: Properties,
{
P::builder()
}
#[rustversion::attr(
since(1.78.0),
diagnostic::on_unimplemented(
message = "`Component<{Props}>` is not implemented for `{Self}`",
label = "Component",
note = "Components are functions in the form `fn() -> Element`, `fn(props: Properties) -> Element`, or `#[component] fn(partial_eq1: u32, partial_eq2: u32) -> Element`.",
note = "You may have forgotten to add `#[component]` to your function to automatically implement the `ComponentFunction` trait."
)
)]
pub trait ComponentFunction<Props, Marker = ()>: Clone + 'static {
fn fn_ptr(&self) -> usize;
fn rebuild(&self, props: Props) -> Element;
}
impl<F, P> ComponentFunction<P> for F
where
F: Fn(P) -> Element + Clone + 'static,
{
fn rebuild(&self, props: P) -> Element {
subsecond::HotFn::current(self.clone()).call((props,))
}
fn fn_ptr(&self) -> usize {
subsecond::HotFn::current(self.clone()).ptr_address().0 as usize
}
}
pub struct EmptyMarker;
impl<F> ComponentFunction<(), EmptyMarker> for F
where
F: Fn() -> Element + Clone + 'static,
{
fn rebuild(&self, props: ()) -> Element {
subsecond::HotFn::current(self.clone()).call(props)
}
fn fn_ptr(&self) -> usize {
subsecond::HotFn::current(self.clone()).ptr_address().0 as usize
}
}
pub trait SuperInto<O, M = ()> {
fn super_into(self) -> O;
}
impl<T, O, M> SuperInto<O, M> for T
where
O: SuperFrom<T, M>,
{
fn super_into(self) -> O {
O::super_from(self)
}
}
pub trait SuperFrom<T, M = ()> {
fn super_from(_: T) -> Self;
}
impl<T, O> SuperFrom<T, ()> for O
where
O: From<T>,
{
fn super_from(input: T) -> Self {
Self::from(input)
}
}
#[doc(hidden)]
pub struct OptionStringFromMarker;
impl<'a> SuperFrom<&'a str, OptionStringFromMarker> for Option<String> {
fn super_from(input: &'a str) -> Self {
Some(String::from(input))
}
}
#[doc(hidden)]
pub struct OptionArgumentsFromMarker;
impl<'a> SuperFrom<Arguments<'a>, OptionArgumentsFromMarker> for Option<String> {
fn super_from(input: Arguments<'a>) -> Self {
Some(input.to_string())
}
}
#[doc(hidden)]
pub struct OptionCallbackMarker<T>(std::marker::PhantomData<T>);
impl<
Function: FnMut(Args) -> Spawn + 'static,
Args: 'static,
Spawn: SpawnIfAsync<Marker, Ret> + 'static,
Ret: 'static,
Marker,
> SuperFrom<Function, OptionCallbackMarker<Marker>> for Option<Callback<Args, Ret>>
{
fn super_from(input: Function) -> Self {
Some(Callback::new(input))
}
}
#[test]
#[allow(unused)]
fn optional_callback_compiles() {
fn compiles() {
let callback: Callback<i32, i32> = (|num| num * num).super_into();
let callback: Callback<i32, ()> = (|num| async move { println!("{num}") }).super_into();
let optional: Option<Callback<i32, i32>> = (|num| num * num).super_into();
let optional: Option<Callback<i32, ()>> =
(|num| async move { println!("{num}") }).super_into();
}
}
#[test]
#[allow(unused)]
fn from_props_compiles() {
let option: i32 = 0i32.super_into();
let option: i32 = 0.super_into(); let option: i128 = 0.super_into();
let option: &'static str = "hello world".super_into();
let option: i64 = 0i32.super_into();
let option: String = "hello world".super_into();
let option: Option<i32> = 0i32.super_into();
let option: Option<i32> = 0.super_into();
let option: Option<i128> = 0.super_into();
fn takes_option_string<M>(_: impl SuperInto<Option<String>, M>) {}
takes_option_string("hello world");
takes_option_string("hello world".to_string());
}