Skip to content

Stack::capacity takes &mut self in src/ctors.rs, forcing callers to hold a mutable borrow to read a const-generic constant #78

Description

@kreinba

The capacity method declared at src/ctors.rs:38-40 is pub fn capacity(&mut self) -> usize and its body simply returns the const-generic N. It reads no field and mutates nothing, but its signature forces every external caller to hold a mutable borrow on the stack just to read the compile-time capacity, which blocks any concurrent shared borrow and breaks several otherwise-natural call sites.

The signature also contradicts the rest of the API: len, is_empty, and other state-readers on Stack in src/stack.rs are correctly declared &self (and several are even const fn). Stack::try_push and Stack::try_pop in src/stack.rs:80-84 and src/stack.rs:117-121 already paper over the bug internally by only calling self.capacity() from inside methods that already hold &mut self. There is no scenario in which the receiver needs to be mutable for this method. Clippy's wrong_self_convention family is set up to flag exactly this pattern.

The fix is to change the signature to pub const fn capacity() -> usize { N } (associated function returning N directly) and update the two callers in src/stack.rs accordingly, or, if a method-style call is preferred for source compatibility, to pub const fn capacity(&self) -> usize { N }. Either form is a one-line change in src/ctors.rs plus two trivial call-site updates in src/stack.rs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions