Skip to content

Commit

Permalink
Bring back &str assignment to optional properties
Browse files Browse the repository at this point in the history
This drops the "optional" conversion concept in favor of directly
converting to `Option<T>`.

fixes #1888
  • Loading branch information
ctron committed Jun 7, 2021
1 parent cf4ed4b commit 8cd8341
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 61 deletions.
12 changes: 6 additions & 6 deletions packages/yew-macro/tests/html_macro/html-component-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ error[E0277]: the trait bound `{integer}: IntoPropValue<String>` is not satisfie
|
= help: the following implementations were found:
<&'static str as IntoPropValue<Cow<'static, str>>>
<&'static str as IntoPropValue<Option<Cow<'static, str>>>>
<&'static str as IntoPropValue<Option<String>>>
<&'static str as IntoPropValue<String>>
<&T as IntoPropValue<Option<T>>>
<&T as IntoPropValue<T>>
and 4 others
and 11 others

error[E0277]: the trait bound `{integer}: IntoPropValue<String>` is not satisfied
--> $DIR/html-component-fail.rs:100:26
Expand All @@ -232,10 +232,10 @@ error[E0277]: the trait bound `{integer}: IntoPropValue<String>` is not satisfie
|
= help: the following implementations were found:
<&'static str as IntoPropValue<Cow<'static, str>>>
<&'static str as IntoPropValue<Option<Cow<'static, str>>>>
<&'static str as IntoPropValue<Option<String>>>
<&'static str as IntoPropValue<String>>
<&T as IntoPropValue<Option<T>>>
<&T as IntoPropValue<T>>
and 4 others
and 11 others

error[E0308]: mismatched types
--> $DIR/html-component-fail.rs:101:30
Expand Down
48 changes: 23 additions & 25 deletions packages/yew-macro/tests/html_macro/html-element-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,12 @@ error[E0277]: the trait bound `(): IntoPropValue<Option<Cow<'static, str>>>` is
|
43 | html! { <input type=() /> };
| ^^ the trait `IntoPropValue<Option<Cow<'static, str>>>` is not implemented for `()`
|
= note: required because of the requirements on the impl of `IntoOptPropValue<Cow<'static, str>>` for `()`

error[E0277]: the trait bound `(): IntoPropValue<Option<Cow<'static, str>>>` is not satisfied
--> $DIR/html-element-fail.rs:44:26
|
44 | html! { <input value=() /> };
| ^^ the trait `IntoPropValue<Option<Cow<'static, str>>>` is not implemented for `()`
|
= note: required because of the requirements on the impl of `IntoOptPropValue<Cow<'static, str>>` for `()`

