chore: Add 'const' to functions where possible. (#1802)

The Clippy check for this (missing_const_for_fn) is already enabled, but
catches more cases in upcoming toolchain versions.

This is part of the work to unblock #1727
This commit is contained in:
Tyler Breisacher 2025-04-20 18:48:49 -07:00 committed by GitHub
parent 79cc92dfb6
commit d88cd29079
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
21 changed files with 55 additions and 55 deletions

View File

@ -197,15 +197,15 @@ impl App {
self.selected_index = index; self.selected_index = index;
} }
fn increment_spacing(&mut self) { const fn increment_spacing(&mut self) {
self.spacing = self.spacing.saturating_add(1); self.spacing = self.spacing.saturating_add(1);
} }
fn decrement_spacing(&mut self) { const fn decrement_spacing(&mut self) {
self.spacing = self.spacing.saturating_sub(1); self.spacing = self.spacing.saturating_sub(1);
} }
fn exit(&mut self) { const fn exit(&mut self) {
self.mode = AppMode::Quit; self.mode = AppMode::Quit;
} }

View File

@ -81,7 +81,7 @@ impl App {
Ok(()) Ok(())
} }
fn update_max_scroll_offset(&mut self) { const fn update_max_scroll_offset(&mut self) {
self.max_scroll_offset = (self.selected_tab.get_example_count() - 1) * EXAMPLE_HEIGHT; self.max_scroll_offset = (self.selected_tab.get_example_count() - 1) * EXAMPLE_HEIGHT;
} }
@ -108,7 +108,7 @@ impl App {
Ok(()) Ok(())
} }
fn quit(&mut self) { const fn quit(&mut self) {
self.state = AppState::Quit; self.state = AppState::Quit;
} }
@ -124,7 +124,7 @@ impl App {
self.scroll_offset = 0; self.scroll_offset = 0;
} }
fn up(&mut self) { const fn up(&mut self) {
self.scroll_offset = self.scroll_offset.saturating_sub(1); self.scroll_offset = self.scroll_offset.saturating_sub(1);
} }
@ -135,11 +135,11 @@ impl App {
.min(self.max_scroll_offset); .min(self.max_scroll_offset);
} }
fn top(&mut self) { const fn top(&mut self) {
self.scroll_offset = 0; self.scroll_offset = 0;
} }
fn bottom(&mut self) { const fn bottom(&mut self) {
self.scroll_offset = self.max_scroll_offset; self.scroll_offset = self.max_scroll_offset;
} }
} }

View File

@ -194,7 +194,7 @@ impl App {
self.selected_tab = self.selected_tab.previous(); self.selected_tab = self.selected_tab.previous();
} }
fn up(&mut self) { const fn up(&mut self) {
self.scroll_offset = self.scroll_offset.saturating_sub(1); self.scroll_offset = self.scroll_offset.saturating_sub(1);
} }
@ -205,7 +205,7 @@ impl App {
.min(max_scroll_offset()); .min(max_scroll_offset());
} }
fn top(&mut self) { const fn top(&mut self) {
self.scroll_offset = 0; self.scroll_offset = 0;
} }
@ -213,15 +213,15 @@ impl App {
self.scroll_offset = max_scroll_offset(); self.scroll_offset = max_scroll_offset();
} }
fn increment_spacing(&mut self) { const fn increment_spacing(&mut self) {
self.spacing = self.spacing.saturating_add(1); self.spacing = self.spacing.saturating_add(1);
} }
fn decrement_spacing(&mut self) { const fn decrement_spacing(&mut self) {
self.spacing = self.spacing.saturating_sub(1); self.spacing = self.spacing.saturating_sub(1);
} }
fn quit(&mut self) { const fn quit(&mut self) {
self.state = AppState::Quit; self.state = AppState::Quit;
} }
} }

View File

@ -93,11 +93,11 @@ impl App {
Ok(()) Ok(())
} }
fn start(&mut self) { const fn start(&mut self) {
self.state = AppState::Started; self.state = AppState::Started;
} }
fn quit(&mut self) { const fn quit(&mut self) {
self.state = AppState::Quitting; self.state = AppState::Quitting;
} }
} }

View File

@ -243,7 +243,7 @@ impl AgeField {
self.value = self.value.saturating_add(1).min(Self::MAX); self.value = self.value.saturating_add(1).min(Self::MAX);
} }
fn decrement(&mut self) { const fn decrement(&mut self) {
self.value = self.value.saturating_sub(1); self.value = self.value.saturating_sub(1);
} }

View File

