Skip to content

Commit e3148dc

Browse files
Rollup merge of #95005 - ssomers:btree_static_assert, r=thomcc
BTree: evaluate static type-related check at compile time `assert`s like the ones replaced here would only go off when you run the right test cases, if the code were ever incorrectly changed such that rhey would trigger. But [inspired on a nice forum question](https://users.rust-lang.org/t/compile-time-const-generic-parameter-check/69202), they can be checked at compile time.
2 parents 983f4da + ea4e5c2 commit e3148dc

File tree

1 file changed

+9
-7
lines changed
  • library/alloc/src/collections/btree

1 file changed

+9
-7
lines changed

library/alloc/src/collections/btree/node.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ impl<BorrowType: marker::BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type>
318318
pub fn ascend(
319319
self,
320320
) -> Result<Handle<NodeRef<BorrowType, K, V, marker::Internal>, marker::Edge>, Self> {
321-
assert!(BorrowType::PERMITS_TRAVERSAL);
321+
let _ = BorrowType::TRAVERSAL_PERMIT;
322322
// We need to use raw pointers to nodes because, if BorrowType is marker::ValMut,
323323
// there might be outstanding mutable references to values that we must not invalidate.
324324
let leaf_ptr: *const _ = Self::as_leaf_ptr(&self);
@@ -1003,7 +1003,7 @@ impl<BorrowType: marker::BorrowType, K, V>
10031003
/// `edge.descend().ascend().unwrap()` and `node.ascend().unwrap().descend()` should
10041004
/// both, upon success, do nothing.
10051005
pub fn descend(self) -> NodeRef<BorrowType, K, V, marker::LeafOrInternal> {
1006-
assert!(BorrowType::PERMITS_TRAVERSAL);
1006+
let _ = BorrowType::TRAVERSAL_PERMIT;
10071007
// We need to use raw pointers to nodes because, if BorrowType is
10081008
// marker::ValMut, there might be outstanding mutable references to
10091009
// values that we must not invalidate. There's no worry accessing the
@@ -1666,15 +1666,17 @@ pub mod marker {
16661666
pub struct ValMut<'a>(PhantomData<&'a mut ()>);
16671667

16681668
pub trait BorrowType {
1669-
// Whether node references of this borrow type allow traversing
1670-
// to other nodes in the tree.
1671-
const PERMITS_TRAVERSAL: bool = true;
1669+
// If node references of this borrow type allow traversing to other
1670+
// nodes in the tree, this constant can be evaluated. Thus reading it
1671+
// serves as a compile-time assertion.
1672+
const TRAVERSAL_PERMIT: () = ();
16721673
}
16731674
impl BorrowType for Owned {
1674-
// Traversal isn't needed, it happens using the result of `borrow_mut`.
1675+
// Reject evaluation, because traversal isn't needed. Instead traversal
1676+
// happens using the result of `borrow_mut`.
16751677
// By disabling traversal, and only creating new references to roots,
16761678
// we know that every reference of the `Owned` type is to a root node.
1677-
const PERMITS_TRAVERSAL: bool = false;
1679+
const TRAVERSAL_PERMIT: () = panic!();
16781680
}
16791681
impl BorrowType for Dying {}
16801682
impl<'a> BorrowType for Immut<'a> {}

0 commit comments

Comments
 (0)