dzamlo / rust-bitfield

This crate provides macros to generate bitfield-like struct.
Apache License 2.0
157 stars 19 forks source link

feature: API to set a range of bits using offset and length #30

Open rnestler opened 4 years ago

rnestler commented 4 years ago

I recently used bitfieldin a project where I found the API passing MSB and LSB a bit cumbersome: https://github.com/gfroerli/firmware/pull/82/files#diff-8ba2aa3932cb35175d51de283952144c82bcf45eef54f18f72f199650929ee80R24

It would have been more intuitive for me to pass a bit_offset and a bit_length: Like that: output.set_bit_range(*bit_index, Self::SIZE, self.0);

Would you consider adding such an API to this crate or do you think the idea is rubbish? :wink:

dzamlo commented 4 years ago

The BitRange trait was never really intended to be used as-is. The goal of this trait is the method can be called from the code generated by the macro, not by a developer. That explain the" quite a weird API".

But adding such a method would be quite simple. The hardest thing will be finding a name for this methods. Any suggestions ?

rnestler commented 4 years ago

The goal of this trait is the method can be called from the code generated by the macro, not by a developer.

So I'm using bitfield wrong / not as intended? Is there something already existing I could use instead?

But adding such a method would be quite simple. The hardest thing will be finding a name for this methods. Any suggestions?

Finding a good name is indeed hard here.

A few ideas:

Also maybe there is a better name than bit_index?

dzamlo commented 4 years ago

So I'm using bitfield wrong / not as intended?

The intended use of this crates is really to use the bitfield! macro with defined fields, and then you access the fields with the generated methods.

The current implementation of BitRange (and now BitRangeMut on master) is quite naive and is only fast when LLVM can do a good job. This depends heavily on the fact that the parameters are constant, last time I tested.

I didn't try it, but bitvec maybe more appropriate for your use case.

But if you find that this crates works better for you and adding that method is useful for you, I can do that.