Skip to content

Commit

Permalink
Implement to_vec method for MidiMessage.
Browse files Browse the repository at this point in the history
Although not efficient, it is suitable and convenient for things like
unit tests.
  • Loading branch information
wmedrano committed Aug 30, 2023
1 parent 5c71cff commit aa50543
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 11 additions & 2 deletions src/midi_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Error> {
MidiMessage::try_from(bytes)
}
Expand Down Expand Up @@ -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<u8> {
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")]
Expand Down

0 comments on commit aa50543

Please sign in to comment.