From 92c881bcaf6a2e9bf831820e3798ccaef063a376 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Audiger?= Date: Mon, 6 Mar 2023 22:30:45 +0100 Subject: [PATCH] Constify functions that can be constified. --- src/ansi.rs | 6 +++--- src/display.rs | 6 +++--- src/gradient.rs | 1 + src/rgb.rs | 6 +++--- src/style.rs | 20 ++++++++++---------- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/ansi.rs b/src/ansi.rs index 8f393fc..0249da0 100644 --- a/src/ansi.rs +++ b/src/ansi.rs @@ -194,7 +194,7 @@ impl Style { /// assert_eq!("", /// style.prefix().to_string()); /// ``` - pub fn prefix(self) -> Prefix { + pub const fn prefix(self) -> Prefix { Prefix(self) } @@ -219,7 +219,7 @@ impl Style { /// assert_eq!("", /// style.infix(style).to_string()); /// ``` - pub fn infix(self, next: Style) -> Infix { + pub const fn infix(self, next: Style) -> Infix { Infix(self, next) } @@ -243,7 +243,7 @@ impl Style { /// assert_eq!("", /// style.suffix().to_string()); /// ``` - pub fn suffix(self) -> Suffix { + pub const fn suffix(self) -> Suffix { Suffix(self) } } diff --git a/src/display.rs b/src/display.rs index 2ba35e4..c066ccd 100644 --- a/src/display.rs +++ b/src/display.rs @@ -107,7 +107,7 @@ where ::Owned: fmt::Debug, { /// Directly access the style - pub fn style_ref(&self) -> &Style { + pub const fn style_ref(&self) -> &Style { &self.style } @@ -136,7 +136,7 @@ pub type AnsiStrings<'a> = AnsiGenericStrings<'a, str>; /// A function to construct an `AnsiStrings` instance. #[allow(non_snake_case)] -pub fn AnsiStrings<'a>(arg: &'a [AnsiString<'a>]) -> AnsiStrings<'a> { +pub const fn AnsiStrings<'a>(arg: &'a [AnsiString<'a>]) -> AnsiStrings<'a> { AnsiGenericStrings(arg) } @@ -146,7 +146,7 @@ pub type AnsiByteStrings<'a> = AnsiGenericStrings<'a, [u8]>; /// A function to construct an `AnsiByteStrings` instance. #[allow(non_snake_case)] -pub fn AnsiByteStrings<'a>(arg: &'a [AnsiByteString<'a>]) -> AnsiByteStrings<'a> { +pub const fn AnsiByteStrings<'a>(arg: &'a [AnsiByteString<'a>]) -> AnsiByteStrings<'a> { AnsiGenericStrings(arg) } diff --git a/src/gradient.rs b/src/gradient.rs index a0d94c8..2c471af 100644 --- a/src/gradient.rs +++ b/src/gradient.rs @@ -16,6 +16,7 @@ impl Gradient { pub const fn new(start: Rgb, end: Rgb) -> Self { Self { start, end } } + pub const fn from_color_rgb(start: Color, end: Color) -> Self { let start_grad = match start { Color::Rgb(r, g, b) => Rgb { r, g, b }, diff --git a/src/rgb.rs b/src/rgb.rs index 66500d8..d4c0acd 100644 --- a/src/rgb.rs +++ b/src/rgb.rs @@ -122,7 +122,7 @@ impl ANSIColorCode for Rgb { } } -fn rgb_add(lhs: &Rgb, rhs: &Rgb) -> Rgb { +const fn rgb_add(lhs: &Rgb, rhs: &Rgb) -> Rgb { Rgb::new( lhs.r.saturating_add(rhs.r), lhs.g.saturating_add(rhs.g), @@ -130,7 +130,7 @@ fn rgb_add(lhs: &Rgb, rhs: &Rgb) -> Rgb { ) } -fn rgb_sub(lhs: &Rgb, rhs: &Rgb) -> Rgb { +const fn rgb_sub(lhs: &Rgb, rhs: &Rgb) -> Rgb { Rgb::new( lhs.r.saturating_sub(rhs.r), lhs.g.saturating_sub(rhs.g), @@ -146,7 +146,7 @@ fn rgb_mul_f32(lhs: &Rgb, rhs: &f32) -> Rgb { ) } -fn rgb_negate(rgb: &Rgb) -> Rgb { +const fn rgb_negate(rgb: &Rgb) -> Rgb { Rgb::new(255 - rgb.r, 255 - rgb.g, 255 - rgb.b) } diff --git a/src/style.rs b/src/style.rs index dfcff3a..b833f44 100644 --- a/src/style.rs +++ b/src/style.rs @@ -71,7 +71,7 @@ impl Style { /// let style = Style::new().bold(); /// println!("{}", style.paint("hey")); /// ``` - pub fn bold(&self) -> Style { + pub const fn bold(&self) -> Style { Style { is_bold: true, ..*self @@ -88,7 +88,7 @@ impl Style { /// let style = Style::new().dimmed(); /// println!("{}", style.paint("sup")); /// ``` - pub fn dimmed(&self) -> Style { + pub const fn dimmed(&self) -> Style { Style { is_dimmed: true, ..*self @@ -105,7 +105,7 @@ impl Style { /// let style = Style::new().italic(); /// println!("{}", style.paint("greetings")); /// ``` - pub fn italic(&self) -> Style { + pub const fn italic(&self) -> Style { Style { is_italic: true, ..*self @@ -122,7 +122,7 @@ impl Style { /// let style = Style::new().underline(); /// println!("{}", style.paint("salutations")); /// ``` - pub fn underline(&self) -> Style { + pub const fn underline(&self) -> Style { Style { is_underline: true, ..*self @@ -138,7 +138,7 @@ impl Style { /// let style = Style::new().blink(); /// println!("{}", style.paint("wazzup")); /// ``` - pub fn blink(&self) -> Style { + pub const fn blink(&self) -> Style { Style { is_blink: true, ..*self @@ -155,7 +155,7 @@ impl Style { /// let style = Style::new().reverse(); /// println!("{}", style.paint("aloha")); /// ``` - pub fn reverse(&self) -> Style { + pub const fn reverse(&self) -> Style { Style { is_reverse: true, ..*self @@ -172,7 +172,7 @@ impl Style { /// let style = Style::new().hidden(); /// println!("{}", style.paint("ahoy")); /// ``` - pub fn hidden(&self) -> Style { + pub const fn hidden(&self) -> Style { Style { is_hidden: true, ..*self @@ -189,7 +189,7 @@ impl Style { /// let style = Style::new().strikethrough(); /// println!("{}", style.paint("yo")); /// ``` - pub fn strikethrough(&self) -> Style { + pub const fn strikethrough(&self) -> Style { Style { is_strikethrough: true, ..*self @@ -206,7 +206,7 @@ impl Style { /// let style = Style::new().fg(Color::Yellow); /// println!("{}", style.paint("hi")); /// ``` - pub fn fg(&self, foreground: Color) -> Style { + pub const fn fg(&self, foreground: Color) -> Style { Style { foreground: Some(foreground), ..*self @@ -223,7 +223,7 @@ impl Style { /// let style = Style::new().on(Color::Blue); /// println!("{}", style.paint("eyyyy")); /// ``` - pub fn on(&self, background: Color) -> Style { + pub const fn on(&self, background: Color) -> Style { Style { background: Some(background), ..*self