Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature | Improve array constructors #19

Merged
merged 8 commits into from
May 14, 2024

Conversation

BenLeadbetter
Copy link
Collaborator

@BenLeadbetter BenLeadbetter commented May 14, 2024

Context

In order to create a messages backed by a simple array (a pretty common use case) we had dedicated new_arr and new_arr_bytes constructors which would construct messages backed by [u32; 4] and [u8; 3] respectively. This was ok, but if I wanted to create a message backed with a [u32; 2] I would need to use try_new, even though I know at compile time that the buffer would be large enough to fit the message.

Additionally, the conversion traits FromUmp, IntoUmp, FromBytes, IntoBytes, RebufferFrom and RebufferInto would only be available in their fallible versions if the target message was an array because we didn't have compile-time checks on the minimum message size.

The Changes

new

All messages, when backed by an array type buffer have a new new constructor specialisation. The size of the target array is checked at compile time, so that new will be valid so long as the array has a size larger than the messages minimum representable size.

let sysex = Sysex7::<[u32; 2]>::new();
let big_sysex = Sysex7::<[u32; 16]>::new();
let small_sysex = Sysex7::<[u32; 1]>::new(); // won't compile

⚠️ Breaking Change ⚠️

We remove the new_arr and new_arr_bytes constructors in favour of using the more flexible and more consistent new constructor on the explicit array generic specialisation.

let message_128 = ChannelPressure::<[u32; 4]>::new();
let message_64 = ChannelPressure::<[u32; 2]>::new();

Conversion Traits

When message is fixed size and the target message is array-backed, and the array has a size greater than the smallest representable size for the message, the conversion traits FromUmp, IntoUmp, FromBytes, IntoBytes, RebufferFrom and RebufferInto can be used in their non-fallible forms.

let message = ChannelPressure::<[u8; 3]>::new();
let message: ChannelPressure<[u32; 4]> = message.into_ump();

@BenLeadbetter BenLeadbetter merged commit f24cb00 into develop May 14, 2024
3 checks passed
@BenLeadbetter BenLeadbetter deleted the feature/improve-array-constructors branch May 14, 2024 04:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant