Replace impl Into<String> with impl ToString (#302)

* Replace `impl Into<String>` with `impl ToString`

This is something I ran into today. Types that implement
`std::fmt::Display` cannot be passed to functions that take `impl
Into<String>`. You have to call `display_thing.to_string()`. Its a small
thing but would be fixed by instead taking `impl ToString`.

Afaik `impl ToString` is a superset of `impl Into<String>`, unless users
manually implement `Into<String> for T` (or `From<T> for String`) for
their own types. However I think its more common to implement `Display`
as that works with `println` and friends. The main difference is that
`Display::fmt` can return errors but thats also quite rare in my
experience.

I did some testing in a [playground] and seems to work.

[playground]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=1111e071f6ae416ae2688d58d2e9b575

* Silence warnings
This commit is contained in:
David Pedersen 2021-04-29 19:49:49 +02:00 committed by GitHub
parent e991a1c310
commit 02a62d1986
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 97 additions and 73 deletions

View file

@ -148,7 +148,7 @@ impl CollapsingHeader {
/// If the label is unique and static this is fine,
/// but if it changes or there are several `CollapsingHeader` with the same title
/// you need to provide a unique id source with [`Self::id_source`].
pub fn new(label: impl Into<String>) -> Self {
pub fn new(label: impl ToString) -> Self {
let label = Label::new(label).text_style(TextStyle::Button).wrap(false);
let id_source = Id::new(label.text());
Self {

View file

@ -55,8 +55,9 @@ impl ComboBox {
}
/// What we show as the currently selected value
pub fn selected_text(mut self, selected_text: impl Into<String>) -> Self {
self.selected_text = selected_text.into();
#[allow(clippy::needless_pass_by_value)]
pub fn selected_text(mut self, selected_text: impl ToString) -> Self {
self.selected_text = selected_text.to_string();
self
}
@ -150,7 +151,7 @@ impl ComboBox {
pub fn combo_box_with_label(
ui: &mut Ui,
label: impl Into<Label>,
selected: impl Into<String>,
selected: impl ToString,
menu_contents: impl FnOnce(&mut Ui),
) -> Response {
let label = label.into();
@ -165,10 +166,11 @@ pub fn combo_box_with_label(
.inner
}
#[allow(clippy::needless_pass_by_value)]
fn combo_box(
ui: &mut Ui,
button_id: Id,
selected: impl Into<String>,
selected: impl ToString,
menu_contents: impl FnOnce(&mut Ui),
) -> Response {
let popup_id = button_id.with("popup");
@ -181,7 +183,7 @@ fn combo_box(
let galley = ui
.fonts()
.layout_no_wrap(TextStyle::Button, selected.into());
.layout_no_wrap(TextStyle::Button, selected.to_string());
let width = galley.size.x + ui.spacing().item_spacing.x + icon_size.x;
let width = width.at_least(full_minimum_width);

View file

@ -122,7 +122,7 @@ pub fn show_tooltip_at(
/// egui::show_tooltip_text(ui.ctx(), egui::Id::new("my_tooltip"), "Helpful text");
/// }
/// ```
pub fn show_tooltip_text(ctx: &CtxRef, id: Id, text: impl Into<String>) {
pub fn show_tooltip_text(ctx: &CtxRef, id: Id, text: impl ToString) {
show_tooltip(ctx, id, |ui| {
ui.add(crate::widgets::Label::new(text));
})

View file

@ -36,8 +36,9 @@ impl<'open> Window<'open> {
/// The window title is used as a unique [`Id`] and must be unique, and should not change.
/// This is true even if you disable the title bar with `.title_bar(false)`.
/// If you need a changing title, you must call `window.id(…)` with a fixed id.
pub fn new(title: impl Into<String>) -> Self {
let title = title.into();
#[allow(clippy::needless_pass_by_value)]
pub fn new(title: impl ToString) -> Self {
let title = title.to_string();
let area = Area::new(&title);
let title_label = Label::new(title).text_style(TextStyle::Heading).wrap(false);
Self {

View file

@ -31,7 +31,7 @@ pub struct Output {
impl Output {
/// Open the given url in a web browser.
/// If egui is running in a browser, the same tab will be reused.
pub fn open_url(&mut self, url: impl Into<String>) {
pub fn open_url(&mut self, url: impl ToString) {
self.open_url = Some(OpenUrl::same_tab(url))
}
@ -59,16 +59,18 @@ pub struct OpenUrl {
}
impl OpenUrl {
pub fn same_tab(url: impl Into<String>) -> Self {
#[allow(clippy::needless_pass_by_value)]
pub fn same_tab(url: impl ToString) -> Self {
Self {
url: url.into(),
url: url.to_string(),
new_tab: false,
}
}
pub fn new_tab(url: impl Into<String>) -> Self {
#[allow(clippy::needless_pass_by_value)]
pub fn new_tab(url: impl ToString) -> Self {
Self {
url: url.into(),
url: url.to_string(),
new_tab: true,
}
}
@ -279,17 +281,19 @@ impl WidgetInfo {
}
}
pub fn labeled(typ: WidgetType, label: impl Into<String>) -> Self {
#[allow(clippy::needless_pass_by_value)]
pub fn labeled(typ: WidgetType, label: impl ToString) -> Self {
Self {
label: Some(label.into()),
label: Some(label.to_string()),
..Self::new(typ)
}
}
/// checkboxes, radio-buttons etc
pub fn selected(typ: WidgetType, selected: bool, label: impl Into<String>) -> Self {
#[allow(clippy::needless_pass_by_value)]
pub fn selected(typ: WidgetType, selected: bool, label: impl ToString) -> Self {
Self {
label: Some(label.into()),
label: Some(label.to_string()),
selected: Some(selected),
..Self::new(typ)
}
@ -302,8 +306,9 @@ impl WidgetInfo {
}
}
pub fn slider(value: f64, label: impl Into<String>) -> Self {
let label = label.into();
#[allow(clippy::needless_pass_by_value)]
pub fn slider(value: f64, label: impl ToString) -> Self {
let label = label.to_string();
Self {
label: if label.is_empty() { None } else { Some(label) },
value: Some(value),
@ -311,9 +316,10 @@ impl WidgetInfo {
}
}
pub fn text_edit(edit_text: impl Into<String>) -> Self {
#[allow(clippy::needless_pass_by_value)]
pub fn text_edit(edit_text: impl ToString) -> Self {
Self {
edit_text: Some(edit_text.into()),
edit_text: Some(edit_text.to_string()),
..Self::new(WidgetType::TextEdit)
}
}

View file

@ -60,16 +60,13 @@ pub fn bar<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResp
}
/// Construct a top level menu in a menu bar. This would be e.g. "File", "Edit" etc.
pub fn menu(ui: &mut Ui, title: impl Into<String>, add_contents: impl FnOnce(&mut Ui)) {
pub fn menu(ui: &mut Ui, title: impl ToString, add_contents: impl FnOnce(&mut Ui)) {
menu_impl(ui, title, Box::new(add_contents))
}
fn menu_impl<'c>(
ui: &mut Ui,
title: impl Into<String>,
add_contents: Box<dyn FnOnce(&mut Ui) + 'c>,
) {
let title = title.into();
#[allow(clippy::needless_pass_by_value)]
fn menu_impl<'c>(ui: &mut Ui, title: impl ToString, add_contents: Box<dyn FnOnce(&mut Ui) + 'c>) {
let title = title.to_string();
let bar_id = ui.id();
let menu_id = bar_id.with(&title);

View file

@ -173,10 +173,17 @@ impl Painter {
/// ## Debug painting
impl Painter {
pub fn debug_rect(&mut self, rect: Rect, color: Color32, text: impl Into<String>) {
#[allow(clippy::needless_pass_by_value)]
pub fn debug_rect(&mut self, rect: Rect, color: Color32, text: impl ToString) {
self.rect_stroke(rect, 0.0, (1.0, color));
let text_style = TextStyle::Monospace;
self.text(rect.min, Align2::LEFT_TOP, text.into(), text_style, color);
self.text(
rect.min,
Align2::LEFT_TOP,
text.to_string(),
text_style,
color,
);
}
pub fn error(&self, pos: Pos2, text: impl std::fmt::Display) -> Rect {
@ -293,17 +300,18 @@ impl Painter {
/// To center the text at the given position, use `anchor: (Center, Center)`.
///
/// Returns where the text ended up.
#[allow(clippy::needless_pass_by_value)]
pub fn text(
&self,
pos: Pos2,
anchor: Align2,
text: impl Into<String>,
text: impl ToString,
text_style: TextStyle,
text_color: Color32,
) -> Rect {
let galley = self
.fonts()
.layout_multiline(text_style, text.into(), f32::INFINITY);
.layout_multiline(text_style, text.to_string(), f32::INFINITY);
let rect = anchor.anchor_rect(Rect::from_min_size(pos, galley.size));
self.galley(rect.min, galley, text_color);
rect

View file

@ -356,21 +356,21 @@ impl Response {
///
/// The text will not be visible if the widget is not enabled.
/// If you call this multiple times the tooltips will stack underneath the previous ones.
pub fn on_hover_text(self, text: impl Into<String>) -> Self {
pub fn on_hover_text(self, text: impl ToString) -> Self {
self.on_hover_ui(|ui| {
ui.add(crate::widgets::Label::new(text));
})
}
/// Show this text when hovering if the widget is disabled.
pub fn on_disabled_hover_text(self, text: impl Into<String>) -> Self {
pub fn on_disabled_hover_text(self, text: impl ToString) -> Self {
self.on_disabled_hover_ui(|ui| {
ui.add(crate::widgets::Label::new(text));
})
}
#[deprecated = "Deprecated 2020-10-01: use `on_hover_text` instead."]
pub fn tooltip_text(self, text: impl Into<String>) -> Self {
pub fn tooltip_text(self, text: impl ToString) -> Self {
self.on_hover_text(text)
}

View file

@ -899,7 +899,7 @@ impl Ui {
/// Shortcut for `add(Hyperlink::new(url))`
///
/// See also [`Hyperlink`].
pub fn hyperlink(&mut self, url: impl Into<String>) -> Response {
pub fn hyperlink(&mut self, url: impl ToString) -> Response {
Hyperlink::new(url).ui(self)
}
@ -911,7 +911,7 @@ impl Ui {
/// ```
///
/// See also [`Hyperlink`].
pub fn hyperlink_to(&mut self, label: impl Into<String>, url: impl Into<String>) -> Response {
pub fn hyperlink_to(&mut self, label: impl ToString, url: impl ToString) -> Response {
Hyperlink::new(url).text(label).ui(self)
}
@ -941,7 +941,7 @@ impl Ui {
/// See also [`Button`].
#[must_use = "You should check if the user clicked this with `if ui.button(…).clicked() { … } "]
#[inline(always)]
pub fn button(&mut self, text: impl Into<String>) -> Response {
pub fn button(&mut self, text: impl ToString) -> Response {
Button::new(text).ui(self)
}
@ -951,19 +951,19 @@ impl Ui {
///
/// Shortcut for `add(Button::new(text).small())`
#[must_use = "You should check if the user clicked this with `if ui.small_button(…).clicked() { … } "]
pub fn small_button(&mut self, text: impl Into<String>) -> Response {
pub fn small_button(&mut self, text: impl ToString) -> Response {
Button::new(text).small().ui(self)
}
/// Show a checkbox.
pub fn checkbox(&mut self, checked: &mut bool, text: impl Into<String>) -> Response {
pub fn checkbox(&mut self, checked: &mut bool, text: impl ToString) -> Response {
Checkbox::new(checked, text).ui(self)
}
/// Show a [`RadioButton`].
/// Often you want to use [`Self::radio_value`] instead.
#[must_use = "You should check if the user clicked this with `if ui.radio(…).clicked() { … } "]
pub fn radio(&mut self, selected: bool, text: impl Into<String>) -> Response {
pub fn radio(&mut self, selected: bool, text: impl ToString) -> Response {
RadioButton::new(selected, text).ui(self)
}
@ -988,7 +988,7 @@ impl Ui {
&mut self,
current_value: &mut Value,
selected_value: Value,
text: impl Into<String>,
text: impl ToString,
) -> Response {
let mut response = self.radio(*current_value == selected_value, text);
if response.clicked() {
@ -1002,7 +1002,7 @@ impl Ui {
///
/// See also [`SelectableLabel`].
#[must_use = "You should check if the user clicked this with `if ui.selectable_label(…).clicked() { … } "]
pub fn selectable_label(&mut self, checked: bool, text: impl Into<String>) -> Response {
pub fn selectable_label(&mut self, checked: bool, text: impl ToString) -> Response {
SelectableLabel::new(checked, text).ui(self)
}
@ -1016,7 +1016,7 @@ impl Ui {
&mut self,
current_value: &mut Value,
selected_value: Value,
text: impl Into<String>,
text: impl ToString,
) -> Response {
let mut response = self.selectable_label(*current_value == selected_value, text);
if response.clicked() {
@ -1212,7 +1212,7 @@ impl Ui {
/// A [`CollapsingHeader`] that starts out collapsed.
pub fn collapsing<R>(
&mut self,
heading: impl Into<String>,
heading: impl ToString,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> CollapsingResponse<R> {
CollapsingHeader::new(heading).show(self, add_contents)

View file

@ -26,9 +26,10 @@ pub struct Button {
}
impl Button {
pub fn new(text: impl Into<String>) -> Self {
#[allow(clippy::needless_pass_by_value)]
pub fn new(text: impl ToString) -> Self {
Self {
text: text.into(),
text: text.to_string(),
text_color: None,
text_style: TextStyle::Button,
fill: Default::default(),
@ -211,10 +212,11 @@ pub struct Checkbox<'a> {
}
impl<'a> Checkbox<'a> {
pub fn new(checked: &'a mut bool, text: impl Into<String>) -> Self {
#[allow(clippy::needless_pass_by_value)]
pub fn new(checked: &'a mut bool, text: impl ToString) -> Self {
Checkbox {
checked,
text: text.into(),
text: text.to_string(),
text_color: None,
}
}
@ -321,10 +323,11 @@ pub struct RadioButton {
}
impl RadioButton {
pub fn new(checked: bool, text: impl Into<String>) -> Self {
#[allow(clippy::needless_pass_by_value)]
pub fn new(checked: bool, text: impl ToString) -> Self {
Self {
checked,
text: text.into(),
text: text.to_string(),
text_color: None,
}
}

View file

@ -16,24 +16,27 @@ pub struct Hyperlink {
}
impl Hyperlink {
pub fn new(url: impl Into<String>) -> Self {
let url = url.into();
#[allow(clippy::needless_pass_by_value)]
pub fn new(url: impl ToString) -> Self {
let url = url.to_string();
Self {
url: url.clone(),
label: Label::new(url),
}
}
pub fn from_label_and_url(label: impl Into<Label>, url: impl Into<String>) -> Self {
#[allow(clippy::needless_pass_by_value)]
pub fn from_label_and_url(label: impl Into<Label>, url: impl ToString) -> Self {
Self {
url: url.into(),
url: url.to_string(),
label: label.into(),
}
}
/// Show some other text than the url
pub fn text(mut self, text: impl Into<String>) -> Self {
self.label.text = text.into();
#[allow(clippy::needless_pass_by_value)]
pub fn text(mut self, text: impl ToString) -> Self {
self.label.text = text.to_string();
self
}

View file

@ -29,9 +29,10 @@ pub struct Label {
}
impl Label {
pub fn new(text: impl Into<String>) -> Self {
#[allow(clippy::needless_pass_by_value)]
pub fn new(text: impl ToString) -> Self {
Self {
text: text.into(),
text: text.to_string(),
wrap: None,
text_style: None,
background_color: Color32::TRANSPARENT,

View file

@ -28,10 +28,11 @@ pub struct SelectableLabel {
}
impl SelectableLabel {
pub fn new(selected: bool, text: impl Into<String>) -> Self {
#[allow(clippy::needless_pass_by_value)]
pub fn new(selected: bool, text: impl ToString) -> Self {
Self {
selected,
text: text.into(),
text: text.to_string(),
}
}
}

View file

@ -174,8 +174,8 @@ impl<'a> Slider<'a> {
}
/// Show a text next to the slider (e.g. explaining what the slider controls).
pub fn text(mut self, text: impl Into<String>) -> Self {
self.text = text.into();
pub fn text(mut self, text: impl ToString) -> Self {
self.text = text.to_string();
self
}

View file

@ -204,8 +204,9 @@ impl<'t> TextEdit<'t> {
}
/// Show a faint hint text when the text field is empty.
pub fn hint_text(mut self, hint_text: impl Into<String>) -> Self {
self.hint_text = hint_text.into();
#[allow(clippy::needless_pass_by_value)]
pub fn hint_text(mut self, hint_text: impl ToString) -> Self {
self.hint_text = hint_text.to_string();
self
}

View file

@ -124,15 +124,16 @@ impl Shape {
}
}
#[allow(clippy::needless_pass_by_value)]
pub fn text(
fonts: &Fonts,
pos: Pos2,
anchor: Align2,
text: impl Into<String>,
text: impl ToString,
text_style: TextStyle,
color: Color32,
) -> Self {
let galley = fonts.layout_multiline(text_style, text.into(), f32::INFINITY);
let galley = fonts.layout_multiline(text_style, text.to_string(), f32::INFINITY);
let rect = anchor.anchor_rect(Rect::from_min_size(pos, galley.size));
Self::Text {
pos: rect.min,

View file

@ -340,20 +340,20 @@ pub mod http {
impl Request {
/// Create a `GET` requests with the given url.
pub fn get(url: impl Into<String>) -> Self {
pub fn get(url: impl ToString) -> Self {
Self {
method: "GET".to_owned(),
url: url.into(),
url: url.to_string(),
body: "".to_string(),
}
}
/// Create a `POST` requests with the give url and body.
pub fn post(url: impl Into<String>, body: impl Into<String>) -> Self {
pub fn post(url: impl ToString, body: impl ToString) -> Self {
Self {
method: "POST".to_owned(),
url: url.into(),
body: body.into(),
url: url.to_string(),
body: body.to_string(),
}
}
}