fix Option<String> in props

This commit is contained in:
Evan Almloff committed 2024-01-24 16:40:14 -06:00
1 parent 30ef29d195
commit c3555a7ec0
6 files changed
+131 -15

No files matched your search

+4 -3
View File
@@ -91,8 +91,9 @@ pub mod prelude {
remove_future, schedule_update, schedule_update_any, spawn, spawn_forever, suspend,
try_consume_context, use_drop, use_error_boundary, use_hook, use_hook_with_cleanup,
AnyValue, Attribute, Component, ComponentFunction, Element, ErrorBoundary, Event,
EventHandler, Fragment, HasAttributes, IntoAttributeValue, IntoDynNode, Properties,
Runtime, RuntimeGuard, ScopeId, ScopeState, Task, Template, TemplateAttribute,
TemplateNode, Throw, VNode, VNodeInner, VirtualDom,
EventHandler, Fragment, HasAttributes, IntoAttributeValue, IntoDynNode,
OptionStringFromMarker, Properties, Runtime, RuntimeGuard, ScopeId, ScopeState, SuperFrom,
SuperInto, Task, Template, TemplateAttribute, TemplateNode, Throw, VNode, VNodeInner,
VirtualDom,
};
}
+72 -1
View File
@@ -1,4 +1,4 @@
use std::any::TypeId;
use std::{any::TypeId, fmt::Arguments};
use crate::innerlude::*;
@@ -118,3 +118,74 @@ impl<F: Fn() -> Element + Clone + 'static> ComponentFunction<(), EmptyMarker> fo
self()
}
}
/// A enhanced version of the `Into` trait that allows with more flexibility.
pub trait SuperInto<O, M = ()> {
/// Convert from a type to another type.
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)
}
}
/// A enhanced version of the `From` trait that allows with more flexibility.
pub trait SuperFrom<T, M = ()> {
/// Convert from a type to another type.
fn super_from(_: T) -> Self;
}
// first implement for all types that are that implement the From trait
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())
}
}
#[test]
#[allow(unused)]
fn from_props_compiles() {
// T -> T works
let option: i32 = 0i32.super_into();
let option: i32 = 0.super_into(); // Note we don't need type hints on all inputs
let option: i128 = 0.super_into();
let option: &'static str = "hello world".super_into();
// // T -> From<T> works
let option: i64 = 0i32.super_into();
let option: String = "hello world".super_into();
// T -> Option works
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());
}