From 3efbb1ddc8b85f85621db9b37a0980ec88d2386d Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Fri, 11 Jan 2019 00:41:59 +0000 Subject: Return Box in default_backend() This patch refactors the default_backend function to return a Box instead of impl Backend. This will allow us to dynamically choose the backend implementation in a future patch. To keep the current interface, we change show_with to accept both a reference to a backend instance as well as a reference to a boxed backend instance. This also means we have to implement AsRef for the backend structs. --- src/lib.rs | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 7e9c53d..87234bd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -96,13 +96,15 @@ pub trait DialogBox { /// Shows this dialog box using the default backend and returns the output. /// - /// `box.show()` is a shorthand for `box.show_with(&default_backend())`. + /// `box.show()` is a shorthand for `box.show_with(default_backend())`. fn show(&self) -> Result { - self.show_with(&default_backend()) + self.show_with(default_backend()) } /// Shows this dialog box using the given backend and returns the output. - fn show_with(&self, backend: &impl backends::Backend) -> Result; + fn show_with(&self, backend: impl AsRef) -> Result + where + B: backends::Backend + ?Sized; } /// A message box. @@ -146,8 +148,11 @@ impl Message { impl DialogBox for Message { type Output = (); - fn show_with(&self, backend: &impl backends::Backend) -> Result { - backend.show_message(self) + fn show_with(&self, backend: impl AsRef) -> Result + where + B: backends::Backend + ?Sized, + { + backend.as_ref().show_message(self) } } @@ -206,8 +211,11 @@ impl Input { impl DialogBox for Input { type Output = Option; - fn show_with(&self, backend: &impl backends::Backend) -> Result { - backend.show_input(self) + fn show_with(&self, backend: impl AsRef) -> Result + where + B: backends::Backend + ?Sized, + { + backend.as_ref().show_input(self) } } @@ -256,8 +264,11 @@ impl Password { impl DialogBox for Password { type Output = Option; - fn show_with(&self, backend: &impl backends::Backend) -> Result { - backend.show_password(self) + fn show_with(&self, backend: impl AsRef) -> Result + where + B: backends::Backend + ?Sized, + { + backend.as_ref().show_password(self) } } @@ -314,8 +325,11 @@ impl Question { impl DialogBox for Question { type Output = Choice; - fn show_with(&self, backend: &impl backends::Backend) -> Result { - backend.show_question(self) + fn show_with(&self, backend: impl AsRef) -> Result + where + B: backends::Backend + ?Sized, + { + backend.as_ref().show_question(self) } } @@ -324,6 +338,6 @@ impl DialogBox for Question { /// The current implementation always returns a [`Dialog`][] instance. /// /// [`Dialog`]: backends/struct.Dialog.html -pub fn default_backend() -> impl backends::Backend { - backends::Dialog::new() +pub fn default_backend() -> Box { + Box::new(backends::Dialog::new()) } -- cgit v1.2.1