-
Notifications
You must be signed in to change notification settings - Fork 130
Introduce BitSlice::from_bit{,_mut} methods #254
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
Open
mina86
wants to merge
1
commit into
ferrilab:main
Choose a base branch
from
mina86:c
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -210,6 +210,84 @@ where | |
| } | ||
| } | ||
|
|
||
| /// Constructs a shared `&BitSlice` reference over a single bit a shared | ||
| /// element. | ||
| /// | ||
| /// ## Parameters | ||
| /// | ||
| /// - `elem`: A shared reference to a memory element. | ||
| /// - `index`: the index of the bit. | ||
| /// | ||
| /// ## Returns | ||
| /// | ||
| /// A shared `&BitSlice` over a single bit of the `elem` at given | ||
| /// `index`. | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ```rust | ||
| /// use bitvec::prelude::*; | ||
| /// | ||
| /// let elem = 0b000_10000_u8; | ||
| /// let slice = BitSlice::<_, Msb0>::from_bit( | ||
| /// &elem, | ||
| /// bitvec::index::BitIdx::new(3).unwrap(), | ||
| /// ); | ||
| /// assert_eq!(bits![1], slice); | ||
| /// ``` | ||
| #[inline] | ||
| pub fn from_bit( | ||
| elem: &T, | ||
| index: crate::index::BitIdx<T::Mem>, | ||
| ) -> &Self { | ||
| unsafe { | ||
| BitPtr::new_unchecked(elem.into(), index) | ||
| .span_unchecked(1) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may wish to create a safe method |
||
| .into_bitslice_ref() | ||
| } | ||
| } | ||
|
|
||
| /// Constructs an exclusive `&mut BitSlice` reference over a single bit | ||
| /// of an element. | ||
| /// | ||
| /// ## Parameters | ||
| /// | ||
| /// - `elem`: An exclusive reference to a memory element. | ||
| /// - `index`: the index of the bit. | ||
| /// | ||
| /// ## Returns | ||
| /// | ||
| /// An exclusive `&mut BitSlice` over a single bit of the `elem` at given | ||
| /// `index`. | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ```rust | ||
| /// use bitvec::prelude::*; | ||
| /// | ||
| /// let mut elem = 0b000_10000_u8; | ||
| /// { | ||
| /// let slice = BitSlice::<_, Msb0>::from_bit_mut( | ||
| /// &mut elem, | ||
| /// bitvec::index::BitIdx::new(3).unwrap(), | ||
| /// ); | ||
| /// assert_eq!(bits![1], slice); | ||
| /// slice.set(0, false); | ||
| /// } | ||
| /// assert_eq!(0, elem); | ||
| /// ``` | ||
| #[inline] | ||
| pub fn from_bit_mut( | ||
| elem: &mut T, | ||
| index: crate::index::BitIdx<T::Mem>, | ||
| ) -> &mut Self { | ||
| unsafe { | ||
| BitPtr::new_unchecked(elem.into(), index) | ||
| .span_unchecked(1) | ||
| .into_bitslice_mut() | ||
| } | ||
| } | ||
|
|
||
| /// Constructs a shared `&BitSlice` reference over a slice of elements. | ||
| /// | ||
| /// The [`BitView`] trait, implemented on all `[T]` slices, provides a | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uhhh..... this is fine, but we should have
from_boolfor completeness just like std hasslice::from_refThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Such as
impl From<bool> for &BitSliceUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may add a
SAFETYcomment if you wishOr........ Just drop most of the unsafety by converting from a bool?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t follow. There cannot be a
BitSlice::<T,_>::from_bool(val: &bul)becauseBitSlice<T, _>requires a reference toT. There cannot be any kind of&bool → BitSlice<bool,_T>conversion either becausebooldoes not implementBitStore.Regarding safety, the code copies convention in
from_elementandfrom_element_mut.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but let me try to invent a hacky way to make this work
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mina86 I looked into this. The problem would be
BitSlice<Cell<_>, _>. So this is a failed experiment: 4cc3035Still, quite educational.
Contains more code, but none of the code is unsafe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I see. This is a separate feature than
from_bit{,_mut}though.from_bit{,_mut}reference a bit of an existing element. In particular, thefrom_bit_mutcannot be implemented viafrom_bool.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mina86 Agreed. Even more so that calling
slice.as_bitptr().to_raw_parts()would have you observe completely different / invalid values, even in the immutable case.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mina86 BTW, też jestem Polakiem. Będę zarządzał tą libką jak tylko potrafię. 🇵🇱 🇵🇱 🇵🇱