Skip to content

Commit 7dd8a84

Browse files
committed
introduce the Comparable trait for btree internals
1 parent d198b85 commit 7dd8a84

File tree

4 files changed

+24
-28
lines changed

4 files changed

+24
-28
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::borrow::Borrow;
1+
use core::cmp::Comparable;
22
use core::ops::RangeBounds;
33
use core::{hint, ptr};
44

@@ -267,7 +267,7 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
267267
) -> LeafRange<BorrowType, K, V>
268268
where
269269
Q: Ord,
270-
K: Borrow<Q>,
270+
K: Comparable<Q>,
271271
R: RangeBounds<Q>,
272272
{
273273
match self.search_tree_for_bifurcation(&range) {
@@ -316,7 +316,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::LeafOrInternal>
316316
pub(super) fn range_search<Q, R>(self, range: R) -> LeafRange<marker::Immut<'a>, K, V>
317317
where
318318
Q: ?Sized + Ord,
319-
K: Borrow<Q>,
319+
K: Comparable<Q>,
320320
R: RangeBounds<Q>,
321321
{
322322
// SAFETY: our borrow type is immutable.
@@ -342,7 +342,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::ValMut<'a>, K, V, marker::LeafOrInternal>
342342
pub(super) fn range_search<Q, R>(self, range: R) -> LeafRange<marker::ValMut<'a>, K, V>
343343
where
344344
Q: ?Sized + Ord,
345-
K: Borrow<Q>,
345+
K: Comparable<Q>,
346346
R: RangeBounds<Q>,
347347
{
348348
unsafe { self.find_leaf_edges_spanning_range(range) }
@@ -746,8 +746,7 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
746746
mut bound: SearchBound<&Q>,
747747
) -> Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>
748748
where
749-
Q: Ord,
750-
K: Borrow<Q>,
749+
K: Comparable<Q>,
751750
{
752751
let mut node = self;
753752
loop {
@@ -769,8 +768,7 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
769768
mut bound: SearchBound<&Q>,
770769
) -> Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::Edge>
771770
where
772-
Q: Ord,
773-
K: Borrow<Q>,
771+
K: Comparable<Q>,
774772
{
775773
let mut node = self;
776774
loop {

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use core::borrow::Borrow;
2-
use core::cmp::Ordering;
1+
use core::cmp::{Comparable, Ordering};
32
use core::ops::{Bound, RangeBounds};
43

54
use SearchBound::*;
@@ -51,8 +50,7 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
5150
key: &Q,
5251
) -> SearchResult<BorrowType, K, V, marker::LeafOrInternal, marker::Leaf>
5352
where
54-
Q: Ord,
55-
K: Borrow<Q>,
53+
K: Comparable<Q>,
5654
{
5755
loop {
5856
self = match self.search_node(key) {
@@ -95,7 +93,7 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
9593
>
9694
where
9795
Q: Ord,
98-
K: Borrow<Q>,
96+
K: Comparable<Q>,
9997
R: RangeBounds<Q>,
10098
{
10199
// Determine if map or set is being searched
@@ -161,8 +159,8 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
161159
bound: SearchBound<&'r Q>,
162160
) -> (Handle<Self, marker::Edge>, SearchBound<&'r Q>)
163161
where
164-
Q: ?Sized + Ord,
165-
K: Borrow<Q>,
162+
Q: ?Sized,
163+
K: Comparable<Q>,
166164
{
167165
let (edge_idx, bound) = self.find_lower_bound_index(bound);
168166
let edge = unsafe { Handle::new_edge(self, edge_idx) };
@@ -175,8 +173,8 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
175173
bound: SearchBound<&'r Q>,
176174
) -> (Handle<Self, marker::Edge>, SearchBound<&'r Q>)
177175
where
178-
Q: ?Sized + Ord,
179-
K: Borrow<Q>,
176+
Q: ?Sized,
177+
K: Comparable<Q>,
180178
{
181179
let (edge_idx, bound) = unsafe { self.find_upper_bound_index(bound, 0) };
182180
let edge = unsafe { Handle::new_edge(self, edge_idx) };
@@ -197,8 +195,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
197195
key: &Q,
198196
) -> SearchResult<BorrowType, K, V, Type, Type>
199197
where
200-
Q: Ord,
201-
K: Borrow<Q>,
198+
K: Comparable<Q>,
202199
{
203200
match unsafe { self.find_key_index(key, 0) } {
204201
IndexResult::KV(idx) => Found(unsafe { Handle::new_kv(self, idx) }),
@@ -216,17 +213,16 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
216213
/// `start_index` must be a valid edge index for the node.
217214
unsafe fn find_key_index<Q: ?Sized>(&self, key: &Q, start_index: usize) -> IndexResult
218215
where
219-
Q: Ord,
220-
K: Borrow<Q>,
216+
K: Comparable<Q>,
221217
{
222218
let node = self.reborrow();
223219
let keys = node.keys();
224220
debug_assert!(start_index <= keys.len());
225221
for (offset, k) in unsafe { keys.get_unchecked(start_index..) }.iter().enumerate() {
226-
match key.cmp(k.borrow()) {
227-
Ordering::Greater => {}
222+
match k.compare(key) {
223+
Ordering::Less => {}
228224
Ordering::Equal => return IndexResult::KV(start_index + offset),
229-
Ordering::Less => return IndexResult::Edge(start_index + offset),
225+
Ordering::Greater => return IndexResult::Edge(start_index + offset),
230226
}
231227
}
232228
IndexResult::Edge(keys.len())
@@ -242,8 +238,8 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
242238
bound: SearchBound<&'r Q>,
243239
) -> (usize, SearchBound<&'r Q>)
244240
where
245-
Q: ?Sized + Ord,
246-
K: Borrow<Q>,
241+
Q: ?Sized,
242+
K: Comparable<Q>,
247243
{
248244
match bound {
249245
Included(key) => match unsafe { self.find_key_index(key, 0) } {
@@ -270,8 +266,8 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
270266
start_index: usize,
271267
) -> (usize, SearchBound<&'r Q>)
272268
where
273-
Q: ?Sized + Ord,
274-
K: Borrow<Q>,
269+
Q: ?Sized,
270+
K: Comparable<Q>,
275271
{
276272
match bound {
277273
Included(key) => match unsafe { self.find_key_index(key, start_index) } {

library/alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
#![feature(char_max_len)]
108108
#![feature(clone_to_uninit)]
109109
#![feature(coerce_unsized)]
110+
#![feature(comparable_trait)]
110111
#![feature(const_default)]
111112
#![feature(const_eval_select)]
112113
#![feature(const_heap)]

library/alloctests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#![feature(assert_matches)]
2121
#![feature(char_internals)]
2222
#![feature(char_max_len)]
23+
#![feature(comparable_trait)]
2324
#![feature(core_intrinsics)]
2425
#![feature(exact_size_is_empty)]
2526
#![feature(extend_one)]

0 commit comments

Comments
 (0)