Increment the counter by a specific amount. Clamps at the limit. Returns the actual amount the counter was incremented by.
(&mut self, amount: u32)
| 76 | |
| 77 | // Check if the counter is enabled |
| 78 | fn is_enabled(&self) -> bool { |
| 79 | self.enabled |
| 80 | } |
| 81 | |
| 82 | // Check if the counter has reached its limit |
| 83 | fn is_at_limit(&self) -> bool { |
| 84 | // Example of calling other &self methods |
| 85 | self.get_count() >= self.get_limit() |
| 86 | } |
| 87 | |
| 88 | // --- Methods taking &mut self (Mutable Access) --- |
| 89 | |
| 90 | // Increment the counter by 1, respecting the limit and enabled status. |
| 91 | // Returns true if incremented, false otherwise. |
| 92 | fn increment(&mut self) -> bool { |
| 93 | if !self.enabled { |
| 94 | // Not enabled, cannot increment |
| 95 | return false; |
| 96 | } |
| 97 | if self.is_at_limit() { // Calls &self method `is_at_limit` |
| 98 | // Already at limit, cannot increment |
| 99 | return false; |