Boddlnagg / midir

Cross-platform realtime MIDI processing in Rust.
MIT License
624 stars 78 forks source link

Allow to choose backend at runtime #4

Open Boddlnagg opened 9 years ago

Boddlnagg commented 9 years ago

E.g. compile with support for both ALSA and JACK, and give the application the ability to dynamically select the backend. It is not clear how this interacts with the jack feature-flag.

chris-zen commented 7 years ago

I guess that one first step for that to happen would be to exploit rust traits for the backends. Specifically common.rs should define traits for MidiInput, MidiOutput, MidiInputConnection and MidiOutputConnection. The next step would be the backends to implement those traits. And finally factory methods in common.rs could instantiate the user's selected backend implementations.

Does that sound right @Boddlnagg ?

Boddlnagg commented 7 years ago

I think there are two ways of doing this: (1) As you said, define traits and then use trait objects, which gives us dynamic dispatch automatically (2) Use an enum with one variant per backend, and the impl on the enum dispatches method calls to the right implementation.

I'm not sure which is better ... I think one disadvantage of (1) is that it requires boxing. I'm not sure of the performance impact, however, and aiming for an easy API might be the better goal. Maybe one should look at how other Rust libraries deal with dynamic backend selection (e.g. database abstractions, ...).

Boddlnagg commented 7 years ago

Using traits (option (1)) might also not be feasible due to object safety rules.

Boddlnagg commented 4 years ago

Another option might be https://crates.io/crates/trait-union