Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
78 changes: 78 additions & 0 deletions src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,84 @@ where
pub unsafe fn set_aliased_unchecked(&self, index: usize, value: bool) {
self.as_bitptr().add(index).freeze().frozen_write_bit(value);
}

/// Replaces the value of a single bit, using alias-safe operations,
/// returning the previous value.
///
/// This is equivalent to [`.replace()`], except that it does not require an
/// `&mut` reference, and allows bit-slices with alias-safe storage to share
/// write permissions.
///
/// ## Parameters
///
/// - `&self`: This method only exists on bit-slices with alias-safe
/// storage, and so does not require exclusive access.
/// - `index`: The bit index to set. It must be in `0 .. self.len()`.
/// - `value`: The new bit-value to write into the bit at `index`.
///
/// ## Panics
///
/// This panics if `index` is out of bounds.
///
/// ## Examples
///
/// ```rust
/// use bitvec::prelude::*;
/// use core::cell::Cell;
///
/// let bits: &BitSlice<_, _> = bits![Cell<usize>, Lsb0; 0, 1];
/// assert!(!bits.replace_aliased(0, true));
/// assert!(bits.replace_aliased(1, false));
///
/// assert_eq!(bits, bits![1, 0]);
/// ```
///
/// [`.replace()`]: Self::replace
#[inline]
pub fn replace_aliased(&self, index: usize, value: bool) -> bool {
self.assert_in_bounds(index, 0..self.len());
unsafe { self.replace_aliased_unchecked(index, value) }
}

/// Replaces the value of a single bit, using alias-safe operations and
/// without bounds checking, returning the previous value.
///
/// This is equivalent to [`.replace_unchecked()`], except that it does not
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 link to fn replace_unchecked, but you do not link to fn replace_aliased. Please do that.

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.

Hi Peter, would you mind briefly elaborating on what you mean exactly, or providing a code suggestion? I couldn't find any equivalent cross linking from .replace_unchecked() to .replace() to follow suit.

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.

It would be nice to have, regardless of what existing docs say:

This is for standard slice:

"For a safe alternative see get."

Screenshot_20260418_133151_Brave.jpg

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.

Vincent, you may update set_aliased_unchecked and replace_unchecked as well

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've updated all unsafe _unchecked methods I could find, accordingly.

/// require an `&mut` reference, and allows bit-slices with alias-safe
/// storage to share write permissions.
///
/// ## Parameters
///
/// - `&self`: This method only exists on bit-slices with alias-safe
/// storage, and so does not require exclusive access.
/// - `index`: The bit index to replace. It must be in `0 .. self.len()`.
/// - `value`: The new bit-value to write into the bit at `index`.
///
/// ## Safety
///
/// The caller must ensure that `index` is not out of bounds.
///
/// ## Examples
///
/// ```rust
/// use bitvec::prelude::*;
/// use core::cell::Cell;
///
/// let data = Cell::new(0u8);
/// let bits = &data.view_bits::<Lsb0>()[.. 2];
/// assert!(!unsafe { bits.replace_aliased_unchecked(3, true) });
/// assert_eq!(data.get(), 8);
/// ```
///
/// [`.replace_unchecked()`]: Self::replace_unchecked
#[inline]
pub unsafe fn replace_aliased_unchecked(
&self,
index: usize,
value: bool,
) -> bool {
self.as_bitptr().add(index).freeze().frozen_write_bit(value)
}
}

/// Miscellaneous information.
Expand Down
6 changes: 6 additions & 0 deletions src/slice/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ fn aliasing() {
assert!(bits[0]);
b.set_aliased(0, false);
assert!(!bits[0]);

let (a, b) = (bits, bits);
assert!(!a.replace_aliased(0, true));
assert!(bits[0]);
assert!(b.replace_aliased(0, false));
assert!(!bits[0]);
}

#[test]
Expand Down