@ -158,16 +158,16 @@ impl App {
self.state.select_previous_column(); self.state.select_previous_column();
} }
pub fn next_color(&mut self) { pub const fn next_color(&mut self) {
self.color_index = (self.color_index + 1) % PALETTES.len(); self.color_index = (self.color_index + 1) % PALETTES.len();
} }
pub fn previous_color(&mut self) { pub const fn previous_color(&mut self) {
let count = PALETTES.len(); let count = PALETTES.len();
self.color_index = (self.color_index + count - 1) % count; self.color_index = (self.color_index + count - 1) % count;
} }
pub fn set_colors(&mut self) { pub const fn set_colors(&mut self) {
self.colors = TableColors::new(&PALETTES[self.color_index]); self.colors = TableColors::new(&PALETTES[self.color_index]);
} }

View File

@ -128,7 +128,7 @@ impl App {
} }
} }
fn select_none(&mut self) { const fn select_none(&mut self) {
self.todo_list.state.select(None); self.todo_list.state.select(None);
} }
@ -139,11 +139,11 @@ impl App {
self.todo_list.state.select_previous(); self.todo_list.state.select_previous();
} }
fn select_first(&mut self) { const fn select_first(&mut self) {
self.todo_list.state.select_first(); self.todo_list.state.select_first();
} }
fn select_last(&mut self) { const fn select_last(&mut self) {
self.todo_list.state.select_last(); self.todo_list.state.select_last();
} }

View File

@ -117,7 +117,7 @@ impl App {
new_cursor_pos.clamp(0, self.input.chars().count()) new_cursor_pos.clamp(0, self.input.chars().count())
} }
fn reset_cursor(&mut self) { const fn reset_cursor(&mut self) {
self.character_index = 0; self.character_index = 0;
} }

View File

@ -83,13 +83,13 @@ impl Cell {
} }
/// Sets the foreground color of the cell. /// Sets the foreground color of the cell.
pub fn set_fg(&mut self, color: Color) -> &mut Self { pub const fn set_fg(&mut self, color: Color) -> &mut Self {
self.fg = color; self.fg = color;
self self
} }
/// Sets the background color of the cell. /// Sets the background color of the cell.
pub fn set_bg(&mut self, color: Color) -> &mut Self { pub const fn set_bg(&mut self, color: Color) -> &mut Self {
self.bg = color; self.bg = color;
self self
} }
@ -132,7 +132,7 @@ impl Cell {
/// ///
/// This is helpful when it is necessary to prevent the buffer from overwriting a cell that is /// This is helpful when it is necessary to prevent the buffer from overwriting a cell that is
/// covered by an image from some terminal graphics protocol (Sixel / iTerm / Kitty ...). /// covered by an image from some terminal graphics protocol (Sixel / iTerm / Kitty ...).
pub fn set_skip(&mut self, skip: bool) -> &mut Self { pub const fn set_skip(&mut self, skip: bool) -> &mut Self {
self.skip = skip; self.skip = skip;
self self
} }

View File

@ -158,7 +158,7 @@ impl Frame<'_> {
} }
/// Gets the buffer that this `Frame` draws into as a mutable reference. /// Gets the buffer that this `Frame` draws into as a mutable reference.
pub fn buffer_mut(&mut self) -> &mut Buffer { pub const fn buffer_mut(&mut self) -> &mut Buffer {
self.buffer self.buffer
} }

View File

@ -166,7 +166,7 @@ where
} }
/// Get a Frame object which provides a consistent view into the terminal state for rendering. /// Get a Frame object which provides a consistent view into the terminal state for rendering.
pub fn get_frame(&mut self) -> Frame { pub const fn get_frame(&mut self) -> Frame {
let count = self.frame_count; let count = self.frame_count;
Frame { Frame {
cursor_position: None, cursor_position: None,
@ -177,7 +177,7 @@ where
} }
/// Gets the current buffer as a mutable reference. /// Gets the current buffer as a mutable reference.
pub fn current_buffer_mut(&mut self) -> &mut Buffer { pub const fn current_buffer_mut(&mut self) -> &mut Buffer {
&mut self.buffers[self.current] &mut self.buffers[self.current]
} }
@ -187,7 +187,7 @@ where
} }
/// Gets the backend as a mutable reference /// Gets the backend as a mutable reference
pub fn backend_mut(&mut self) -> &mut B { pub const fn backend_mut(&mut self) -> &mut B {
&mut self.backend &mut self.backend
} }

View File

@ -133,7 +133,7 @@ where
feature = "backend-writer", feature = "backend-writer",
issue = "https://github.com/ratatui/ratatui/pull/991" issue = "https://github.com/ratatui/ratatui/pull/991"
)] )]
pub fn writer_mut(&mut self) -> &mut W { pub const fn writer_mut(&mut self) -> &mut W {
&mut self.writer &mut self.writer
} }
} }

View File

@ -118,7 +118,7 @@ where
feature = "backend-writer", feature = "backend-writer",
issue = "https://github.com/ratatui/ratatui/pull/991" issue = "https://github.com/ratatui/ratatui/pull/991"
)] )]
pub fn writer_mut(&mut self) -> &mut W { pub const fn writer_mut(&mut self) -> &mut W {
&mut self.writer &mut self.writer
} }
} }

