From 02a62d198622eeb179f70e96ef67f421ff6e2286 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Thu, 29 Apr 2021 19:49:49 +0200 Subject: [PATCH] Replace `impl Into` with `impl ToString` (#302) * Replace `impl Into` 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`. 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`, unless users manually implement `Into for T` (or `From 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 --- egui/src/containers/collapsing_header.rs | 2 +- egui/src/containers/combo_box.rs | 12 +++++---- egui/src/containers/popup.rs | 2 +- egui/src/containers/window.rs | 5 ++-- egui/src/data/output.rs | 32 ++++++++++++++---------- egui/src/menu.rs | 11 +++----- egui/src/painter.rs | 16 +++++++++--- egui/src/response.rs | 6 ++--- egui/src/ui.rs | 20 +++++++-------- egui/src/widgets/button.rs | 15 ++++++----- egui/src/widgets/hyperlink.rs | 15 ++++++----- egui/src/widgets/label.rs | 5 ++-- egui/src/widgets/selected_label.rs | 5 ++-- egui/src/widgets/slider.rs | 4 +-- egui/src/widgets/text_edit.rs | 5 ++-- epaint/src/shape.rs | 5 ++-- epi/src/lib.rs | 10 ++++---- 17 files changed, 97 insertions(+), 73 deletions(-) diff --git a/egui/src/containers/collapsing_header.rs b/egui/src/containers/collapsing_header.rs index c05ef57a..b7bdc8f5 100644 --- a/egui/src/containers/collapsing_header.rs +++ b/egui/src/containers/collapsing_header.rs @@ -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) -> 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 { diff --git a/egui/src/containers/combo_box.rs b/egui/src/containers/combo_box.rs index 4622a119..8f19ffcf 100644 --- a/egui/src/containers/combo_box.rs +++ b/egui/src/containers/combo_box.rs @@ -55,8 +55,9 @@ impl ComboBox { } /// What we show as the currently selected value - pub fn selected_text(mut self, selected_text: impl Into) -> 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