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. --- CHANGELOG.md | 3 +++ src/backends/dialog.rs | 6 ++++++ src/backends/zenity.rs | 6 ++++++ src/lib.rs | 40 +++++++++++++++++++++++++++------------- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e2feb87..58a4919 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# Unreleased +- Refactor `default_backend()` to return a `Box`. + # v0.1.1 (2019-01-11) - Add the `Password` dialog box. - Add the `Zenity` backend. diff --git a/src/backends/dialog.rs b/src/backends/dialog.rs index 939b8f3..6f078e6 100644 --- a/src/backends/dialog.rs +++ b/src/backends/dialog.rs @@ -77,6 +77,12 @@ impl Dialog { } } +impl AsRef for Dialog { + fn as_ref(&self) -> &Self { + self + } +} + fn require_success(status: process::ExitStatus) -> Result<()> { if status.success() { Ok(()) diff --git a/src/backends/zenity.rs b/src/backends/zenity.rs index a90c3da..4bf862b 100644 --- a/src/backends/zenity.rs +++ b/src/backends/zenity.rs @@ -88,6 +88,12 @@ impl Zenity { } } +impl AsRef for Zenity { + fn as_ref(&self) -> &Self { + self + } +} + fn require_success(status: process::ExitStatus) -> Result<()> { if status.success() { Ok(()) 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.3