Skip to content

Commit

Permalink
refactor(const): Mark low level fns const
Browse files Browse the repository at this point in the history
Mark functions of `structs` that suit themselves to be composed as const,
namely:

- `Block`
- `Layout`
- `Rect`

Continuation of: ratatui#115
  • Loading branch information
a-kenji committed Jun 24, 2023
1 parent b808305 commit a09416c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
22 changes: 11 additions & 11 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,30 +107,30 @@ impl Layout {
self
}

pub fn margin(mut self, margin: u16) -> Layout {
pub const fn margin(mut self, margin: u16) -> Layout {
self.margin = Margin {
horizontal: margin,
vertical: margin,
};
self
}

pub fn horizontal_margin(mut self, horizontal: u16) -> Layout {
pub const fn horizontal_margin(mut self, horizontal: u16) -> Layout {
self.margin.horizontal = horizontal;
self
}

pub fn vertical_margin(mut self, vertical: u16) -> Layout {
pub const fn vertical_margin(mut self, vertical: u16) -> Layout {
self.margin.vertical = vertical;
self
}

pub fn direction(mut self, direction: Direction) -> Layout {
pub const fn direction(mut self, direction: Direction) -> Layout {
self.direction = direction;
self
}

pub(crate) fn expand_to_fill(mut self, expand_to_fill: bool) -> Layout {
pub(crate) const fn expand_to_fill(mut self, expand_to_fill: bool) -> Layout {
self.expand_to_fill = expand_to_fill;
self
}
Expand Down Expand Up @@ -422,23 +422,23 @@ impl Rect {
}
}

pub fn area(self) -> u16 {
pub const fn area(self) -> u16 {
self.width * self.height
}

pub fn left(self) -> u16 {
pub const fn left(self) -> u16 {
self.x
}

pub fn right(self) -> u16 {
pub const fn right(self) -> u16 {
self.x.saturating_add(self.width)
}

pub fn top(self) -> u16 {
pub const fn top(self) -> u16 {
self.y
}

pub fn bottom(self) -> u16 {
pub const fn bottom(self) -> u16 {
self.y.saturating_add(self.height)
}

Expand Down Expand Up @@ -481,7 +481,7 @@ impl Rect {
}
}

pub fn intersects(self, other: Rect) -> bool {
pub const fn intersects(self, other: Rect) -> bool {
self.x < other.x + other.width
&& self.x + self.width > other.x
&& self.y < other.y + other.height
Expand Down
28 changes: 14 additions & 14 deletions src/widgets/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum BorderType {
}

impl BorderType {
pub fn line_symbols(border_type: BorderType) -> line::Set {
pub const fn line_symbols(border_type: BorderType) -> line::Set {
match border_type {
BorderType::Plain => line::NORMAL,
BorderType::Rounded => line::ROUNDED,
Expand All @@ -38,7 +38,7 @@ pub struct Padding {
}

impl Padding {
pub fn new(left: u16, right: u16, top: u16, bottom: u16) -> Self {
pub const fn new(left: u16, right: u16, top: u16, bottom: u16) -> Self {
Padding {
left,
right,
Expand All @@ -47,7 +47,7 @@ impl Padding {
}
}

pub fn zero() -> Self {
pub const fn zero() -> Self {
Padding {
left: 0,
right: 0,
Expand All @@ -56,7 +56,7 @@ impl Padding {
}
}

pub fn horizontal(value: u16) -> Self {
pub const fn horizontal(value: u16) -> Self {
Padding {
left: value,
right: value,
Expand All @@ -65,7 +65,7 @@ impl Padding {
}
}

pub fn vertical(value: u16) -> Self {
pub const fn vertical(value: u16) -> Self {
Padding {
left: 0,
right: 0,
Expand All @@ -74,7 +74,7 @@ impl Padding {
}
}

pub fn uniform(value: u16) -> Self {
pub const fn uniform(value: u16) -> Self {
Padding {
left: value,
right: value,
Expand Down Expand Up @@ -192,7 +192,7 @@ impl<'a> Block<'a> {
}

/// Applies the style to all titles. If a title already has a style, it will add on top of it.
pub fn title_style(mut self, style: Style) -> Block<'a> {
pub const fn title_style(mut self, style: Style) -> Block<'a> {
self.titles_style = style;
self
}
Expand All @@ -210,7 +210,7 @@ impl<'a> Block<'a> {
/// .title("bar")
/// .title_alignment(Alignment::Center);
/// ```
pub fn title_alignment(mut self, alignment: Alignment) -> Block<'a> {
pub const fn title_alignment(mut self, alignment: Alignment) -> Block<'a> {
self.titles_alignment = alignment;
self
}
Expand All @@ -233,27 +233,27 @@ impl<'a> Block<'a> {
/// .title("bar")
/// .title_position(Position::Bottom);
/// ```
pub fn title_position(mut self, position: Position) -> Block<'a> {
pub const fn title_position(mut self, position: Position) -> Block<'a> {
self.titles_position = position;
self
}

pub fn border_style(mut self, style: Style) -> Block<'a> {
pub const fn border_style(mut self, style: Style) -> Block<'a> {
self.border_style = style;
self
}

pub fn style(mut self, style: Style) -> Block<'a> {
pub const fn style(mut self, style: Style) -> Block<'a> {
self.style = style;
self
}

pub fn borders(mut self, flag: Borders) -> Block<'a> {
pub const fn borders(mut self, flag: Borders) -> Block<'a> {
self.borders = flag;
self
}

pub fn border_type(mut self, border_type: BorderType) -> Block<'a> {
pub const fn border_type(mut self, border_type: BorderType) -> Block<'a> {
self.border_type = border_type;
self
}
Expand Down Expand Up @@ -317,7 +317,7 @@ impl<'a> Block<'a> {
inner
}

pub fn padding(mut self, padding: Padding) -> Block<'a> {
pub const fn padding(mut self, padding: Padding) -> Block<'a> {
self.padding = padding;
self
}
Expand Down

0 comments on commit a09416c

Please sign in to comment.