Skip to content

Commit

Permalink
Split Controller trait into types and behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde committed Aug 22, 2023
1 parent a2f0892 commit 89dba37
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
20 changes: 15 additions & 5 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ use std::future::Future;

/// Asynchronous context listener task.
///
/// Listens for changes in [`Controller::Context`] and updates the
/// corresponding hardware state accordingly, e.g. LEDs, screens,
/// Listens for changes in [`ControllerTypes::Context`] and updates
/// the corresponding hardware state accordingly, e.g. LEDs, screens,
/// or motorized jog wheels and faders.
pub type BoxedControllerTask = Box<dyn Future<Output = ()> + Send + 'static>;

pub trait Controller {
pub trait ControllerTypes {
type Context;
type InputEvent;
type ControlAction;
}

pub trait Controller {
type Types: ControllerTypes;

/// Attach a context listener task.
///
Expand All @@ -24,7 +28,10 @@ pub trait Controller {
///
/// Controllers that don't need a task may return `None`.
#[must_use]
fn attach_context_listener(&mut self, context: &Self::Context) -> Option<BoxedControllerTask>;
fn attach_context_listener(
&mut self,
context: &<Self::Types as ControllerTypes>::Context,
) -> Option<BoxedControllerTask>;

/// Map a generic input event into a control action.
///
Expand All @@ -35,5 +42,8 @@ pub trait Controller {
/// is unsupported and should be ignored or if it only affects the internal
/// state then `None` could be returned.
#[must_use]
fn map_input_event(&mut self, event: Self::InputEvent) -> Option<Self::ControlAction>;
fn map_input_event(
&mut self,
event: <Self::Types as ControllerTypes>::InputEvent,
) -> Option<<Self::Types as ControllerTypes>::ControlAction>;
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use std::{
};

mod controller;
pub use self::controller::{BoxedControllerTask, Controller};
pub use self::controller::{BoxedControllerTask, Controller, ControllerTypes};

pub mod devices;

Expand Down

0 comments on commit 89dba37

Please sign in to comment.