Skip to content

Commit

Permalink
Impl from Box<str> for WidgetText, RichText (emilk#5309)
Browse files Browse the repository at this point in the history
`Box<str>` 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<str> = "Hello".into();
ui.label(text.into_string());

let text_ref: &Box<str> = &"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<str> = "Hello".into();
ui.label(text);

let text_ref: &Box<str> = &"Hello".into();
ui.label(text_ref);
```

* [x] I have followed the instructions in the PR template
  • Loading branch information
dimtpap authored and hacknus committed Oct 30, 2024
1 parent 805eec3 commit 9479caa
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions crates/egui/src/widget_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ impl From<String> for RichText {
}
}

impl From<&Box<str>> for RichText {
#[inline]
fn from(text: &Box<str>) -> Self {
Self::new(text.clone())
}
}

impl From<&mut Box<str>> for RichText {
#[inline]
fn from(text: &mut Box<str>) -> Self {
Self::new(text.clone())
}
}

impl From<Box<str>> for RichText {
#[inline]
fn from(text: Box<str>) -> Self {
Self::new(text)
}
}

impl From<Cow<'_, str>> for RichText {
#[inline]
fn from(text: Cow<'_, str>) -> Self {
Expand Down Expand Up @@ -701,6 +722,20 @@ impl From<String> for WidgetText {
}
}

impl From<&Box<str>> for WidgetText {
#[inline]
fn from(text: &Box<str>) -> Self {
Self::RichText(RichText::new(text.clone()))
}
}

impl From<Box<str>> for WidgetText {
#[inline]
fn from(text: Box<str>) -> Self {
Self::RichText(RichText::new(text))
}
}

impl From<Cow<'_, str>> for WidgetText {
#[inline]
fn from(text: Cow<'_, str>) -> Self {
Expand Down

0 comments on commit 9479caa

Please sign in to comment.