Skip to content

Commit ea4e5c2

Browse files
committed
BTree: evaluate static type-related check at compile time
1 parent 461e807 commit ea4e5c2

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
@@ -315,7 +315,7 @@ impl<BorrowType: marker::BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type>
315315
pub fn ascend(
316316
self,
317317
) -> Result<Handle<NodeRef<BorrowType, K, V, marker::Internal>, marker::Edge>, Self> {
318-
assert!(BorrowType::PERMITS_TRAVERSAL);
318+
let _ = BorrowType::TRAVERSAL_PERMIT;
319319
// We need to use raw pointers to nodes because, if BorrowType is marker::ValMut,
320320
// there might be outstanding mutable references to values that we must not invalidate.
321321
let leaf_ptr: *const _ = Self::as_leaf_ptr(&self);
@@ -986,7 +986,7 @@ impl<BorrowType: marker::BorrowType, K, V>
986986
/// `edge.descend().ascend().unwrap()` and `node.ascend().unwrap().descend()` should
987987
/// both, upon success, do nothing.
988988
pub fn descend(self) -> NodeRef<BorrowType, K, V, marker::LeafOrInternal> {
989-
assert!(BorrowType::PERMITS_TRAVERSAL);
989+
let _ = BorrowType::TRAVERSAL_PERMIT;
990990
// We need to use raw pointers to nodes because, if BorrowType is
991991
// marker::ValMut, there might be outstanding mutable references to
992992
// values that we must not invalidate. There's no worry accessing the
@@ -1637,15 +1637,17 @@ pub mod marker {
16371637
pub struct ValMut<'a>(PhantomData<&'a mut ()>);
16381638

16391639
pub trait BorrowType {
1640-
// Whether node references of this borrow type allow traversing
1641-
// to other nodes in the tree.
1642-
const PERMITS_TRAVERSAL: bool = true;
1640+
// If node references of this borrow type allow traversing to other
1641+
// nodes in the tree, this constant can be evaluated. Thus reading it
1642+
// serves as a compile-time assertion.
1643+
const TRAVERSAL_PERMIT: () = ();
16431644
}
16441645
impl BorrowType for Owned {
1645-
// Traversal isn't needed, it happens using the result of `borrow_mut`.
1646+
// Reject evaluation, because traversal isn't needed. Instead traversal
1647+
// happens using the result of `borrow_mut`.
16461648
// By disabling traversal, and only creating new references to roots,
16471649
// we know that every reference of the `Owned` type is to a root node.
1648-
const PERMITS_TRAVERSAL: bool = false;
1650+
const TRAVERSAL_PERMIT: () = panic!();
16491651
}
16501652
impl BorrowType for Dying {}
16511653
impl<'a> BorrowType for Immut<'a> {}

0 commit comments

Comments
 (0)