From ebb4646358a291fdaa3a4aee8669910865f568c9 Mon Sep 17 00:00:00 2001 From: Dimitris Papaioannou Date: Tue, 29 Oct 2024 11:50:06 +0200 Subject: [PATCH] Impl from Box for WidgetText, RichText (#5309) `Box` is an immutable heap-allocated string slice. This PR makes it more convenient to use them in labels for example. Before this PR ```rust let text: Box = "Hello".into(); ui.label(text.into_string()); let text_ref: &Box = &"Hello".into(); ui.label(text_ref.clone().into_string()); // or ui.label(text_ref.as_ref()); // or ui.label(&**text_ref); ``` After this PR ```rust let text: Box = "Hello".into(); ui.label(text); let text_ref: &Box = &"Hello".into(); ui.label(text_ref); ``` * [x] I have followed the instructions in the PR template --- crates/egui/src/widget_text.rs | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/crates/egui/src/widget_text.rs b/crates/egui/src/widget_text.rs index 03b8cbb9..80ee6f05 100644 --- a/crates/egui/src/widget_text.rs +++ b/crates/egui/src/widget_text.rs @@ -67,6 +67,27 @@ impl From for RichText { } } +impl From<&Box> for RichText { + #[inline] + fn from(text: &Box) -> Self { + Self::new(text.clone()) + } +} + +impl From<&mut Box> for RichText { + #[inline] + fn from(text: &mut Box) -> Self { + Self::new(text.clone()) + } +} + +impl From> for RichText { + #[inline] + fn from(text: Box) -> Self { + Self::new(text) + } +} + impl From> for RichText { #[inline] fn from(text: Cow<'_, str>) -> Self { @@ -701,6 +722,20 @@ impl From for WidgetText { } } +impl From<&Box> for WidgetText { + #[inline] + fn from(text: &Box) -> Self { + Self::RichText(RichText::new(text.clone())) + } +} + +impl From> for WidgetText { + #[inline] + fn from(text: Box) -> Self { + Self::RichText(RichText::new(text)) + } +} + impl From> for WidgetText { #[inline] fn from(text: Cow<'_, str>) -> Self {