Skip to content

Commit

Permalink
fix: clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
arriqaaq authored Feb 10, 2024
1 parent 25f9c14 commit 07fa2f5
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 36 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: CI

run-name: "CI run '${{ github.head_ref || github.ref_name }}'"

on:
workflow_dispatch:
push:
branches:
- main
pull_request:
merge_group:

defaults:
run:
shell: bash

jobs:
ci:
strategy:
matrix:
os: ["ubuntu-latest"]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v2
- name: Cache
id: rust-cache
uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml', '.github/workflows/*.yml') }}

- uses: actions-rs/toolchain@v1
with:
toolchain: 1.75.0
components: rustfmt, clippy
default: true

- name: Install cargo-deny
if: steps.rust-cache.outputs.cache-hit != 'true'
run: rustup run --install 1.70 cargo install --force --version 0.14.3 cargo-deny --locked

- name: Compile
run: cargo build --all-targets --all-features

- name: Run tests
run: make test

- name: Clippy
run: cargo clippy --all --all-targets -- -Dwarnings

- name: Format
run: cargo fmt --all -- --check
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "vart"
publish = true
version = "0.1.0"
version = "0.1.1"
edition = "2021"
license = "Apache-2.0"
readme = "README.md"
Expand Down
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This command builds the project, including all targets, and generates the documentation.
build: check
cargo build --all-targets
cargo doc

# This command checks the licenses of all dependencies, formats the code, and runs the Clippy linter.
check:
cargo deny --all-features check licenses
cargo fmt --all -- --check
cargo clippy --all --all-targets

# This command runs the tests with backtrace enabled.
test: check
RUST_BACKTRACE=1 cargo test
23 changes: 23 additions & 0 deletions deny.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Configuration documentation:
#  https://embarkstudios.github.io/cargo-deny/index.html

[advisories]
vulnerability = "deny"
yanked = "deny"
unmaintained = "warn"
notice = "warn"
ignore = [
]
git-fetch-with-cli = true

[licenses]
allow-osi-fsf-free = "either"
copyleft = "warn"
unlicensed = "deny"
default = "deny"
confidence-threshold = 0.8


[bans]
multiple-versions = "warn"
highlight = "all"
67 changes: 35 additions & 32 deletions src/art.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const NODE48MAX: usize = 48;
const NODE256MIN: usize = NODE48MAX + 1;

// Maximum number of active snapshots
pub(crate) const DEFAULT_MAX_ACTIVE_SNAPSHOTS: u64 = 10000;
pub const DEFAULT_MAX_ACTIVE_SNAPSHOTS: u64 = 10000;

/// A struct representing a node in an Adaptive Radix Trie.
///
Expand Down Expand Up @@ -470,7 +470,7 @@ impl<P: KeyTrait + Clone, V: Clone> Node<P, V> {
///
/// Returns `true` if the node type is an inner node, otherwise returns `false`.
///
#[inline]
#[allow(dead_code)]
pub(crate) fn is_inner(&self) -> bool {
!self.is_twig()
}
Expand Down Expand Up @@ -652,7 +652,7 @@ impl<P: KeyTrait + Clone, V: Clone> Node<P, V> {
commit_version: u64,
ts: u64,
depth: usize,
) -> Result<(Arc<Node<P, V>>, Option<V>), TrieError> {
) -> Result<(NodeArc<P, V>, Option<V>), TrieError> {
// Obtain the current node's prefix and its length.
let cur_node_prefix = cur_node.prefix().clone();
let cur_node_prefix_len = cur_node.prefix().len();
Expand Down Expand Up @@ -912,6 +912,9 @@ pub struct KV<P, V> {
pub ts: u64,
}

// A type alias for a node reference.
type NodeArc<P, V> = Arc<Node<P, V>>;

impl<P: KeyTrait, V: Clone> KV<P, V> {
pub fn new(key: P, value: V, version: u64, timestamp: u64) -> Self {
KV {
Expand Down Expand Up @@ -1204,7 +1207,7 @@ impl<P: KeyTrait, V: Clone> Tree<P, V> {
/// Returns `Ok(())` if the snapshot is successfully closed and removed. Returns an `Err`
/// with `TrieError::SnapshotNotFound` if the snapshot with the given ID is not found.
///
pub(crate) fn close_snapshot(&mut self, snapshot_id: u64) -> Result<(), TrieError> {
pub fn close_snapshot(&mut self, snapshot_id: u64) -> Result<(), TrieError> {
// Check if the tree is already closed
self.is_closed()?;

Expand Down Expand Up @@ -1312,7 +1315,7 @@ mod tests {
fn read_words_from_file(file_path: &str) -> io::Result<Vec<String>> {
let file = File::open(file_path)?;
let reader = BufReader::new(file);
let words: Vec<String> = reader.lines().filter_map(|line| line.ok()).collect();
let words: Vec<String> = reader.lines().map_while(Result::ok).collect();
Ok(words)
}

Expand All @@ -1325,7 +1328,7 @@ mod tests {
// Insertion phase
for word in &words {
let key = &VariableSizeKey::from_str(word).unwrap();
tree.insert(key, 1, 0, 0);
let _ = tree.insert(key, 1, 0, 0);
}

// Search phase
Expand Down Expand Up @@ -1357,7 +1360,7 @@ mod tests {
];

for word in &insert_words {
tree.insert(&VariableSizeKey::from_str(word).unwrap(), 1, 0, 0);
let _ = tree.insert(&VariableSizeKey::from_str(word).unwrap(), 1, 0, 0);
}

// Deletion phase
Expand All @@ -1380,7 +1383,7 @@ mod tests {
];

for (word, val) in &words_to_insert {
tree.insert(&VariableSizeKey::from_str(word).unwrap(), *val, 0, 0);
let _ = tree.insert(&VariableSizeKey::from_str(word).unwrap(), *val, 0, 0);
}

// Verification phase
Expand All @@ -1399,7 +1402,7 @@ mod tests {
// Insertion phase
let key = VariableSizeKey::from_str("abc").unwrap();
let value = 1;
tree.insert(&key, value, 0, 0);
let _ = tree.insert(&key, value, 0, 0);

// Verification phase
let (_, val, _ts, _) = tree.get(&key, 0).unwrap();
Expand Down Expand Up @@ -1429,7 +1432,7 @@ mod tests {
// Insertion
let key = VariableSizeKey::from_str("test").unwrap();
let value = 1;
tree.insert(&key, value, 0, 0);
let _ = tree.insert(&key, value, 0, 0);

// Removal
assert!(tree.remove(&key).unwrap());
Expand All @@ -1446,8 +1449,8 @@ mod tests {
let mut tree = Tree::<VariableSizeKey, i32>::new();

// Insertion
tree.insert(&key1, 1, 0, 0);
tree.insert(&key2, 1, 0, 0);
tree.insert(&key1, 1, 0, 0).unwrap();
tree.insert(&key2, 1, 0, 0).unwrap();

// Removal
assert!(tree.remove(&key1).unwrap());
Expand All @@ -1470,8 +1473,8 @@ mod tests {
let mut tree = Tree::<VariableSizeKey, i32>::new();

// Insertion
tree.insert(&key1, 1, 0, 0);
tree.insert(&key2, 1, 0, 0);
tree.insert(&key1, 1, 0, 0).unwrap();
tree.insert(&key2, 1, 0, 0).unwrap();

// Removal
assert!(tree.remove(&key1).unwrap());
Expand Down Expand Up @@ -1514,7 +1517,7 @@ mod tests {
// Insertion
for i in 0..5u32 {
let key = VariableSizeKey::from_slice(&i.to_be_bytes());
tree.insert(&key, 1, 0, 0);
tree.insert(&key, 1, 0, 0).unwrap();
}

// Removal
Expand Down Expand Up @@ -1562,7 +1565,7 @@ mod tests {
// Insertion
for i in 0..17u32 {
let key = VariableSizeKey::from_slice(&i.to_be_bytes());
tree.insert(&key, 1, 0, 0);
tree.insert(&key, 1, 0, 0).unwrap();
}

// Removal
Expand All @@ -1585,7 +1588,7 @@ mod tests {
// Insertion
for i in 0..17u32 {
let key = VariableSizeKey::from_slice(&i.to_be_bytes());
tree.insert(&key, 1, 0, 0);
tree.insert(&key, 1, 0, 0).unwrap();
}

// Root verification
Expand Down Expand Up @@ -1652,7 +1655,7 @@ mod tests {
// Insertion
for i in 0..49u32 {
let key = VariableSizeKey::from_slice(&i.to_be_bytes());
tree.insert(&key, 1, 0, 0);
tree.insert(&key, 1, 0, 0).unwrap();
}

// Root verification
Expand Down Expand Up @@ -1686,7 +1689,7 @@ mod tests {
// // }

#[derive(Debug, Clone, PartialEq)]
struct KVT {
struct Kvt {
k: Vec<u8>, // Key
version: u64, // version
}
Expand All @@ -1696,27 +1699,27 @@ mod tests {
let mut tree: Tree<VariableSizeKey, i32> = Tree::<VariableSizeKey, i32>::new();

let kvts = vec![
KVT {
Kvt {
k: b"key1_0".to_vec(),
version: 0,
},
KVT {
Kvt {
k: b"key2_0".to_vec(),
version: 0,
},
KVT {
Kvt {
k: b"key3_0".to_vec(),
version: 0,
},
KVT {
Kvt {
k: b"key4_0".to_vec(),
version: 0,
},
KVT {
Kvt {
k: b"key5_0".to_vec(),
version: 0,
},
KVT {
Kvt {
k: b"key6_0".to_vec(),
version: 0,
},
Expand Down Expand Up @@ -1870,7 +1873,7 @@ mod tests {
// Insertion
for i in 0..u16::MAX {
let key: FixedSizeKey<16> = i.into();
tree.insert(&key, i, 0, i as u64);
tree.insert(&key, i, 0, i as u64).unwrap();
}

// Iteration and verification
Expand Down Expand Up @@ -1898,7 +1901,7 @@ mod tests {
// Insertion
for i in 0..u8::MAX {
let key: FixedSizeKey<32> = i.into();
tree.insert(&key, i, 0, 0);
tree.insert(&key, i, 0, 0).unwrap();
}

// Iteration and verification
Expand All @@ -1925,7 +1928,7 @@ mod tests {
// Insertion
for i in 0..=max {
let key: FixedSizeKey<8> = i.into();
tree.insert(&key, i, 0, 0);
tree.insert(&key, i, 0, 0).unwrap();
}

// Test inclusive range
Expand Down Expand Up @@ -1973,7 +1976,7 @@ mod tests {
// Insertion
for i in 0..=max {
let key: FixedSizeKey<16> = i.into();
tree.insert(&key, i, 0, 0);
tree.insert(&key, i, 0, 0).unwrap();
}

let mut len = 0usize;
Expand All @@ -1993,9 +1996,9 @@ mod tests {
// Insertions
let key1 = VariableSizeKey::from_str("abc").unwrap();
let key2 = VariableSizeKey::from_str("efg").unwrap();
tree.insert(&key1, 1, 0, 0);
tree.insert(&key1, 2, 10, 0);
tree.insert(&key2, 3, 11, 0);
tree.insert(&key1, 1, 0, 0).unwrap();
tree.insert(&key1, 2, 10, 0).unwrap();
tree.insert(&key2, 3, 11, 0).unwrap();

// Versioned retrievals and assertions
let (_, val, _, _) = tree.get(&key1, 1).unwrap();
Expand Down
5 changes: 4 additions & 1 deletion src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::KeyTrait;
// TODO: need to add more tests for snapshot readers
/// A structure representing a pointer for iterating over the Trie's key-value pairs.
pub struct IterationPointer<P: KeyTrait, V: Clone> {
#[allow(dead_code)]
pub(crate) id: u64,
root: Arc<Node<P, V>>,
}
Expand Down Expand Up @@ -45,9 +46,11 @@ impl<P: KeyTrait, V: Clone> IterationPointer<P, V> {
}
}

type NodeIterator<'a, P, V> = Box<dyn Iterator<Item = (u8, &'a Arc<Node<P, V>>)> + 'a>;

/// An iterator over the nodes in the Trie.
struct NodeIter<'a, P: KeyTrait, V: Clone> {
node: Box<dyn Iterator<Item = (u8, &'a Arc<Node<P, V>>)> + 'a>,
node: NodeIterator<'a, P, V>,
}

impl<'a, P: KeyTrait, V: Clone> NodeIter<'a, P, V> {
Expand Down
Loading

0 comments on commit 07fa2f5

Please sign in to comment.