From aa5054357bb95a9bd5960cc9ffc459b55bacf0f5 Mon Sep 17 00:00:00 2001 From: "Will S. Medrano" Date: Tue, 29 Aug 2023 20:50:03 -0700 Subject: [PATCH] Implement to_vec method for MidiMessage. Although not efficient, it is suitable and convenient for things like unit tests. --- Cargo.toml | 2 +- src/midi_message.rs | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 08da45d..f7abef6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ license = "MIT" name = "wmidi" readme = "README.md" repository = "https://github.com/RustAudio/wmidi" -version = "4.0.7" +version = "4.0.8" [lib] # Required to pass flags to criterion benchmark. diff --git a/src/midi_message.rs b/src/midi_message.rs index 09ee803..a97a57d 100644 --- a/src/midi_message.rs +++ b/src/midi_message.rs @@ -158,8 +158,7 @@ impl<'a> TryFrom<&'a [u8]> for MidiMessage<'a> { } impl<'a> MidiMessage<'a> { - /// Construct a midi message from bytes. Use `MidiMessage::try_from(bytes)` instead. - #[deprecated(since = "2.0.0", note = "Use MidiMessage::try_from instead.")] + /// Construct a midi message from bytes. pub fn from_bytes(bytes: &'a [u8]) -> Result { MidiMessage::try_from(bytes) } @@ -356,6 +355,16 @@ impl<'a> MidiMessage<'a> { let data_bytes = unsafe { U7::from_bytes_unchecked(&bytes[1..end_i]) }; Ok(MidiMessage::SysEx(data_bytes)) } + + /// Convert the message to a vector of bytes. Prefer using + /// `copy_to_slice` if possible for better performance. + #[cfg(feature = "std")] + pub fn to_vec(&self) -> Vec { + let mut data = vec![0; self.bytes_size()]; + // Unwrapping is ok as data has enough capacity for the data. + self.copy_to_slice(&mut data).unwrap(); + data + } } #[cfg(feature = "std")]