Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Key trait as public to allow custom data types as leveldb keys #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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