Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/slice/BitSlice.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ assert_eq!(immut, mutable);
You may borrow existing elements or slices with the following functions:

- [`from_element`] and [`from_element_mut`],
- [`from_bit`] and [`from_bit_mut`],
- [`from_slice`] and [`from_slice_mut`],
- [`try_from_slice`] and [`try_from_slice_mut`]

Expand Down
78 changes: 78 additions & 0 deletions src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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ę. 🇵🇱 🇵🇱 🇵🇱

index: crate::index::BitIdx<T::Mem>,
) -> &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

.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
Expand Down