You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sometimes a multi byte value is stored over a span of registers, e.g a 64bit value could be stored over 4 consecutive registers. When sending a request to write to such a register, one has to use WriteMultipleRegisters(Address, Data<'r>), which requires a data.
If the rust code is using the value as a 64 bit integer, it is quite cumbersome to convert it to &[u16] that is required by Data::from_words, the only way to construct a Data.
This PR proposes some TryFrom and From implementations to help with these situations. You can now write
Data::from(&0x12345678_u64.to_be_bytes())
Since From can only be implemented for even number of bytes, a helper macro is used to declare implementations up to [u8; 32] (similar how Default is implemented for arrays in the standard library).
One question: Why is constructing a zero length Data forbidden? I don't have a usecase, but should it be allowed?
I think that such auxiliary functions have nothing directly to do with Modbus and should rather be kept separate.
What do you mean by that? modbus-core enforces the use of Data in WriteMultipleRegisters(Address, Data<'r>), and the data member of Data is crate private. There is no way of adding this functionality from outside of the crate, except for jumping trough a bunch of hoops and u16 conversions with Data::from_words.
Sorry, that was my bad, I had forgotten that the fields of Data are not public.
So yes, your suggestion could make sense 🤔
Give me another chance to think about it before I merge this PR.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Sometimes a multi byte value is stored over a span of registers, e.g a 64bit value could be stored over 4 consecutive registers. When sending a request to write to such a register, one has to use
WriteMultipleRegisters(Address, Data<'r>), which requires a data.If the rust code is using the value as a 64 bit integer, it is quite cumbersome to convert it to
&[u16]that is required byData::from_words, the only way to construct aData.This PR proposes some
TryFromandFromimplementations to help with these situations. You can now writeSince
Fromcan only be implemented for even number of bytes, a helper macro is used to declare implementations up to[u8; 32](similar howDefaultis implemented for arrays in the standard library).One question: Why is constructing a zero length
Dataforbidden? I don't have a usecase, but should it be allowed?