debouncr icon indicating copy to clipboard operation
debouncr copied to clipboard

Add generic DebouncerExt trait

Open everdrone opened this issue 11 months ago • 0 comments

This PR addresses the need for a generic Debouncer<T, M> implementation by introducing a trait around all RepeatN types. This trait encapsulates the correct associated type for each RepeatN.

For example, Repeat4, when passed to Debouncer<T, M>, now automatically provides T through its associated type. As a result, Debouncer<u8, Repeat4> can now be simplified to Debouncer<Repeat4>.

impl RepeatN for Repeat4 {
    type Type = u8;
}

Additionally, a DebouncerExt trait has been implemented for all debouncers. This enables all the separate initializers, such as debounce_n(bool), to be unified under DebouncerExt::new(bool). This change allows users to define a generic Debouncer<M> in functions and types via type parameters.

struct Button<T: DebouncerExt> {
    debouncer: T,
}

impl<T: DebouncerExt> Button<T> {
    pub fn new(initial_state: bool) -> Self {
        Self {
            debouncer: T::new(initial_state),
        }
    }

    pub fn update(&mut self, pressed: bool) -> Option<Edge> {
        self.debouncer.update(pressed)
    }
}

This is just a draft. I have only made minimal edits to the documentation to ensure the tests pass and to present this PR as a proposal.

everdrone avatar Jan 10 '25 14:01 everdrone