error[E0277]: the trait bound `(): IntoPropValue<Option<Cow<'static, str>>>` is not satisfied
--> $DIR/html-element-fail.rs:45:21
Expand All @@ -204,10 +200,8 @@ error[E0277]: the trait bound `(): IntoPropValue<Option<Cow<'static, str>>>` is
|
::: $WORKSPACE/packages/yew/src/virtual_dom/mod.rs:69:47
|
69 | pub fn new(key: &'static str, value: impl IntoOptPropValue<AttrValue>) -> Self {
| --------------------------- required by this bound in `PositionalAttr::new`
|
= note: required because of the requirements on the impl of `IntoOptPropValue<Cow<'static, str>>` for `()`
69 | pub fn new(key: &'static str, value: impl IntoPropValue<Option<AttrValue>>) -> Self {
| -------------------------------- required by this bound in `PositionalAttr::new`

error[E0277]: the trait bound `NotToString: IntoPropValue<Option<Cow<'static, str>>>` is not satisfied
--> $DIR/html-element-fail.rs:46:27
Expand All @@ -217,10 +211,8 @@ error[E0277]: the trait bound `NotToString: IntoPropValue<Option<Cow<'static, st
|
::: $WORKSPACE/packages/yew/src/virtual_dom/mod.rs:69:47
|
69 | pub fn new(key: &'static str, value: impl IntoOptPropValue<AttrValue>) -> Self {
| --------------------------- required by this bound in `PositionalAttr::new`
|
= note: required because of the requirements on the impl of `IntoOptPropValue<Cow<'static, str>>` for `NotToString`
69 | pub fn new(key: &'static str, value: impl IntoPropValue<Option<AttrValue>>) -> Self {
| -------------------------------- required by this bound in `PositionalAttr::new`

error[E0277]: the trait bound `Option<NotToString>: IntoPropValue<Option<Cow<'static, str>>>` is not satisfied
--> $DIR/html-element-fail.rs:47:22
Expand All @@ -230,10 +222,13 @@ error[E0277]: the trait bound `Option<NotToString>: IntoPropValue<Option<Cow<'st
|
::: $WORKSPACE/packages/yew/src/virtual_dom/mod.rs:69:47
|
69 | pub fn new(key: &'static str, value: impl IntoOptPropValue<AttrValue>) -> Self {
| --------------------------- required by this bound in `PositionalAttr::new`
69 | pub fn new(key: &'static str, value: impl IntoPropValue<Option<AttrValue>>) -> Self {
| -------------------------------- required by this bound in `PositionalAttr::new`
|
= note: required because of the requirements on the impl of `IntoOptPropValue<Cow<'static, str>>` for `Option<NotToString>`
= help: the following implementations were found:
<Option<&'static str> as IntoPropValue<Option<Cow<'static, str>>>>
<Option<&'static str> as IntoPropValue<Option<String>>>
<Option<String> as IntoPropValue<Option<Cow<'static, str>>>>

error[E0277]: the trait bound `Option<{integer}>: IntoPropValue<Option<Cow<'static, str>>>` is not satisfied
--> $DIR/html-element-fail.rs:48:21
Expand All @@ -243,10 +238,13 @@ error[E0277]: the trait bound `Option<{integer}>: IntoPropValue<Option<Cow<'stat
|
::: $WORKSPACE/packages/yew/src/virtual_dom/mod.rs:69:47
|
69 | pub fn new(key: &'static str, value: impl IntoOptPropValue<AttrValue>) -> Self {
| --------------------------- required by this bound in `PositionalAttr::new`
69 | pub fn new(key: &'static str, value: impl IntoPropValue<Option<AttrValue>>) -> Self {
| -------------------------------- required by this bound in `PositionalAttr::new`
|
= note: required because of the requirements on the impl of `IntoOptPropValue<Cow<'static, str>>` for `Option<{integer}>`
= help: the following implementations were found:
<Option<&'static str> as IntoPropValue<Option<Cow<'static, str>>>>
<Option<&'static str> as IntoPropValue<Option<String>>>
<Option<String> as IntoPropValue<Option<Cow<'static, str>>>>

error[E0277]: the trait bound `{integer}: IntoPropValue<Option<yew::Callback<MouseEvent>>>` is not satisfied
--> $DIR/html-element-fail.rs:51:28
Expand All @@ -267,11 +265,10 @@ error[E0277]: the trait bound `{integer}: IntoPropValue<Option<yew::Callback<Mou
|
= help: the following implementations were found:
<&'static str as IntoPropValue<Cow<'static, str>>>
<&'static str as IntoPropValue<Option<Cow<'static, str>>>>
<&'static str as IntoPropValue<Option<String>>>
<&'static str as IntoPropValue<String>>
<&T as IntoPropValue<Option<T>>>
<&T as IntoPropValue<T>>
and 4 others
= note: required because of the requirements on the impl of `IntoOptPropValue<yew::Callback<MouseEvent>>` for `{integer}`
and 11 others

error[E0277]: the trait bound `yew::Callback<String>: IntoPropValue<Option<yew::Callback<MouseEvent>>>` is not satisfied
--> $DIR/html-element-fail.rs:52:28
Expand All @@ -289,8 +286,6 @@ error[E0277]: the trait bound `yew::Callback<String>: IntoPropValue<Option<yew::
99 | | ontransitionstart(name: "transitionstart", event: TransitionEvent) -> web_sys::TransitionEvent => |_, event| { event }
100 | | }
| |_- required by this bound in `yew::html::onclick::Wrapper::__macro_new`
|
= note: required because of the requirements on the impl of `IntoOptPropValue<yew::Callback<MouseEvent>>` for `yew::Callback<String>`

error[E0277]: the trait bound `Option<{integer}>: IntoPropValue<Option<yew::Callback<FocusEvent>>>` is not satisfied
--> $DIR/html-element-fail.rs:53:28
Expand All @@ -309,7 +304,10 @@ error[E0277]: the trait bound `Option<{integer}>: IntoPropValue<Option<yew::Call
100 | | }
| |_- required by this bound in `yew::html::onfocus::Wrapper::__macro_new`
|
= note: required because of the requirements on the impl of `IntoOptPropValue<yew::Callback<FocusEvent>>` for `Option<{integer}>`
= help: the following implementations were found:
<Option<&'static str> as IntoPropValue<Option<Cow<'static, str>>>>
<Option<&'static str> as IntoPropValue<Option<String>>>
<Option<String> as IntoPropValue<Option<Cow<'static, str>>>>

error[E0308]: mismatched types
--> $DIR/html-element-fail.rs:56:24
Expand Down
6 changes: 3 additions & 3 deletions packages/yew/src/html/classes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{IntoOptPropValue, IntoPropValue};
use super::IntoPropValue;
use crate::virtual_dom::AttrValue;
use indexmap::IndexSet;
use std::{
Expand Down Expand Up @@ -74,8 +74,8 @@ impl IntoPropValue<AttrValue> for Classes {
}
}

impl IntoOptPropValue<AttrValue> for Classes {
fn into_opt_prop_value(self) -> Option<AttrValue> {
impl IntoPropValue<Option<AttrValue>> for Classes {
fn into_prop_value(self) -> Option<AttrValue> {
if self.is_empty() {
None
} else {
Expand Down
30 changes: 14 additions & 16 deletions packages/yew/src/html/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ macro_rules! impl_into_prop {
}
}
// implement V -> Option<T>
impl IntoOptPropValue<$to_ty> for $from_ty {
fn into_opt_prop_value(self) -> Option<$to_ty> {
impl IntoPropValue<Option<$to_ty>> for $from_ty {
fn into_prop_value(self) -> Option<$to_ty> {
let $value = self;
Some({ $conversion })
}
}
// implement Option<V> -> Option<T>
impl IntoOptPropValue<$to_ty> for Option<$from_ty> {
fn into_opt_prop_value(self) -> Option<$to_ty> {
impl IntoPropValue<Option<$to_ty>> for Option<$from_ty> {
fn into_prop_value(self) -> Option<$to_ty> {
self.map(IntoPropValue::into_prop_value)
}
}
Expand All @@ -79,17 +79,15 @@ impl_into_prop!(|value: &'static str| -> String { value.to_owned() });
impl_into_prop!(|value: &'static str| -> Cow<'static, str> { Cow::Borrowed(value) });
impl_into_prop!(|value: String| -> Cow<'static, str> { Cow::Owned(value) });

/// A trait similar to `Into<Option<T>>` which allows conversion to an optional value of a
/// `Properties` struct.
pub trait IntoOptPropValue<T> {
/// Convert `self` to an optional value of a `Properties` struct.
fn into_opt_prop_value(self) -> Option<T>;
}
impl<T, V> IntoOptPropValue<V> for T
where
T: IntoPropValue<Option<V>>,
{
fn into_opt_prop_value(self) -> Option<V> {
self.into_prop_value()
#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_str() {
let _: String = "foo".into_prop_value();
let _: Option<String> = "foo".into_prop_value();
let _: Cow<'static, str> = "foo".into_prop_value();
let _: Option<Cow<'static, str>> = "foo".into_prop_value();
}
}
6 changes: 3 additions & 3 deletions packages/yew/src/html/listener/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ macro_rules! impl_action {
use cfg_match::cfg_match;
use crate::callback::Callback;
#[allow(unused_imports)]
use crate::html::{listener::*, IntoOptPropValue};
use crate::html::{listener::*, IntoPropValue};
use crate::virtual_dom::Listener;
use std::rc::Rc;
cfg_if! {
Expand All @@ -38,8 +38,8 @@ macro_rules! impl_action {
}

#[doc(hidden)]
pub fn __macro_new(callback: impl IntoOptPropValue<Callback<Event>>) -> Option<Rc<dyn Listener>> {
let callback = callback.into_opt_prop_value()?;
pub fn __macro_new(callback: impl IntoPropValue<Option<Callback<Event>>>) -> Option<Rc<dyn Listener>> {
let callback = callback.into_prop_value()?;
Some(Rc::new(Self::new(callback)))
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/yew/src/virtual_dom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mod vtag;
#[doc(hidden)]
pub mod vtext;

use crate::html::{AnyScope, IntoOptPropValue, NodeRef};
use crate::html::{AnyScope, IntoPropValue, NodeRef};
use cfg_if::cfg_if;
use indexmap::IndexMap;
use std::{borrow::Cow, collections::HashMap, fmt, hint::unreachable_unchecked, iter, mem, rc::Rc};
Expand Down Expand Up @@ -66,8 +66,8 @@ pub type AttrValue = Cow<'static, str>;
pub struct PositionalAttr(pub &'static str, pub Option<AttrValue>);
impl PositionalAttr {
/// Create a positional attribute
pub fn new(key: &'static str, value: impl IntoOptPropValue<AttrValue>) -> Self {
Self(key, value.into_opt_prop_value())
pub fn new(key: &'static str, value: impl IntoPropValue<Option<AttrValue>>) -> Self {
Self(key, value.into_prop_value())
}

/// Create a boolean attribute.
Expand Down
10 changes: 5 additions & 5 deletions packages/yew/src/virtual_dom/vtag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use super::{
AttrValue, Attributes, Key, Listener, Listeners, Patch, PositionalAttr, VDiff, VList, VNode,
};
use crate::html::{AnyScope, IntoOptPropValue, IntoPropValue, NodeRef};
use crate::html::{AnyScope, IntoPropValue, NodeRef};
use crate::utils::document;
use cfg_if::cfg_if;
use cfg_match::cfg_match;
Expand Down Expand Up @@ -152,15 +152,15 @@ impl VTag {

/// Sets `value` for an
/// [InputElement](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input).
pub fn set_value(&mut self, value: impl IntoOptPropValue<AttrValue>) {
self.value = value.into_opt_prop_value();
pub fn set_value(&mut self, value: impl IntoPropValue<Option<AttrValue>>) {
self.value = value.into_prop_value();
}

/// Sets `kind` property of an
/// [InputElement](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input).
/// Same as set `type` attribute.
pub fn set_kind(&mut self, value: impl IntoOptPropValue<AttrValue>) {
self.kind = value.into_opt_prop_value();
pub fn set_kind(&mut self, value: impl IntoPropValue<Option<AttrValue>>) {
self.kind = value.into_prop_value();
}

#[doc(hidden)]
Expand Down

0 comments on commit 8cd8341

Please sign in to comment.