Skip to content

Commit

Permalink
feat(anstyle): Allow copy/clone of render
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Sep 28, 2023
1 parent 6f9165a commit 5af61c4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
20 changes: 10 additions & 10 deletions crates/anstyle/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Color {

/// Render the ANSI code for a foreground color
#[inline]
pub fn render_fg(self) -> impl core::fmt::Display {
pub fn render_fg(self) -> impl core::fmt::Display + Copy + Clone {
match self {
Self::Ansi(color) => DisplayBuffer::default().write_str(color.as_fg_str()),
Self::Ansi256(color) => color.as_fg_buffer(),
Expand All @@ -44,7 +44,7 @@ impl Color {

/// Render the ANSI code for a background color
#[inline]
pub fn render_bg(self) -> impl core::fmt::Display {
pub fn render_bg(self) -> impl core::fmt::Display + Copy + Clone {
match self {
Self::Ansi(color) => DisplayBuffer::default().write_str(color.as_bg_str()),
Self::Ansi256(color) => color.as_bg_buffer(),
Expand All @@ -64,7 +64,7 @@ impl Color {
}

#[inline]
pub(crate) fn render_underline(self) -> impl core::fmt::Display {
pub(crate) fn render_underline(self) -> impl core::fmt::Display + Copy + Clone {
match self {
Self::Ansi(color) => color.as_underline_buffer(),
Self::Ansi256(color) => color.as_underline_buffer(),
Expand Down Expand Up @@ -191,7 +191,7 @@ impl AnsiColor {

/// Render the ANSI code for a foreground color
#[inline]
pub fn render_fg(self) -> impl core::fmt::Display {
pub fn render_fg(self) -> impl core::fmt::Display + Copy + Clone {
self.as_fg_str()
}

Expand Down Expand Up @@ -219,7 +219,7 @@ impl AnsiColor {

/// Render the ANSI code for a background color
#[inline]
pub fn render_bg(self) -> impl core::fmt::Display {
pub fn render_bg(self) -> impl core::fmt::Display + Copy + Clone {
self.as_bg_str()
}

Expand Down Expand Up @@ -396,7 +396,7 @@ impl Ansi256Color {

/// Render the ANSI code for a foreground color
#[inline]
pub fn render_fg(self) -> impl core::fmt::Display {
pub fn render_fg(self) -> impl core::fmt::Display + Copy + Clone {
self.as_fg_buffer()
}

Expand All @@ -410,7 +410,7 @@ impl Ansi256Color {

/// Render the ANSI code for a background color
#[inline]
pub fn render_bg(self) -> impl core::fmt::Display {
pub fn render_bg(self) -> impl core::fmt::Display + Copy + Clone {
self.as_bg_buffer()
}

Expand Down Expand Up @@ -481,7 +481,7 @@ impl RgbColor {

/// Render the ANSI code for a foreground color
#[inline]
pub fn render_fg(self) -> impl core::fmt::Display {
pub fn render_fg(self) -> impl core::fmt::Display + Copy + Clone {
self.as_fg_buffer()
}

Expand All @@ -499,7 +499,7 @@ impl RgbColor {

/// Render the ANSI code for a background color
#[inline]
pub fn render_bg(self) -> impl core::fmt::Display {
pub fn render_bg(self) -> impl core::fmt::Display + Copy + Clone {
self.as_bg_buffer()
}

Expand Down Expand Up @@ -536,7 +536,7 @@ impl From<(u8, u8, u8)> for RgbColor {
}
}

#[derive(Default, Debug)]
#[derive(Copy, Clone, Default, Debug)]
struct DisplayBuffer {
buffer: [u8; 19],
len: usize,
Expand Down
3 changes: 2 additions & 1 deletion crates/anstyle/src/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl Effects {

/// Render the ANSI code
#[inline]
pub fn render(self) -> impl core::fmt::Display {
pub fn render(self) -> impl core::fmt::Display + Copy + Clone {
EffectsDisplay(self)
}

Expand Down Expand Up @@ -307,6 +307,7 @@ pub(crate) const METADATA: [Metadata; 12] = [
},
];

#[derive(Copy, Clone, Default, Debug)]
struct EffectsDisplay(Effects);

impl core::fmt::Display for EffectsDisplay {
Expand Down
3 changes: 2 additions & 1 deletion crates/anstyle/src/reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ pub struct Reset;
impl Reset {
/// Render the ANSI code
#[inline]
pub fn render(self) -> impl core::fmt::Display {
pub fn render(self) -> impl core::fmt::Display + Copy + Clone {
ResetDisplay
}
}

#[derive(Copy, Clone, Default, Debug)]
struct ResetDisplay;

impl core::fmt::Display for ResetDisplay {
Expand Down
5 changes: 3 additions & 2 deletions crates/anstyle/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl Style {

/// Render the ANSI code
#[inline]
pub fn render(self) -> impl core::fmt::Display {
pub fn render(self) -> impl core::fmt::Display + Copy + Clone {
StyleDisplay(self)
}

Expand Down Expand Up @@ -121,7 +121,7 @@ impl Style {
///
/// Unlike [`Reset::render`][crate::Reset::render], this will elide the code if there is nothing to reset.
#[inline]
pub fn render_reset(self) -> impl core::fmt::Display {
pub fn render_reset(self) -> impl core::fmt::Display + Copy + Clone {
if self != Self::new() {
RESET
} else {
Expand Down Expand Up @@ -374,6 +374,7 @@ impl core::cmp::PartialEq<crate::Effects> for Style {
}
}

#[derive(Copy, Clone, Default, Debug)]
struct StyleDisplay(Style);

impl core::fmt::Display for StyleDisplay {
Expand Down

0 comments on commit 5af61c4

Please sign in to comment.