Skip to content

Introduce BitSlice::from_bit{,_mut} methods#254

Open
mina86 wants to merge 1 commit intoferrilab:mainfrom
mina86:c
Open

Introduce BitSlice::from_bit{,_mut} methods#254
mina86 wants to merge 1 commit intoferrilab:mainfrom
mina86:c

Conversation

@mina86
Copy link
Copy Markdown

@mina86 mina86 commented Dec 4, 2023

Introduce BitSlice::from_bit and BitSlice::from_bit_mut methods to
easily in an infallible way construct a reference to a single bit of
an element.

For the most part, this is just a convenience wrapper around calling
.get(n..=n) on a slice constructed via from_element{,_mut} method
however in certain situations it makes it possible to avoid unwrapping
of the returned Option.

Comment thread src/slice.rs
/// ```
#[inline]
pub fn from_bit(
elem: &T,
Copy link
Copy Markdown
Contributor

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_bool for completeness just like std has slice::from_ref

Copy link
Copy Markdown
Contributor

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 &BitSlice

Copy link
Copy Markdown
Contributor

@pczarn pczarn Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may add a SAFETY comment if you wish

Or........ Just drop most of the unsafety by converting from a bool?

Copy link
Copy Markdown
Author

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) because BitSlice<T, _> requires a reference to T. There cannot be any kind of &bool → BitSlice<bool,_T> conversion either because bool does not implement BitStore.

Regarding safety, the code copies convention in from_element and from_element_mut.

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

@pczarn pczarn Apr 24, 2026

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: 4cc3035

Still, quite educational.

Contains more code, but none of the code is unsafe.

Copy link
Copy Markdown
Author

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, the from_bit_mut cannot be implemented via from_bool.

Copy link
Copy Markdown
Contributor

@pczarn pczarn Apr 26, 2026

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.

Copy link
Copy Markdown
Contributor

@pczarn pczarn Apr 26, 2026

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ę. 🇵🇱 🇵🇱 🇵🇱

Introduce BitSlice::from_bit and BitSlice::from_bit_mut methods to
easily in an infallible way construct a reference to a single bit of
an element.

For the most part, this is just a convenience wrapper around calling
`.get(n..=n)` on a slice constructed via from_element{,_mut} method
however in certain situations it makes it possible to avoid unwrapping
of the returned Option.
Comment thread src/slice.rs
) -> &Self {
unsafe {
BitPtr::new_unchecked(elem.into(), index)
.span_unchecked(1)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may wish to create a safe method BitPtr::single_bit_span

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