pub trait TryIntoBytes<T> {
// Required method
fn try_into_bytes(self) -> Result<T, BufferOverflow>;
}Expand description
Attempt to convert a Ump backed message into a Bytes backed message.
The conversion may fail with a BufferOverflow error if the target buffer is not large enough to contain the data.
Note that in most cases this is a “lossy” conversion. Some
fields in a Ump message are not represented in a Bytes
message like group, for example.
use midi2::{TryIntoBytes, Data, sysex7::Sysex7, error::BufferOverflow};
let ump_message = Sysex7::try_from(&[
0x3016_0001_u32,
0x0203_0405,
0x3034_0607,
0x0809_0000,
][..]).expect("Valid data");
let bytes_message: Result<Sysex7<[u8; 12]>, BufferOverflow> = ump_message
.clone()
.try_into_bytes();
assert_eq!(bytes_message.expect("Buffer is large enough").data(), &[
0xF0, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xF7,
]);
let small_bytes_message: Result<Sysex7<[u8; 10]>, BufferOverflow> = ump_message
.try_into_bytes()
.clone();
small_bytes_message.expect_err("Buffer is too small");This is the reciprocal trait to TryFromUmp. Any implementer of TryFromUmp automatically implements TryIntoBytes, similar to the core::convert::TryInto trait.