Skip to content

Commit

Permalink
Merge pull request #1 from cassc/allow-impl-database-key
Browse files Browse the repository at this point in the history
remove dependency to db-key
  • Loading branch information
rim99 authored Aug 24, 2024
2 parents 77cc91f + 5deb65f commit 030c3c3
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ name = "leveldb"

[dependencies]

db-key = "0.0.5"
libc = "0.2.4"

[dependencies.leveldb-sys]
Expand Down
25 changes: 25 additions & 0 deletions src/database/key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub trait Key {
fn from_u8(key: &[u8]) -> Self;
fn as_slice<T, F: Fn(&[u8]) -> T>(&self, f: F) -> T;
}

pub fn from_u8<K: Key>(key: &[u8]) -> K {
Key::from_u8(key)
}

impl Key for i32 {
fn from_u8(key: &[u8]) -> i32 {
assert!(key.len() == 4);

(key[0] as i32) << 24 | (key[1] as i32) << 16 | (key[2] as i32) << 8 | (key[3] as i32)
}

fn as_slice<T, F: Fn(&[u8]) -> T>(&self, f: F) -> T {
let mut dst = [0u8, 0, 0, 0];
dst[0] = (*self >> 24) as u8;
dst[1] = (*self >> 16) as u8;
dst[2] = (*self >> 8) as u8;
dst[3] = *self as u8;
f(&dst)
}
}
3 changes: 1 addition & 2 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! The main database module, allowing to interface with leveldb on
//! a key-value basis.
extern crate db_key as key;

use leveldb_sys::*;

use self::options::{Options, c_options};
Expand All @@ -28,6 +26,7 @@ pub mod batch;
pub mod management;
pub mod compaction;
pub mod bytes;
pub mod key;

#[allow(missing_docs)]
struct RawDB {
Expand Down
2 changes: 1 addition & 1 deletion tests/comparator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(test)]
mod comparator {
use libc::c_char;
use key::Key;
use leveldb::database::key::Key;
use utils::{tmpdir, db_put_simple};
use leveldb::database::{Database};
use leveldb::iterator::Iterable;
Expand Down
3 changes: 1 addition & 2 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
extern crate db_key as key;
extern crate leveldb;
extern crate tempdir;
extern crate libc;
Expand All @@ -13,4 +12,4 @@ mod cache;
mod writebatch;
mod management;
mod compaction;
mod concurrent_access;
mod concurrent_access;
3 changes: 2 additions & 1 deletion tests/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use leveldb::database::Database;
use leveldb::database::kv::{KV};
use leveldb::database::key::Key;
use leveldb::options::{Options,WriteOptions};
use std::path::Path;
use tempdir::TempDir;
use key::Key;


pub fn open_database<K: Key + Ord>(path: &Path, create_if_missing: bool) -> Database<K> {
let mut opts = Options::new();
Expand Down

0 comments on commit 030c3c3

Please sign in to comment.