-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Specialize widget::text
helper
#2363
Conversation
19906d4
to
34f799a
Compare
Thank you! This is pretty good. I use a lot of
Did you leave out In case of custom local types is there really any need to implement Isn't this: struct MyStruct;
impl ToString for MyStruct {
fn to_string(&self) -> String {
"MyStruct".into()
}
}
//...
App {
my_struct: MyStruct
}
impl App {
fn view(&self) -> Element<Message> {
text(self.my_struct.to_string())
.into()
}
} Pretty much the same thing as this: struct MyStruct;
impl ToString for MyStruct {
fn to_string(&self) -> String {
"MyStruct".into()
}
}
impl<'a> IntoFragment<'a> for MyStruct {
fn into_fragment(self) -> Fragment<'a> {
Fragment::Owned(self.to_string())
}
}
impl<'a> IntoFragment<'a> for &MyStruct {
fn into_fragment(self) -> Fragment<'a> {
Fragment::Owned(self.to_string())
}
}
//...
App {
my_struct: MyStruct
}
impl App {
fn view(&self) -> Element<Message> {
text(&self.my_struct)
.into()
}
} Both cases will allocate a new This means that for custom local or remote types that implement |
@alex-ds13 Say struct MyStruct {
some_text: String,
}
impl<'a> IntoFragment<'a> for &'a MyStruct {
fn into_fragment(self) -> Content<'a> {
Cow::Borrowed(&self.some_text)
}
}
let my_struct = MyStruct { some_text: String::from("Some text!") };
text(&my_struct) It saves you from typing |
Also, |
@alex-ds13 My last commit showcases a real use case of the pattern I talked about in the |
That's true. Overall I think this is a good change and if anyone needs to use a remote type that they can't implement |
Was there any technical reason for you to alias |
@alex-ds13 This is so you don't need yet an additional import ( |
This PR specializes the arguments of
widget::text
by manually implementing a newtext::IntoFragment
trait for the primitive types. No more extra allocations when callingtext("Hello!")
ortext(some_string)
! 🎉The downside is that the
text
helper cannot be used with all the implementors ofToString
anymore. However, it is always possible to implement thetext::IntoFragment
trait for local types.Furthermore, a new
widget::value
helper has been introduced with the same functionality as the oldtext
helper.cc @alex-ds13