From 1d83e59e8ab51d403baee0daa878644c6a37be3f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 1 Apr 2024 21:36:08 +0200 Subject: Specialize `widget::text` helper with custom `IntoContent` trait --- core/src/widget/text.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 3 deletions(-) (limited to 'core/src/widget') diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index 12f6956a..e05eb0d9 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -21,7 +21,7 @@ where Theme: Catalog, Renderer: text::Renderer, { - content: Cow<'a, str>, + content: Content<'a>, size: Option, line_height: LineHeight, width: Length, @@ -39,9 +39,9 @@ where Renderer: text::Renderer, { /// Create a new fragment of [`Text`] with the given contents. - pub fn new(content: impl Into>) -> Self { + pub fn new(content: impl IntoContent<'a>) -> Self { Text { - content: content.into(), + content: content.into_content(), size: None, line_height: LineHeight::default(), font: None, @@ -366,3 +366,67 @@ impl Catalog for Theme { class(self) } } + +/// The content of a [`Text`] widget. +/// +/// This is just an alias to a string that may be either +/// borrowed or owned. +pub type Content<'a> = Cow<'a, str>; + +/// A trait for converting a value to some text [`Content`]. +pub trait IntoContent<'a> { + /// Converts the value to some text [`Content`]. + fn into_content(self) -> Content<'a>; +} + +impl<'a> IntoContent<'a> for &'a str { + fn into_content(self) -> Content<'a> { + Content::Borrowed(self) + } +} + +impl<'a> IntoContent<'a> for &'a String { + fn into_content(self) -> Content<'a> { + Content::Borrowed(self.as_str()) + } +} + +impl<'a> IntoContent<'a> for String { + fn into_content(self) -> Content<'a> { + Content::Owned(self) + } +} + +macro_rules! into_content { + ($type:ty) => { + impl<'a> IntoContent<'a> for $type { + fn into_content(self) -> Content<'a> { + Content::Owned(self.to_string()) + } + } + + impl<'a> IntoContent<'a> for &$type { + fn into_content(self) -> Content<'a> { + Content::Owned(self.to_string()) + } + } + }; +} + +into_content!(char); +into_content!(bool); + +into_content!(u8); +into_content!(u16); +into_content!(u32); +into_content!(u64); +into_content!(u128); + +into_content!(i8); +into_content!(i16); +into_content!(i32); +into_content!(i64); +into_content!(i128); + +into_content!(f32); +into_content!(f64); -- cgit From 34f799aa3dbb58c49708c1f1d9ee436922db636d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 1 Apr 2024 21:47:55 +0200 Subject: Rename `text::IntoContent` to `IntoFragment` --- core/src/widget/text.rs | 80 ++++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 40 deletions(-) (limited to 'core/src/widget') diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index e05eb0d9..123c6a69 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -21,7 +21,7 @@ where Theme: Catalog, Renderer: text::Renderer, { - content: Content<'a>, + fragment: Fragment<'a>, size: Option, line_height: LineHeight, width: Length, @@ -39,9 +39,9 @@ where Renderer: text::Renderer, { /// Create a new fragment of [`Text`] with the given contents. - pub fn new(content: impl IntoContent<'a>) -> Self { + pub fn new(fragment: impl IntoFragment<'a>) -> Self { Text { - content: content.into_content(), + fragment: fragment.into_fragment(), size: None, line_height: LineHeight::default(), font: None, @@ -184,7 +184,7 @@ where limits, self.width, self.height, - &self.content, + &self.fragment, self.line_height, self.size, self.font, @@ -367,66 +367,66 @@ impl Catalog for Theme { } } -/// The content of a [`Text`] widget. +/// A fragment of [`Text`]. /// /// This is just an alias to a string that may be either /// borrowed or owned. -pub type Content<'a> = Cow<'a, str>; +pub type Fragment<'a> = Cow<'a, str>; -/// A trait for converting a value to some text [`Content`]. -pub trait IntoContent<'a> { - /// Converts the value to some text [`Content`]. - fn into_content(self) -> Content<'a>; +/// A trait for converting a value to some text [`Fragment`]. +pub trait IntoFragment<'a> { + /// Converts the value to some text [`Fragment`]. + fn into_fragment(self) -> Fragment<'a>; } -impl<'a> IntoContent<'a> for &'a str { - fn into_content(self) -> Content<'a> { - Content::Borrowed(self) +impl<'a> IntoFragment<'a> for &'a str { + fn into_fragment(self) -> Fragment<'a> { + Fragment::Borrowed(self) } } -impl<'a> IntoContent<'a> for &'a String { - fn into_content(self) -> Content<'a> { - Content::Borrowed(self.as_str()) +impl<'a> IntoFragment<'a> for &'a String { + fn into_fragment(self) -> Fragment<'a> { + Fragment::Borrowed(self.as_str()) } } -impl<'a> IntoContent<'a> for String { - fn into_content(self) -> Content<'a> { - Content::Owned(self) +impl<'a> IntoFragment<'a> for String { + fn into_fragment(self) -> Fragment<'a> { + Fragment::Owned(self) } } -macro_rules! into_content { +macro_rules! into_fragment { ($type:ty) => { - impl<'a> IntoContent<'a> for $type { - fn into_content(self) -> Content<'a> { - Content::Owned(self.to_string()) + impl<'a> IntoFragment<'a> for $type { + fn into_fragment(self) -> Fragment<'a> { + Fragment::Owned(self.to_string()) } } - impl<'a> IntoContent<'a> for &$type { - fn into_content(self) -> Content<'a> { - Content::Owned(self.to_string()) + impl<'a> IntoFragment<'a> for &$type { + fn into_fragment(self) -> Fragment<'a> { + Fragment::Owned(self.to_string()) } } }; } -into_content!(char); -into_content!(bool); +into_fragment!(char); +into_fragment!(bool); -into_content!(u8); -into_content!(u16); -into_content!(u32); -into_content!(u64); -into_content!(u128); +into_fragment!(u8); +into_fragment!(u16); +into_fragment!(u32); +into_fragment!(u64); +into_fragment!(u128); -into_content!(i8); -into_content!(i16); -into_content!(i32); -into_content!(i64); -into_content!(i128); +into_fragment!(i8); +into_fragment!(i16); +into_fragment!(i32); +into_fragment!(i64); +into_fragment!(i128); -into_content!(f32); -into_content!(f64); +into_fragment!(f32); +into_fragment!(f64); -- cgit From dee43d5f66c97dddffebbc02c4a87674fb2c13b9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 2 Apr 2024 09:24:22 +0200 Subject: Implement `IntoFragment` for `usize` and `isize` --- core/src/widget/text.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'core/src/widget') diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index 123c6a69..53591e41 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -421,12 +421,14 @@ into_fragment!(u16); into_fragment!(u32); into_fragment!(u64); into_fragment!(u128); +into_fragment!(usize); into_fragment!(i8); into_fragment!(i16); into_fragment!(i32); into_fragment!(i64); into_fragment!(i128); +into_fragment!(isize); into_fragment!(f32); into_fragment!(f64); -- cgit From 99a904112ca111f2ab0e60e30b6c369741b1653b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 2 Apr 2024 10:08:10 +0200 Subject: Implement `IntoFragment` for `Fragment` --- core/src/widget/text.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'core/src/widget') diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index 53591e41..f1f0b345 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -379,6 +379,18 @@ pub trait IntoFragment<'a> { fn into_fragment(self) -> Fragment<'a>; } +impl<'a> IntoFragment<'a> for Fragment<'a> { + fn into_fragment(self) -> Fragment<'a> { + self + } +} + +impl<'a, 'b> IntoFragment<'a> for &'a Fragment<'b> { + fn into_fragment(self) -> Fragment<'a> { + Fragment::Borrowed(self) + } +} + impl<'a> IntoFragment<'a> for &'a str { fn into_fragment(self) -> Fragment<'a> { Fragment::Borrowed(self) -- cgit