From 611dc452ecf1e57f1bef3133ae42416ce65c3432 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Tue, 31 Jan 2023 18:57:49 +0100 Subject: [PATCH] Add conversion functions on SysExMessage trait --- src/midi/sysex.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/midi/sysex.rs b/src/midi/sysex.rs index 68286bc8..7a81f317 100644 --- a/src/midi/sysex.rs +++ b/src/midi/sysex.rs @@ -7,10 +7,29 @@ pub trait SysExMessage: Debug + Clone + PartialEq + Send + Sync { /// The maximum SysEx message size, in bytes. const MAX_BUFFER_SIZE: usize; - // TODO: Conversion functions + /// Read a SysEx message from `buffer` and convert it to this message type if supported. + /// `buffer`'s length matches the received message. It is not padded to `MAX_BUFFER_SIZE` bytes. + fn from_buffer(buffer: &[u8]) -> Option; + + /// Serialize this message object as a SysEx message in `buffer`, returning the message's length + /// in bytes. + /// + /// `buffer` is a `[u8; Self::MAX_BUFFER_SIZE]`, but Rust currently doesn't allow using + /// associated constants in method types: + /// + /// + fn to_buffer(self, buffer: &mut [u8]) -> usize; } /// A default implementation plugins that don't need SysEx support can use. impl SysExMessage for () { const MAX_BUFFER_SIZE: usize = 0; + + fn from_buffer(_buffer: &[u8]) -> Option { + None + } + + fn to_buffer(self, _buffer: &mut [u8]) -> usize { + 0 + } }