RustAudio / vst-rs

VST 2.4 API implementation in rust. Create plugins or hosts. Previously rust-vst on the RustDSP group.
MIT License
1.04k stars 90 forks source link

Don't require Plugin to impl Default #153

Closed Boscop closed 3 years ago

Boscop commented 3 years ago

The fact that a plugin has to be Default makes it very unergonomic, especially if the plugin state contains many types that aren't Default, which are initialized in new() (such as Sender/Receiver), requiring wrapping them in Option etc.

Plugin::new only requires Default because PluginInstance impls Plugin but doesn't impl the Plugin::new method (PluginInstance is not Default)! PluginInstance has an inherent new method with a different signature: fn new(effect: *mut AEffect, lib: Arc<Library>) -> PluginInstance.

The options we have is to either impl Plugin::new for PluginInstance (it is never called anyway, (because Plugin::new is only called on the client side, but PluginInstance is only used on the host side), so we could just use unreachable!()) or moving the new method from Plugin into a new trait PluginWithNew: Plugin { fn new(host: HostCallback) -> Self; }.

askeksa commented 3 years ago

I've wanted to do this for so long. It is very unergonomic indeed.

It's a breaking change, since it now requires plugins to implement new, but most non-trivial plugins probably do that anyway, and it's very easy to adapt a plugin that doesn't.

We could consider, as a next step, removing the Default implementation for HostCallback, which was really only there to make it slightly less unergonomic to implement Default for a Plugin. This would save the check for the callback being initialized on every call and statically guard against that panic situation.

Review for PR coming up...

Boscop commented 3 years ago

@askeksa Yes, good point. Having HostCallback not Default also eliminates the possibility of forgetting to initialize it in new().