Skip to content

impl: add {set,clear}_range#21

Open
pskrgag wants to merge 1 commit intobodil:masterfrom
pskrgag:master
Open

impl: add {set,clear}_range#21
pskrgag wants to merge 1 commit intobodil:masterfrom
pskrgag:master

Conversation

@pskrgag
Copy link
Copy Markdown

@pskrgag pskrgag commented Dec 10, 2022

Setting bits with for loop one by one is not performant. Creating mask for bit_and/bit_or to achieve the same goal is not trivial task especially for "big" bitmasks.

To make life easier let's introduce new methods to set and clear range of bits by creating needed mask

Signed-off-by: Pavel Skripkin paskripkin@gmail.com

Setting bits with for loop one by one is not performant. Creating
mask for bit_and/bit_or to achive the same goal is not trivial task
especially for "big" bitmasks.

To make life easier let's introduce new methods to set and clear range
of bits by creating needed mask

Signed-off-by: Pavel Skripkin <paskripkin@gmail.com>
@arlyon
Copy link
Copy Markdown

arlyon commented Dec 12, 2022

Would it make sense instead to support a RangeBound?

@pskrgag
Copy link
Copy Markdown
Author

pskrgag commented Dec 14, 2022

@arlyon Thank you for taking a look!

I am far from expert in Rust :( I don't get how implementing this trait would help solving set/clear for bit range. IIUC this trait allows to find range bounds, so it may help with finding if bit is set. However I don't see how to manipulate ranges via this trait

I wound appreciate if you will help me understand your suggestion.

Thanks!

@arlyon
Copy link
Copy Markdown

arlyon commented Dec 20, 2022

RangeBound is implemented by 0..3 (Range) 0..=3 (RangeInclusive), ..4 (RangeTo), 5.. (RangeFrom) and .. (RangeFull) so something that is generic over these would allow you to do any of the following:

bytes.set_range(..);
bytes.clear_range(..);
bytes.set_range(..5);
bytes.clear_range(3..=6);

You can then choose which syntax you want based on the situation (and also removes the need to check whether the method is inclusive or not!). Then set_range and clear_range checks the lower and upper bound on the range you give it to determine which bits to set or clear.

@pskrgag
Copy link
Copy Markdown
Author

pskrgag commented Dec 21, 2022

@arlyon Thank you so much for explanations!

Your idea is clear to me now, and I think it will make code more "rusty". I will update the PR soon.

Thanks!

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.

2 participants