Introduce BitSlice::from_bit{,_mut} methods#254
Conversation
| /// ``` | ||
| #[inline] | ||
| pub fn from_bit( | ||
| elem: &T, |
There was a problem hiding this comment.
uhhh..... this is fine, but we should have from_bool for completeness just like std has slice::from_ref
There was a problem hiding this comment.
Such as impl From<bool> for &BitSlice
There was a problem hiding this comment.
You may add a SAFETY comment if you wish
Or........ Just drop most of the unsafety by converting from a bool?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Sure, but let me try to invent a hacky way to make this work
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
@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.
| ) -> &Self { | ||
| unsafe { | ||
| BitPtr::new_unchecked(elem.into(), index) | ||
| .span_unchecked(1) |
There was a problem hiding this comment.
You may wish to create a safe method BitPtr::single_bit_span
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} methodhowever in certain situations it makes it possible to avoid unwrapping
of the returned Option.