-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
xilem_core: Provide a way to add View
implementations for external types
#402
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not clear why you've added a std feature for this; none of the things behind it actually need it.
I'm also not convinced it's meaningful to have a View
implementation for numbers. It's cute, but it's a bit weird imo.
xilem_core/src/views/orphan.rs
Outdated
impl_orphan_view_for!(String); | ||
#[cfg(feature = "std")] | ||
impl_as_orphan_view_for!(std::borrow::Cow<'static, str>); | ||
// Why does the following not work, but the `Cow` impl does?? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I presume this would be because we already have an impl for Arc<T>
, and the compiler (for some reason) can't reason that str
is not View
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, that makes sense, thanks for the hint.
I guess I wasn't thorough enough and thought, these require
xilem_web actually already had support for numbers as
So at this point I'm leaning towards the more traditional approach with providing an actual |
…ions for external types Co-authored-by: Daniel McNab <36049421+DJMcNab@users.noreply.github.com>
I've removed the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I really preferred the "AsView" style is that I think it's important that these views are only syntax sugar for an existing view, and are not the only way to access certain functionality. That is mostly because of extensibility, but also in terms of allowing people to make the meaning of their code clearer if needed, by migrating from the syntax sugar.
I do agree that for String
specifically, that has issues around needing to clone the string for a short time, which is very unfortunate. But IMO, the answer there is to make non-'static
views be supported.
That being said, this is probably a slightly more idiomatic actual implementation than the "AsView" style, and so I think we should have it this way. This is a really good trick to avoid the marker trait awkwardness the previous solution had (DJMcNab#4)
impl_orphan_view_for!(alloc::string::String); | ||
impl_orphan_view_for!(alloc::borrow::Cow<'static, str>); | ||
|
||
// number impls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still not convinced that these are that meaningful, but I'm not going to block on that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah apart from a little bit macro-expanded boilerplate (which I don't think should contribute too much to the compilation) doesn't hurt?
(In case we can also feature gate this, like the kurbo module I guess, would probably be interesting whether this meaningfully contributes to compilation time...)
I think it's cool to have something like (taken from xilem_web) p(("The answer to life is ", 42))
and it should be cheap as view, since equality checks are trivial.
Yep that's why I originally wanted to have both of these (and the full Thanks for the review and the initial impl in DJMcNab#4 that inspired this. |
Since we're suffering from the orphan rule with the new xilem_core implementation, we cannot use
View
in downstream crates for external types such as aWidgetView
implementation for&'static str
in Xilem.This PR adds an
OrphanView
trait which theContext
of theView
has to implement for the type which has an implementation in xilem_core for this. TheView
impl for these types is basically a proxy forOrphanView
(which in turn has the same method signature asView
), so downstream crates can achieve the same as with theView
.