Skip to content

Commit

Permalink
WIP: Remove the blanket impl on Display
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcefrog committed Dec 14, 2023
1 parent 5b39064 commit 2e0d7dc
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 45 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

- **Breaking change:** `Render` no longer has a blanket implementation for `Display`: you can no longer use a string, integer, or some object that implements `Display` as a model directly. You must instead implement `Render` explicitly.

It seems that the `Display` implementation is often not a very satisfactory progress bar, and the presence of the blanket implementation causes confusing error messages when `Render` is not implemented correctly.

- Change back to `std::sync::Mutex` from `parking_lot`, to keep dependencies smaller.

## 0.1.4
Expand Down
17 changes: 0 additions & 17 deletions examples/string_as_model.rs

This file was deleted.

31 changes: 3 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ processed, the amount of data transmitted or received, the currently active task
The Model can be any of these things, from simplest to most powerful:
1. Any type that implements [std::fmt::Display], such as a String or integer.
2. One of the provided [models].
3. An application-defined struct (or enum or other type) that implements [Model].
1. One of the provided [models].
2. An application-defined struct (or enum or other type) that implements [Model].
The model is responsible for rendering itself into a String, optionally with ANSI styling,
by implementing [Model::render] (or [std::fmt::Display]). Applications might
by implementing [Model::render]. Applications might
choose to use any of the Rust crates that can render ANSI control codes into a
string, such as yansi.
Expand Down Expand Up @@ -211,7 +210,6 @@ is welcome.

#![warn(missing_docs)]

use std::fmt::Display;
use std::io::{self, Write};
use std::sync::{Arc, Mutex};
use std::time::Instant;
Expand Down Expand Up @@ -292,29 +290,6 @@ pub trait Model {
}
}

/// Blanket implementation of Model for Display.
///
/// `self` is converted to a display string without regard for
/// the terminal width.
///
/// This allows direct use of e.g. a String or integer as a model
/// for very basic progress indications.
///
/// ```
/// use nutmeg::{Options, View};
///
/// let view = View::new(0, Options::default());
/// view.update(|model| *model += 1);
/// ```
impl<T> Model for T
where
T: Display,
{
fn render(&mut self, _width: usize) -> String {
self.to_string()
}
}

/// A view that draws and coordinates a progress bar on the terminal.
///
/// There should be only one `View` active on a terminal at any time, and
Expand Down

0 comments on commit 2e0d7dc

Please sign in to comment.