Skip to content

Commit aa50543

Browse files
committed
Implement to_vec method for MidiMessage.
Although not efficient, it is suitable and convenient for things like unit tests.
1 parent 5c71cff commit aa50543

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ license = "MIT"
77
name = "wmidi"
88
readme = "README.md"
99
repository = "https://github.com/RustAudio/wmidi"
10-
version = "4.0.7"
10+
version = "4.0.8"
1111

1212
[lib]
1313
# Required to pass flags to criterion benchmark.

src/midi_message.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ impl<'a> TryFrom<&'a [u8]> for MidiMessage<'a> {
158158
}
159159

160160
impl<'a> MidiMessage<'a> {
161-
/// Construct a midi message from bytes. Use `MidiMessage::try_from(bytes)` instead.
162-
#[deprecated(since = "2.0.0", note = "Use MidiMessage::try_from instead.")]
161+
/// Construct a midi message from bytes.
163162
pub fn from_bytes(bytes: &'a [u8]) -> Result<Self, Error> {
164163
MidiMessage::try_from(bytes)
165164
}
@@ -356,6 +355,16 @@ impl<'a> MidiMessage<'a> {
356355
let data_bytes = unsafe { U7::from_bytes_unchecked(&bytes[1..end_i]) };
357356
Ok(MidiMessage::SysEx(data_bytes))
358357
}
358+
359+
/// Convert the message to a vector of bytes. Prefer using
360+
/// `copy_to_slice` if possible for better performance.
361+
#[cfg(feature = "std")]
362+
pub fn to_vec(&self) -> Vec<u8> {
363+
let mut data = vec![0; self.bytes_size()];
364+
// Unwrapping is ok as data has enough capacity for the data.
365+
self.copy_to_slice(&mut data).unwrap();
366+
data
367+
}
359368
}
360369

361370
#[cfg(feature = "std")]

0 commit comments

Comments
 (0)