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

Apply rustfmt #41

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
74 changes: 43 additions & 31 deletions src/database/batch.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//! Module providing write batches

use super::error::Error;
use super::Database;
use database::key::from_u8;
use database::key::Key;
use leveldb_sys::*;
use libc::{c_char, size_t, c_void};
use libc::{c_char, c_void, size_t};
use options::{c_writeoptions, WriteOptions};
use std::marker::PhantomData;
use database::key::Key;
use database::key::from_u8;
use std::slice;
use options::{WriteOptions, c_writeoptions};
use super::error::Error;
use std::ptr;
use super::Database;
use std::slice;

#[allow(missing_docs)]
struct RawWritebatch {
Expand Down Expand Up @@ -43,10 +43,12 @@ impl<K: Key> Batch<K> for Database<K> {
let mut error = ptr::null_mut();
let c_writeoptions = c_writeoptions(options);

leveldb_write(self.database.ptr,
c_writeoptions,
batch.writebatch.ptr,
&mut error);
leveldb_write(
self.database.ptr,
c_writeoptions,
batch.writebatch.ptr,
&mut error,
);
leveldb_writeoptions_destroy(c_writeoptions);

if error == ptr::null_mut() {
Expand Down Expand Up @@ -78,11 +80,13 @@ impl<K: Key> Writebatch<K> {
pub fn put(&mut self, key: K, value: &[u8]) {
unsafe {
key.as_slice(|k| {
leveldb_writebatch_put(self.writebatch.ptr,
k.as_ptr() as *mut c_char,
k.len() as size_t,
value.as_ptr() as *mut c_char,
value.len() as size_t);
leveldb_writebatch_put(
self.writebatch.ptr,
k.as_ptr() as *mut c_char,
k.len() as size_t,
value.as_ptr() as *mut c_char,
value.len() as size_t,
);
})
}
}
Expand All @@ -91,9 +95,11 @@ impl<K: Key> Writebatch<K> {
pub fn delete(&mut self, key: K) {
unsafe {
key.as_slice(|k| {
leveldb_writebatch_delete(self.writebatch.ptr,
k.as_ptr() as *mut c_char,
k.len() as size_t);
leveldb_writebatch_delete(
self.writebatch.ptr,
k.as_ptr() as *mut c_char,
k.len() as size_t,
);
})
}
}
Expand All @@ -102,10 +108,12 @@ impl<K: Key> Writebatch<K> {
pub fn iterate<T: WritebatchIterator<K = K>>(&mut self, iterator: Box<T>) -> Box<T> {
unsafe {
let iter = Box::into_raw(iterator);
leveldb_writebatch_iterate(self.writebatch.ptr,
iter as *mut c_void,
put_callback::<K, T>,
deleted_callback::<K, T>);
leveldb_writebatch_iterate(
self.writebatch.ptr,
iter as *mut c_void,
put_callback::<K, T>,
deleted_callback::<K, T>,
);
Box::from_raw(iter)
}
}
Expand All @@ -123,11 +131,13 @@ pub trait WritebatchIterator {
fn deleted(&mut self, key: Self::K);
}

extern "C" fn put_callback<K: Key, T: WritebatchIterator<K = K>>(state: *mut c_void,
key: *const c_char,
keylen: size_t,
val: *const c_char,
vallen: size_t) {
extern "C" fn put_callback<K: Key, T: WritebatchIterator<K = K>>(
state: *mut c_void,
key: *const c_char,
keylen: size_t,
val: *const c_char,
vallen: size_t,
) {
unsafe {
let iter: &mut T = &mut *(state as *mut T);
let key_slice = slice::from_raw_parts::<u8>(key as *const u8, keylen as usize);
Expand All @@ -137,9 +147,11 @@ extern "C" fn put_callback<K: Key, T: WritebatchIterator<K = K>>(state: *mut c_v
}
}

extern "C" fn deleted_callback<K: Key, T: WritebatchIterator<K = K>>(state: *mut c_void,
key: *const c_char,
keylen: size_t) {
extern "C" fn deleted_callback<K: Key, T: WritebatchIterator<K = K>>(
state: *mut c_void,
key: *const c_char,
keylen: size_t,
) {
unsafe {
let iter: &mut T = &mut *(state as *mut T);
let key_slice = slice::from_raw_parts::<u8>(key as *const u8, keylen as usize);
Expand Down
10 changes: 3 additions & 7 deletions src/database/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ::std::slice;
use std::slice;

/// Bytes allocated by leveldb
///
Expand Down Expand Up @@ -53,17 +53,13 @@ impl ::std::ops::Deref for Bytes {
type Target = [u8];

fn deref(&self) -> &Self::Target {
unsafe {
slice::from_raw_parts(self.bytes, self.size)
}
unsafe { slice::from_raw_parts(self.bytes, self.size) }
}
}

impl ::std::ops::DerefMut for Bytes {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe {
slice::from_raw_parts_mut(self.bytes as *mut u8, self.size)
}
unsafe { slice::from_raw_parts_mut(self.bytes as *mut u8, self.size) }
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/database/cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Structs and traits to work with the leveldb cache.
use leveldb_sys::{leveldb_cache_t, leveldb_cache_create_lru, leveldb_cache_destroy};
use leveldb_sys::{leveldb_cache_create_lru, leveldb_cache_destroy, leveldb_cache_t};
use libc::size_t;

#[allow(missing_docs)]
Expand All @@ -24,7 +24,9 @@ impl Cache {
/// Create a leveldb LRU cache of a given size
pub fn new(size: size_t) -> Cache {
let cache = unsafe { leveldb_cache_create_lru(size) };
Cache { raw: RawCache { ptr: cache } }
Cache {
raw: RawCache { ptr: cache },
}
}

#[allow(missing_docs)]
Expand Down
14 changes: 8 additions & 6 deletions src/database/compaction.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Compaction
use super::Database;
use super::key::Key;
use super::Database;
use leveldb_sys::leveldb_compact_range;
use libc::{c_char, size_t};

Expand All @@ -13,11 +13,13 @@ impl<'a, K: Key + 'a> Compaction<'a, K> for Database<K> {
unsafe {
start.as_slice(|s| {
limit.as_slice(|l| {
leveldb_compact_range(self.database.ptr,
s.as_ptr() as *mut c_char,
s.len() as size_t,
l.as_ptr() as *mut c_char,
l.len() as size_t);
leveldb_compact_range(
self.database.ptr,
s.as_ptr() as *mut c_char,
s.len() as size_t,
l.as_ptr() as *mut c_char,
l.len() as size_t,
);
});
});
}
Expand Down
45 changes: 25 additions & 20 deletions src/database/comparator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
//! Comparators allow to override this comparison.
//! The ordering of keys introduced by the compartor influences iteration order.
//! Databases written with one Comparator cannot be opened with another.
use database::key::from_u8;
use database::key::Key;
use leveldb_sys::*;
use libc::{size_t, c_void, c_char};
use std::slice;
use libc::{c_char, c_void, size_t};
use std::cmp::Ordering;
use database::key::Key;
use database::key::from_u8;
use std::marker::PhantomData;
use std::slice;

/// A comparator has two important functions:
///
Expand Down Expand Up @@ -48,22 +48,25 @@ impl<K: Key + Ord> OrdComparator<K> {
}

/// DefaultComparator is the a stand in for "no comparator set"
#[derive(Copy,Clone)]
#[derive(Copy, Clone)]
pub struct DefaultComparator;

unsafe trait InternalComparator : Comparator where Self: Sized {

unsafe trait InternalComparator: Comparator
where
Self: Sized,
{
extern "C" fn name(state: *mut c_void) -> *const c_char {
let x = unsafe { &*(state as *mut Self) };
x.name()
}

extern "C" fn compare(state: *mut c_void,
a: *const c_char,
a_len: size_t,
b: *const c_char,
b_len: size_t)
-> i32 {
extern "C" fn compare(
state: *mut c_void,
a: *const c_char,
a_len: size_t,
b: *const c_char,
b_len: size_t,
) -> i32 {
unsafe {
let a_slice = slice::from_raw_parts::<u8>(a as *const u8, a_len as usize);
let b_slice = slice::from_raw_parts::<u8>(b as *const u8, b_len as usize);
Expand All @@ -80,7 +83,7 @@ unsafe trait InternalComparator : Comparator where Self: Sized {

extern "C" fn destructor(state: *mut c_void) {
let _x: Box<Self> = unsafe { Box::from_raw(state as *mut Self) };
// let the Box fall out of scope and run the T's destructor
// let the Box fall out of scope and run the T's destructor
}
}

Expand All @@ -89,15 +92,17 @@ unsafe impl<C: Comparator> InternalComparator for C {}
#[allow(missing_docs)]
pub fn create_comparator<T: Comparator>(x: Box<T>) -> *mut leveldb_comparator_t {
unsafe {
leveldb_comparator_create(Box::into_raw(x) as *mut c_void,
<T as InternalComparator>::destructor,
<T as InternalComparator>::compare,
<T as InternalComparator>::name)
leveldb_comparator_create(
Box::into_raw(x) as *mut c_void,
<T as InternalComparator>::destructor,
<T as InternalComparator>::compare,
<T as InternalComparator>::name,
)
}
}

impl<K: Key + Ord> Comparator for OrdComparator<K> {
type K = K;
type K = K;

fn name(&self) -> *const c_char {
let slice: &str = self.name.as_ref();
Expand All @@ -110,7 +115,7 @@ impl<K: Key + Ord> Comparator for OrdComparator<K> {
}

impl Comparator for DefaultComparator {
type K = i32;
type K = i32;

fn name(&self) -> *const c_char {
"default_comparator".as_ptr() as *const c_char
Expand Down
10 changes: 6 additions & 4 deletions src/database/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! The module defining custom leveldb error type.

use libc::c_void;
use leveldb_sys::leveldb_free;
use std;
use libc::c_char;
use libc::c_void;
use std;

/// A leveldb error, just containing the error string
/// provided by leveldb.
Expand All @@ -23,10 +23,12 @@ impl Error {
/// This method is `unsafe` because the pointer must be valid and point to heap.
/// The pointer will be passed to `free`!
pub unsafe fn new_from_char(message: *const c_char) -> Error {
use std::str::from_utf8;
use std::ffi::CStr;
use std::str::from_utf8;

let err_string = from_utf8(CStr::from_ptr(message).to_bytes()).unwrap().to_string();
let err_string = from_utf8(CStr::from_ptr(message).to_bytes())
.unwrap()
.to_string();
leveldb_free(message as *mut c_void);
Error::new(err_string)
}
Expand Down
Loading