mod serialize; mod timer; mod vint; mod counting_writer; pub mod bitpacker; pub use self::serialize::BinarySerializable; pub use self::timer::Timing; pub use self::timer::TimerTree; pub use self::timer::OpenTimer; pub use self::vint::VInt; pub use self::counting_writer::CountingWriter; use std::io; /// Create a default io error given a string. pub fn make_io_err(msg: String) -> io::Error { io::Error::new(io::ErrorKind::Other, msg) } /// Has length trait pub trait HasLen { /// Return length fn len(&self) -> usize; /// Returns true iff empty. fn is_empty(&self) -> bool { self.len() == 0 } } const HIGHEST_BIT: u64 = 1 << 63; /// Maps a `i64` to `u64` /// /// For simplicity, tantivy internally handles `i64` as `u64`. /// The mapping is defined by this function. /// /// Maps `i64` to `u64` so that /// `-2^63 .. 2^63-1` is mapped /// to /// `0 .. 2^64-1` /// in that order. /// /// This is more suited than simply casting (`val as u64`) /// because of bitpacking. /// /// Imagine a list of `i64` ranging from -10 to 10. /// When casting negative values, the negative values are projected /// to values over 2^63, and all values end up requiring 64 bits. /// /// # See also /// The [reverse mapping is `u64_to_i64`](./fn.u64_to_i64.html). #[inline(always)] pub fn i64_to_u64(val: i64) -> u64 { (val as u64) ^ HIGHEST_BIT } /// Reverse the mapping given by [`i64_to_u64`](./fn.i64_to_u64.html). #[inline(always)] pub fn u64_to_i64(val: u64) -> i64 { (val ^ HIGHEST_BIT) as i64 } #[cfg(test)] mod test { use super::{i64_to_u64, u64_to_i64}; fn test_i64_converter_helper(val: i64) { assert_eq!(u64_to_i64(i64_to_u64(val)), val); } #[test] fn test_i64_converter() { assert_eq!(i64_to_u64(i64::min_value()), u64::min_value()); assert_eq!(i64_to_u64(i64::max_value()), u64::max_value()); test_i64_converter_helper(0i64); test_i64_converter_helper(i64::min_value()); test_i64_converter_helper(i64::max_value()); for i in -1000i64..1000i64 { test_i64_converter_helper(i); } } }