@@ -1315,6 +1315,28 @@ impl<A: Allocator + Clone> RawTableInner<A> {
1315
1315
1316
1316
/// Sets a control byte, and possibly also the replicated control byte at
1317
1317
/// 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
1318
1340
#[ inline]
1319
1341
unsafe fn set_ctrl ( & self , index : usize , ctrl : u8 ) {
1320
1342
// Replicate the first Group::WIDTH control bytes at the end of
@@ -1335,8 +1357,12 @@ impl<A: Allocator + Clone> RawTableInner<A> {
1335
1357
// ---------------------------------------------
1336
1358
// | [A] | [B] | [EMPTY] | [EMPTY] | [A] | [B] |
1337
1359
// ---------------------------------------------
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`.
1338
1363
let index2 = ( ( index. wrapping_sub ( Group :: WIDTH ) ) & self . bucket_mask ) + Group :: WIDTH ;
1339
1364
1365
+ // SAFETY: The caller must uphold the safety rules for the [`RawTableInner::set_ctrl`]
1340
1366
* self . ctrl ( index) = ctrl;
1341
1367
* self . ctrl ( index2) = ctrl;
1342
1368
}
0 commit comments