chris-zen / coremidi

CoreMIDI library for Rust
https://chris-zen.github.io/coremidi/coremidi/
MIT License
75 stars 20 forks source link

Correct lifetime for callbacks #3

Closed raphlinus closed 7 years ago

raphlinus commented 7 years ago

Stores the callbacks (for notifications and input both) in boxes, so both the function and its destructor can be type-erased. Also changes the basic callback type to FnMut(&Data) + Send + 'static, to indicate that the callback can be called from a different thread and after the function registering the callback returns, and that the callback doesn't get to hold any references to the data.

Also changes the signature of Packet to just yield a data slice; the old approach of returning an iterator was unsafe because the iterator could outlive the data pointer. Some packet types get lifetimes and PhantomData enforcing those lifetimes as well.

Increments the version to 0.2.0, is the change to the callback and packet types is clearly breaking from a semver perspective.

Fixes #2

chris-zen commented 7 years ago

@raphlinus Thank you very much for this great contribution. I am sorry I couldn't spend time on it before. To be honest, this was my first time with rust and C binding, and I was not sure about how to implement callbacks and manage the lifetimes properly. I have learn several things from your work ;-)

I'll merge and publish to crates.io

@raphlinus I'd love to see what projects you are using this lib with if they are open ;-)

raphlinus commented 7 years ago

@chris-zen I plan to publish my virtual-analog synthesizer soon. It's currently very basic, but I think the ideas behind it are solid - see https://github.com/RustAudio/dsp-chain/issues/141 for some discussion.

Thanks for merging and pushing! No worries about the delay, I'm currently juggling way too many projects, so know what it's like.