Skip to content

Commit 285778e

Browse files
committed
Bump Rust edition
1 parent 98b199b commit 285778e

File tree

9 files changed

+40
-52
lines changed

9 files changed

+40
-52
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
authors = ["Sean Leffler <sean@errno.com>"]
33
name = "qp-trie"
44
version = "0.8.2"
5-
5+
edition = "2021"
66
description = "An idiomatic and fast QP-trie implementation in pure Rust, written with an emphasis on safety."
77

88
documentation = "https://docs.rs/qp-trie"
@@ -23,7 +23,6 @@ travis-ci = { repository = "sdleffler/qp-trie-rs", branch = "master" }
2323
[dependencies]
2424
new_debug_unreachable = "1.0.1"
2525
serde = { version = "1.0.11", optional = true, features = ["derive"] }
26-
unreachable = "1.0.0"
2726

2827
[dev-dependencies]
2928
bincode = "1.0"

src/entry.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ use core::borrow::Borrow;
22
use core::marker::PhantomData;
33
use core::mem;
44

5-
use unreachable::UncheckedOptionExt;
6-
7-
use node::{Leaf, Node};
8-
use util::nybble_get_mismatch;
5+
use crate::node::{Leaf, Node};
6+
use crate::util::nybble_get_mismatch;
97

