Skip to content

Commit

Permalink
Constify functions that can be constified.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaudiger committed Mar 11, 2023
1 parent 9fdb618 commit 92c881b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/ansi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ where
<S as ToOwned>::Owned: fmt::Debug,
{
/// Directly access the style
pub fn style_ref(&self) -> &Style {
pub const fn style_ref(&self) -> &Style {
&self.style
}

Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down
1 change: 1 addition & 0 deletions src/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
6 changes: 3 additions & 3 deletions src/rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ 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),
lhs.b.saturating_add(rhs.b),
)
}

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),
Expand All @@ -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)
}

Expand Down
20 changes: 10 additions & 10 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 92c881b

Please sign in to comment.