diff options
| author | Robin Krahl <robin.krahl@ireas.org> | 2019-01-11 00:41:59 +0000 | 
|---|---|---|
| committer | Robin Krahl <robin.krahl@ireas.org> | 2019-01-11 01:46:50 +0100 | 
| commit | 3efbb1ddc8b85f85621db9b37a0980ec88d2386d (patch) | |
| tree | b2d029ba4b8bf83fecb6f4e7cef090cbd562025f | |
| parent | 27834815893eca768f64c4fb9c5a85d42dea60e6 (diff) | |
| download | dialog-rs-3efbb1ddc8b85f85621db9b37a0980ec88d2386d.tar.gz dialog-rs-3efbb1ddc8b85f85621db9b37a0980ec88d2386d.tar.bz2 | |
Return Box<dyn Backend> in default_backend()
This patch refactors the default_backend function to return a Box<dyn
Backend> 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<Self> for the backend
structs.
| -rw-r--r-- | CHANGELOG.md | 3 | ||||
| -rw-r--r-- | src/backends/dialog.rs | 6 | ||||
| -rw-r--r-- | src/backends/zenity.rs | 6 | ||||
| -rw-r--r-- | 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<dyn Backend>`. +  # 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<Dialog> 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<Zenity> for Zenity { +    fn as_ref(&self) -> &Self { +        self +    } +} +  fn require_success(status: process::ExitStatus) -> Result<()> {      if status.success() {          Ok(()) @@ -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::Output> { -        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<Self::Output>; +    fn show_with<B>(&self, backend: impl AsRef<B>) -> Result<Self::Output> +    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<Self::Output> { -        backend.show_message(self) +    fn show_with<B>(&self, backend: impl AsRef<B>) -> Result<Self::Output> +    where +        B: backends::Backend + ?Sized, +    { +        backend.as_ref().show_message(self)      }  } @@ -206,8 +211,11 @@ impl Input {  impl DialogBox for Input {      type Output = Option<String>; -    fn show_with(&self, backend: &impl backends::Backend) -> Result<Self::Output> { -        backend.show_input(self) +    fn show_with<B>(&self, backend: impl AsRef<B>) -> Result<Self::Output> +    where +        B: backends::Backend + ?Sized, +    { +        backend.as_ref().show_input(self)      }  } @@ -256,8 +264,11 @@ impl Password {  impl DialogBox for Password {      type Output = Option<String>; -    fn show_with(&self, backend: &impl backends::Backend) -> Result<Self::Output> { -        backend.show_password(self) +    fn show_with<B>(&self, backend: impl AsRef<B>) -> Result<Self::Output> +    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<Self::Output> { -        backend.show_question(self) +    fn show_with<B>(&self, backend: impl AsRef<B>) -> Result<Self::Output> +    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<dyn backends::Backend> { +    Box::new(backends::Dialog::new())  } | 