108
pub fn make_entry<'a, K: 'a + Borrow<[u8]>, V: 'a>(
119
key: K,
@@ -28,7 +26,7 @@ pub enum Entry<'a, K: 'a, V: 'a> {
2826
impl<'a, K: 'a + Borrow<[u8]>, V: 'a> Entry<'a, K, V> {
2927
fn nonempty(key: K, root: &'a mut Option<Node<K, V>>, count: &'a mut usize) -> Entry<'a, K, V> {
3028
let (exemplar_ptr, mismatch) = {
31-
let node = unsafe { root.as_mut().unchecked_unwrap() };
29+
let node = unsafe { root.as_mut().unwrap_unchecked() };
3230
let exemplar = node.get_exemplar_mut(key.borrow());
3331
let mismatch = nybble_get_mismatch(exemplar.key_slice(), key.borrow());
3432
(exemplar as *mut Leaf<K, V>, mismatch)
@@ -38,7 +36,7 @@ impl<'a, K: 'a + Borrow<[u8]>, V: 'a> Entry<'a, K, V> {
3836
None => Entry::occupied(exemplar_ptr, root as *mut Option<Node<K, V>>, count),
3937

4038
Some((b, i)) => {
41-
let node = unsafe { root.as_mut().unchecked_unwrap() };
39+
let node = unsafe { root.as_mut().unwrap_unchecked() };
4240

4341
Entry::vacant_nonempty(key, i, b, node, count)
4442
}
@@ -143,7 +141,7 @@ impl<'a, K: 'a + Borrow<[u8]>, V: 'a> VacantEntry<'a, K, V> {
143141

144142
*root = Some(Node::Leaf(Leaf::new(self.key, val)));
145143
let root_mut_opt = root.as_mut();
146-
let leaf_mut = unsafe { root_mut_opt.unchecked_unwrap().unwrap_leaf_mut() };
144+
let leaf_mut = unsafe { root_mut_opt.unwrap_unchecked().unwrap_leaf_mut() };
147145
&mut leaf_mut.val
148146
}
149147
VacantEntryInner::Internal(graft, graft_nybble, node) => {
@@ -177,20 +175,20 @@ impl<'a, K: 'a + Borrow<[u8]>, V: 'a> OccupiedEntry<'a, K, V> {
177175
match *root {
178176
Some(Node::Leaf(_)) => {
179177
let leaf_opt = root.take();
180-
let leaf = unsafe { leaf_opt.unchecked_unwrap().unwrap_leaf() };
178+
let leaf = unsafe { leaf_opt.unwrap_unchecked().unwrap_leaf() };
181179

182180
debug_assert!(leaf.key_slice() == self.key().borrow());
183181
(leaf.key, leaf.val)
184182
}
185183

186184
Some(Node::Branch(_)) => {
187185
let branch_opt = root.as_mut();
188-
let branch = unsafe { branch_opt.unchecked_unwrap() };
186+
let branch = unsafe { branch_opt.unwrap_unchecked() };
189187

190188
let leaf_opt = branch.remove_validated(self.key().borrow());
191189

192190
debug_assert!(leaf_opt.is_some());
193-
let leaf = unsafe { leaf_opt.unchecked_unwrap() };
191+
let leaf = unsafe { leaf_opt.unwrap_unchecked() };
194192

195193
(leaf.key, leaf.val)
196194
}

src/iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use alloc::{vec, vec::Vec};
22

3-
use node::Node;
3+
use crate::node::Node;
44

55
/// An iterator over the keys and values in a QP-trie.
66
#[derive(Clone, Debug)]

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ extern crate alloc;
44

55
#[macro_use]
66
extern crate debug_unreachable;
7-
extern crate unreachable;
87

98
#[cfg(feature = "serde")]
109
#[macro_use]

src/node.rs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ use core::borrow::Borrow;
22
use core::fmt;
33
use core::mem;
44

5-
use unreachable::UncheckedOptionExt;
6-
7-
use iter::{IntoIter, Iter, IterMut};
8-
use sparse::Sparse;
9-
use util::{nybble_index, nybble_mismatch};
5+
use crate::iter::{IntoIter, Iter, IterMut};
6+
use crate::sparse::Sparse;
7+
use crate::util::{nybble_index, nybble_mismatch};
108

119
// A leaf in the trie.
1210
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -82,27 +80,26 @@ impl<K: Borrow<[u8]>, V> Branch<K, V> {
8280
pub fn entry_mut(&mut self, index: u8) -> &mut Node<K, V> {
8381
let entry = self.entries.get_mut(index);
8482
debug_assert!(entry.is_some());
85-
unsafe { entry.unchecked_unwrap() }
83+
unsafe { entry.unwrap_unchecked() }
8684
}
8785

8886
// Get the child node corresponding to the given key.
8987
#[inline]
9088
pub fn child(&self, key: &[u8]) -> Option<&Node<K, V>> {
91-
self.entries.get(nybble_index(self.choice, key.borrow()))
89+
self.entries.get(nybble_index(self.choice, key))
9290
}
9391

9492
// Mutable version of `Branch::child`.
9593
#[inline]
9694
pub fn child_mut(&mut self, key: &[u8]) -> Option<&mut Node<K, V>> {
97-
self.entries
98-
.get_mut(nybble_index(self.choice, key.borrow()))
95+
self.entries.get_mut(nybble_index(self.choice, key))
9996
}
10097

10198
// Immutably borrow the leaf for the given key, if it exists, mutually recursing through
10299
// `Node::get`.
103100
#[inline]
104101
pub fn get(&self, key: &[u8]) -> Option<&Leaf<K, V>> {
105-
match self.child(key.borrow()) {
102+
match self.child(key) {
106103
Some(child) => child.get(key),
107104
None => None,
108105
}
@@ -112,37 +109,34 @@ impl<K: Borrow<[u8]>, V> Branch<K, V> {
112109
// `Node::get_mut`.
113110
#[inline]
114111
pub fn get_mut(&mut self, key: &[u8]) -> Option<&mut Leaf<K, V>> {
115-
self.child_mut(key.borrow())
116-
.and_then(|node| node.get_mut(key))
112+
self.child_mut(key).and_then(|node| node.get_mut(key))
117113
}
118114

119115
// Retrieve the node which contains the exemplar. This does not recurse and return the actual
120116
// exemplar - just the node which might be or contain it.
121117
#[inline]
122118
pub fn exemplar(&self, key: &[u8]) -> &Node<K, V> {
123-
self.entries
124-
.get_or_any(nybble_index(self.choice, key.borrow()))
119+
self.entries.get_or_any(nybble_index(self.choice, key))
125120
}
126121

127122
// As `Branch::exemplar` but for mutable borrows.
128123
#[inline]
129124
pub fn exemplar_mut(&mut self, key: &[u8]) -> &mut Node<K, V> {
130-
self.entries
131-
.get_or_any_mut(nybble_index(self.choice, key.borrow()))
125+
self.entries.get_or_any_mut(nybble_index(self.choice, key))
132126
}
133127

134128
// Immutably borrow the exemplar for the given key, mutually recursing through
135129
// `Node::get_exemplar`.
136130
#[inline]
137131
pub fn get_exemplar(&self, key: &[u8]) -> &Leaf<K, V> {
138-
self.exemplar(key.borrow()).get_exemplar(key)
132+
self.exemplar(key).get_exemplar(key)
139133
}
140134

141135
// Mutably borrow the exemplar for the given key, mutually recursing through
142136
// `Node::get_exemplar_mut`.
143137
#[inline]
144138
pub fn get_exemplar_mut(&mut self, key: &[u8]) -> &mut Leaf<K, V> {
145-
self.exemplar_mut(key.borrow()).get_exemplar_mut(key)
139+
self.exemplar_mut(key).get_exemplar_mut(key)
146140
}
147141

148142
// Convenience method for inserting a leaf into the branch's sparse array.
@@ -336,7 +330,7 @@ impl<K: Borrow<[u8]>, V> Node<K, V> {
336330
let child_opt = branch.child(prefix);
337331

338332
// unsafe: child must exist in the trie - prefix'd nodes must exist.
339-
let child = unsafe { child_opt.unchecked_unwrap() };
333+
let child = unsafe { child_opt.unwrap_unchecked() };
340334

341335
child.get_prefix_validated(prefix)
342336
}
@@ -379,7 +373,7 @@ impl<K: Borrow<[u8]>, V> Node<K, V> {
379373

380374
// unsafe: child must exist as there must exist nodes with the given prefix in
381375
// the trie.
382-
let child = unsafe { child_opt.unchecked_unwrap() };
376+
let child = unsafe { child_opt.unwrap_unchecked() };
383377

384378
child.get_prefix_validated_mut(prefix)
385379
}
@@ -565,11 +559,11 @@ impl<K: Borrow<[u8]>, V> Node<K, V> {
565559
match *root {
566560
Some(Node::Leaf(..))
567561
// unsafe: root has been match'd as some branch.
568-
if unsafe { root.as_ref().unchecked_unwrap().unwrap_leaf_ref() }
562+
if unsafe { root.as_ref().unwrap_unchecked().unwrap_leaf_ref() }
569563
.key_slice() == key => {
570564

571565
// unsafe: same rationale.
572-
Some(unsafe { root.take().unchecked_unwrap().unwrap_leaf() })
566+
Some(unsafe { root.take().unwrap_unchecked().unwrap_leaf() })
573567
}
574568

575569
Some(ref mut node @ Node::Branch(..)) => node.remove_validated(key),
@@ -627,24 +621,24 @@ impl<K: Borrow<[u8]>, V> Node<K, V> {
627621
match *root {
628622
Some(Node::Leaf(..))
629623
// unsafe: root has been matched as some leaf.
630-
if unsafe { root.as_ref().unchecked_unwrap().unwrap_leaf_ref() }
624+
if unsafe { root.as_ref().unwrap_unchecked().unwrap_leaf_ref() }
631625
.key_slice()
632626
.starts_with(prefix) => root.take(),
633627

634628
Some(Node::Branch(..))
635629
// unsafe: root has been matched as some branch.
636-
if unsafe { root.as_ref().unchecked_unwrap().unwrap_branch_ref() }
630+
if unsafe { root.as_ref().unwrap_unchecked().unwrap_branch_ref() }
637631
.get_exemplar(prefix)
638632
.key_slice()
639633
.starts_with(prefix) => {
640634

641635
// unsafe: same rationale.
642-
if unsafe { root.as_ref().unchecked_unwrap().unwrap_branch_ref() }
636+
if unsafe { root.as_ref().unwrap_unchecked().unwrap_branch_ref() }
643637
.choice >= prefix.len() * 2
644638
{
645639
root.take()
646640
} else {
647-
unsafe { root.as_mut().unchecked_unwrap() }.remove_prefix_validated(prefix)
641+
unsafe { root.as_mut().unwrap_unchecked() }.remove_prefix_validated(prefix)
648642
}
649643
}
650644

src/sparse.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use alloc::vec::{IntoIter, Vec};
22
use core::fmt;
33
use core::slice::{Iter, IterMut};
44

5-
use unreachable::UncheckedOptionExt;
6-
75
// A sparse array, holding up to 17 elements, indexed by nybbles with a special exception for
86
// elements which are shorter than the "choice point" of the branch node which holds this sparse
97
// array. This special exception is the "head".
@@ -120,7 +118,7 @@ impl<T> Sparse<T> {
120118
#[inline]
121119
pub fn clear_last(&mut self) -> T {
122120
debug_assert!(self.len() == 1);
123-
unsafe { self.entries.pop().unchecked_unwrap() }
121+
unsafe { self.entries.pop().unwrap_unchecked() }
124122
}
125123

126124
#[inline]

src/subtrie.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use core::borrow::Borrow;
22
use core::fmt;
33
use core::ops::Index;
44

5-
use iter::Iter;
6-
use node::Node;
5+
use crate::iter::Iter;
6+
use crate::node::Node;
77

88
pub struct SubTrie<'a, K: 'a, V: 'a> {
99
pub(crate) root: Option<&'a Node<K, V>>,

src/trie.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use core::fmt;
44
use core::iter::FromIterator;
55
use core::ops::{Index, IndexMut};
66

7-
use entry::{make_entry, Entry};
8-
use iter::{IntoIter, Iter, IterMut, Keys, Values, ValuesMut};
9-
use node::{Leaf, Node};
10-
use subtrie::SubTrie;
11-
use util::nybble_mismatch;
12-
use wrapper::{BStr, BString};
7+
use crate::entry::{make_entry, Entry};
8+
use crate::iter::{IntoIter, Iter, IterMut, Keys, Values, ValuesMut};
9+
use crate::node::{Leaf, Node};
10+
use crate::subtrie::SubTrie;
11+
use crate::util::nybble_mismatch;
12+
use crate::wrapper::{BStr, BString};
1313

1414
/// A QP-trie. QP stands for - depending on who you ask - either "quelques-bits popcount" or
1515
/// "quad-bit popcount". In any case, the fact of the matter is that this is a compressed radix

src/wrapper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::fmt;
55
use core::hash::{Hash, Hasher};
66
use core::ops::Deref;
77

8-
use trie::Break;
8+
use crate::trie::Break;
99

1010
/// A wrapper for `String` which implements `Borrow<[u8]>` and hashes in the same way as a byte
1111
/// slice.

0 commit comments

Comments
 (0)