Skip to content

Commit 5e4a982

Browse files
committed
Auto merge of #394 - JustForFun88:raw_table_inner_set_ctrl, r=Amanieu
Doc `RawTableInner::set_ctrl`
2 parents 96522e0 + bcfdd49 commit 5e4a982

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/raw/mod.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,28 @@ impl<A: Allocator + Clone> RawTableInner<A> {
13151315

13161316
/// Sets a control byte, and possibly also the replicated control byte at
13171317
/// the end of the array.
1318+
///
1319+
/// This function does not make any changes to the `data` parts of the table,
1320+
/// or any changes to the the `items` or `growth_left` field of the table.
1321+
///
1322+
/// # Safety
1323+
///
1324+
/// You must observe the following safety rules when calling this function:
1325+
///
1326+
/// * The [`RawTableInner`] has already been allocated;
1327+
///
1328+
/// * The `index` must not be greater than the `RawTableInner.bucket_mask`, i.e.
1329+
/// `index <= RawTableInner.bucket_mask` or, in other words, `(index + 1)` must
1330+
/// be no greater than the number returned by the function [`RawTableInner::buckets`].
1331+
///
1332+
/// Calling this function on a table that has not been allocated results in [`undefined behavior`].
1333+
///
1334+
/// See also [`Bucket::as_ptr`] method, for more information about of properly removing
1335+
/// or saving `data element` from / into the [`RawTable`] / [`RawTableInner`].
1336+
///
1337+
/// [`RawTableInner::buckets`]: RawTableInner::buckets
1338+
/// [`Bucket::as_ptr`]: Bucket::as_ptr
1339+
/// [`undefined behavior`]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
13181340
#[inline]
13191341
unsafe fn set_ctrl(&self, index: usize, ctrl: u8) {
13201342
// Replicate the first Group::WIDTH control bytes at the end of
@@ -1335,8 +1357,12 @@ impl<A: Allocator + Clone> RawTableInner<A> {
13351357
// ---------------------------------------------
13361358
// | [A] | [B] | [EMPTY] | [EMPTY] | [A] | [B] |
13371359
// ---------------------------------------------
1360+
1361+
// This is the same as `(index.wrapping_sub(Group::WIDTH)) % self.buckets() + Group::WIDTH`
1362+
// because the number of buckets is a power of two, and `self.bucket_mask = self.buckets() - 1`.
13381363
let index2 = ((index.wrapping_sub(Group::WIDTH)) & self.bucket_mask) + Group::WIDTH;
13391364

1365+
// SAFETY: The caller must uphold the safety rules for the [`RawTableInner::set_ctrl`]
13401366
*self.ctrl(index) = ctrl;
13411367
*self.ctrl(index2) = ctrl;
13421368
}

0 commit comments

Comments
 (0)