View File

@ -113,7 +113,7 @@ impl TermwizBackend {
} }
/// Returns a mutable reference to the buffered terminal used by the backend. /// Returns a mutable reference to the buffered terminal used by the backend.
pub fn buffered_terminal_mut(&mut self) -> &mut BufferedTerminal<SystemTerminal> { pub const fn buffered_terminal_mut(&mut self) -> &mut BufferedTerminal<SystemTerminal> {
&mut self.buffered_terminal &mut self.buffered_terminal
} }
} }

View File

@ -92,7 +92,7 @@ impl App {
Ok(()) Ok(())
} }
fn reset(&mut self) { const fn reset(&mut self) {
self.progress = 0.0; self.progress = 0.0;
self.progress_columns = 0; self.progress_columns = 0;
self.state = AppState::Stop; self.state = AppState::Stop;

View File

@ -436,7 +436,7 @@ impl Painter<'_, '_> {
/// let mut painter = Painter::from(&mut ctx); /// let mut painter = Painter::from(&mut ctx);
/// assert_eq!(painter.bounds(), (&[0.0, 2.0], &[0.0, 2.0])); /// assert_eq!(painter.bounds(), (&[0.0, 2.0], &[0.0, 2.0]));
/// ``` /// ```
pub fn bounds(&self) -> (&[f64; 2], &[f64; 2]) { pub const fn bounds(&self) -> (&[f64; 2], &[f64; 2]) {
(&self.context.x_bounds, &self.context.y_bounds) (&self.context.x_bounds, &self.context.y_bounds)
} }
} }

View File

@ -106,7 +106,7 @@ impl ListState {
/// let mut state = ListState::default(); /// let mut state = ListState::default();
/// *state.offset_mut() = 1; /// *state.offset_mut() = 1;
/// ``` /// ```
pub fn offset_mut(&mut self) -> &mut usize { pub const fn offset_mut(&mut self) -> &mut usize {
&mut self.offset &mut self.offset
} }
@ -138,7 +138,7 @@ impl ListState {
/// let mut state = ListState::default(); /// let mut state = ListState::default();
/// *state.selected_mut() = Some(1); /// *state.selected_mut() = Some(1);
/// ``` /// ```
pub fn selected_mut(&mut self) -> &mut Option<usize> { pub const fn selected_mut(&mut self) -> &mut Option<usize> {
&mut self.selected &mut self.selected
} }
@ -154,7 +154,7 @@ impl ListState {
/// let mut state = ListState::default(); /// let mut state = ListState::default();
/// state.select(Some(1)); /// state.select(Some(1));
/// ``` /// ```
pub fn select(&mut self, index: Option<usize>) { pub const fn select(&mut self, index: Option<usize>) {
self.selected = index; self.selected = index;
if index.is_none() { if index.is_none() {
self.offset = 0; self.offset = 0;
@ -210,7 +210,7 @@ impl ListState {
/// let mut state = ListState::default(); /// let mut state = ListState::default();
/// state.select_first(); /// state.select_first();
/// ``` /// ```
pub fn select_first(&mut self) { pub const fn select_first(&mut self) {
self.select(Some(0)); self.select(Some(0));
} }
@ -227,7 +227,7 @@ impl ListState {
/// let mut state = ListState::default(); /// let mut state = ListState::default();
/// state.select_last(); /// state.select_last();
/// ``` /// ```
pub fn select_last(&mut self) { pub const fn select_last(&mut self) {
self.select(Some(usize::MAX)); self.select(Some(usize::MAX));
} }

View File

@ -267,7 +267,7 @@ where
} }
/// Set the horizontal offset to skip render. /// Set the horizontal offset to skip render.
pub fn set_horizontal_offset(&mut self, horizontal_offset: u16) { pub const fn set_horizontal_offset(&mut self, horizontal_offset: u16) {
self.horizontal_offset = horizontal_offset; self.horizontal_offset = horizontal_offset;
} }
} }

View File

@ -456,7 +456,7 @@ impl ScrollbarState {
} }
/// Decrements the scroll position by one, ensuring it doesn't go below zero. /// Decrements the scroll position by one, ensuring it doesn't go below zero.
pub fn prev(&mut self) { pub const fn prev(&mut self) {
self.position = self.position.saturating_sub(1); self.position = self.position.saturating_sub(1);
} }
@ -469,12 +469,12 @@ impl ScrollbarState {
} }
/// Sets the scroll position to the start of the scrollable content. /// Sets the scroll position to the start of the scrollable content.
pub fn first(&mut self) { pub const fn first(&mut self) {
self.position = 0; self.position = 0;
} }
/// Sets the scroll position to the end of the scrollable content. /// Sets the scroll position to the end of the scrollable content.
pub fn last(&mut self) { pub const fn last(&mut self) {
self.position = self.content_length.saturating_sub(1); self.position = self.content_length.saturating_sub(1);
} }

View File

@ -182,7 +182,7 @@ impl TableState {
/// let mut state = TableState::default(); /// let mut state = TableState::default();
/// *state.offset_mut() = 1; /// *state.offset_mut() = 1;
/// ``` /// ```
pub fn offset_mut(&mut self) -> &mut usize { pub const fn offset_mut(&mut self) -> &mut usize {
&mut self.offset &mut self.offset
} }
@ -247,7 +247,7 @@ impl TableState {
/// let mut state = TableState::default(); /// let mut state = TableState::default();
/// *state.selected_mut() = Some(1); /// *state.selected_mut() = Some(1);
/// ``` /// ```
pub fn selected_mut(&mut self) -> &mut Option<usize> { pub const fn selected_mut(&mut self) -> &mut Option<usize> {
&mut self.selected &mut self.selected
} }
@ -262,7 +262,7 @@ impl TableState {
/// let mut state = TableState::default(); /// let mut state = TableState::default();
/// *state.selected_column_mut() = Some(1); /// *state.selected_column_mut() = Some(1);
/// ``` /// ```
pub fn selected_column_mut(&mut self) -> &mut Option<usize> { pub const fn selected_column_mut(&mut self) -> &mut Option<usize> {
&mut self.selected_column &mut self.selected_column
} }
@ -278,7 +278,7 @@ impl TableState {
/// let mut state = TableState::default(); /// let mut state = TableState::default();
/// state.select(Some(1)); /// state.select(Some(1));
/// ``` /// ```
pub fn select(&mut self, index: Option<usize>) { pub const fn select(&mut self, index: Option<usize>) {
self.selected = index; self.selected = index;
if index.is_none() { if index.is_none() {
self.offset = 0; self.offset = 0;
@ -294,7 +294,7 @@ impl TableState {
/// let mut state = TableState::default(); /// let mut state = TableState::default();
/// state.select_column(Some(1)); /// state.select_column(Some(1));
/// ``` /// ```
pub fn select_column(&mut self, index: Option<usize>) { pub const fn select_column(&mut self, index: Option<usize>) {
self.selected_column = index; self.selected_column = index;
} }
@ -309,7 +309,7 @@ impl TableState {
/// let mut state = TableState::default(); /// let mut state = TableState::default();
/// state.select_cell(Some((1, 5))); /// state.select_cell(Some((1, 5)));
/// ``` /// ```
pub fn select_cell(&mut self, indexes: Option<(usize, usize)>) { pub const fn select_cell(&mut self, indexes: Option<(usize, usize)>) {
if let Some((r, c)) = indexes { if let Some((r, c)) = indexes {
self.selected = Some(r); self.selected = Some(r);
self.selected_column = Some(c); self.selected_column = Some(c);
@ -405,7 +405,7 @@ impl TableState {
/// let mut state = TableState::default(); /// let mut state = TableState::default();
/// state.select_first(); /// state.select_first();
/// ``` /// ```
pub fn select_first(&mut self) { pub const fn select_first(&mut self) {
self.select(Some(0)); self.select(Some(0));
} }
@ -421,7 +421,7 @@ impl TableState {
/// let mut state = TableState::default(); /// let mut state = TableState::default();
/// state.select_first_column(); /// state.select_first_column();
/// ``` /// ```
pub fn select_first_column(&mut self) { pub const fn select_first_column(&mut self) {
self.select_column(Some(0)); self.select_column(Some(0));
} }
@ -438,7 +438,7 @@ impl TableState {
/// let mut state = TableState::default(); /// let mut state = TableState::default();
/// state.select_last(); /// state.select_last();
/// ``` /// ```
pub fn select_last(&mut self) { pub const fn select_last(&mut self) {
self.select(Some(usize::MAX)); self.select(Some(usize::MAX));
} }
@ -454,7 +454,7 @@ impl TableState {
/// let mut state = TableState::default(); /// let mut state = TableState::default();
/// state.select_last(); /// state.select_last();
/// ``` /// ```
pub fn select_last_column(&mut self) { pub const fn select_last_column(&mut self) {
self.select_column(Some(usize::MAX)); self.select_column(Some(usize::MAX));
} }

View File

@ -40,7 +40,7 @@ impl Default for AppState {
} }
} }
impl AppState { impl AppState {
fn select(&mut self, index: usize) { const fn select(&mut self, index: usize) {
self.list.select(Some(index)); self.list.select(Some(index));
self.table.select_cell(Some((index, index))); self.table.select_cell(Some((index, index)));
self.scrollbar = self.scrollbar.position(index); self.scrollbar = self.scrollbar.position(index);