From 0cc38449c21f29bd9348e28c5719d650e16159cf Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 5 Mar 2016 17:56:02 -0800 Subject: [PATCH] Introduce a Slice abstraction for buffers This will allow for dynamic creation of PHF data structures. --- phf/src/lib.rs | 25 +++++++++++++++++++++++++ phf/src/map.rs | 10 +++++----- phf/src/ordered_map.rs | 12 ++++++------ phf_codegen/src/lib.rs | 20 ++++++++++---------- phf_macros/src/util.rs | 10 +++++----- 5 files changed, 51 insertions(+), 26 deletions(-) diff --git a/phf/src/lib.rs b/phf/src/lib.rs index af3b94bc..1d8e4a84 100644 --- a/phf/src/lib.rs +++ b/phf/src/lib.rs @@ -13,6 +13,8 @@ extern crate std as core; extern crate phf_shared; +use core::ops::Deref; + pub use phf_shared::PhfHash; #[doc(inline)] pub use map::Map; @@ -27,3 +29,26 @@ pub mod map; pub mod set; pub mod ordered_map; pub mod ordered_set; + +// WARNING: this is not considered part of phf's public API and is subject to +// change at any time. +// +// Basically Cow, but with the Owned version conditionally compiled +#[doc(hidden)] +pub enum Slice { + Static(&'static [T]), + #[cfg(not(feature = "core"))] + Dynamic(Vec), +} + +impl Deref for Slice { + type Target = [T]; + + fn deref(&self) -> &[T] { + match *self { + Slice::Static(t) => t, + #[cfg(not(feature = "core"))] + Slice::Dynamic(ref t) => t, + } + } +} diff --git a/phf/src/map.rs b/phf/src/map.rs index d2e33554..7f282efa 100644 --- a/phf/src/map.rs +++ b/phf/src/map.rs @@ -4,8 +4,8 @@ use core::ops::Index; use core::slice; use core::fmt; use core::iter::IntoIterator; -use PhfHash; -use phf_shared; +use phf_shared::{self, PhfHash}; +use Slice; /// An immutable map constructed at compile time. /// @@ -18,9 +18,9 @@ pub struct Map { #[doc(hidden)] pub key: u64, #[doc(hidden)] - pub disps: &'static [(u32, u32)], + pub disps: Slice<(u32, u32)>, #[doc(hidden)] - pub entries: &'static [(K, V)], + pub entries: Slice<(K, V)>, } impl fmt::Debug for Map where K: fmt::Debug, V: fmt::Debug { @@ -81,7 +81,7 @@ impl Map { K: Borrow { let hash = phf_shared::hash(key, self.key); - let index = phf_shared::get_index(hash, self.disps, self.entries.len()); + let index = phf_shared::get_index(hash, &*self.disps, self.entries.len()); let entry = &self.entries[index as usize]; let b: &T = entry.0.borrow(); if b == key { diff --git a/phf/src/ordered_map.rs b/phf/src/ordered_map.rs index 6d2f85f4..cd1d5522 100644 --- a/phf/src/ordered_map.rs +++ b/phf/src/ordered_map.rs @@ -4,9 +4,9 @@ use core::iter::IntoIterator; use core::ops::Index; use core::fmt; use core::slice; +use phf_shared::{self, PhfHash}; -use PhfHash; -use phf_shared; +use Slice; /// An order-preserving immutable map constructed at compile time. /// @@ -22,11 +22,11 @@ pub struct OrderedMap { #[doc(hidden)] pub key: u64, #[doc(hidden)] - pub disps: &'static [(u32, u32)], + pub disps: Slice<(u32, u32)>, #[doc(hidden)] - pub idxs: &'static [usize], + pub idxs: Slice, #[doc(hidden)] - pub entries: &'static [(K, V)], + pub entries: Slice<(K, V)>, } impl fmt::Debug for OrderedMap where K: fmt::Debug, V: fmt::Debug { @@ -109,7 +109,7 @@ impl OrderedMap { K: Borrow { let hash = phf_shared::hash(key, self.key); - let idx_index = phf_shared::get_index(hash, self.disps, self.idxs.len()); + let idx_index = phf_shared::get_index(hash, &*self.disps, self.idxs.len()); let idx = self.idxs[idx_index as usize]; let entry = &self.entries[idx]; diff --git a/phf_codegen/src/lib.rs b/phf_codegen/src/lib.rs index 2c0c9180..70cfae91 100644 --- a/phf_codegen/src/lib.rs +++ b/phf_codegen/src/lib.rs @@ -142,7 +142,7 @@ impl Map { try!(write!(w, "::phf::Map {{ key: {}, - disps: &[", + disps: ::phf::Slice::Static(&[", state.key)); for &(d1, d2) in &state.disps { try!(write!(w, @@ -153,8 +153,8 @@ impl Map { } try!(write!(w, " - ], - entries: &[")); + ]), + entries: ::phf::Slice::Static(&[")); for &idx in &state.map { try!(write!(w, " @@ -164,7 +164,7 @@ impl Map { } write!(w, " - ] + ]), }}") } } @@ -241,7 +241,7 @@ impl OrderedMap { try!(write!(w, "::phf::OrderedMap {{ key: {}, - disps: &[", + disps: ::phf::Slice::Static(&[", state.key)); for &(d1, d2) in &state.disps { try!(write!(w, @@ -252,8 +252,8 @@ impl OrderedMap { } try!(write!(w, " - ], - idxs: &[")); + ]), + idxs: ::phf::Slice::Static(&[")); for &idx in &state.map { try!(write!(w, " @@ -262,8 +262,8 @@ impl OrderedMap { } try!(write!(w, " - ], - entries: &[")); + ]), + entries: ::phf::Slice::Static(&[")); for (key, value) in self.keys.iter().zip(self.values.iter()) { try!(write!(w, " @@ -273,7 +273,7 @@ impl OrderedMap { } write!(w, " - ] + ]), }}") } } diff --git a/phf_macros/src/util.rs b/phf_macros/src/util.rs index 4caea28d..2f8e642d 100644 --- a/phf_macros/src/util.rs +++ b/phf_macros/src/util.rs @@ -100,8 +100,8 @@ pub fn create_map(cx: &mut ExtCtxt, let key = state.key; MacEager::expr(quote_expr!(cx, ::phf::Map { key: $key, - disps: &$disps, - entries: &$entries, + disps: ::phf::Slice::Static(&$disps), + entries: ::phf::Slice::Static(&$entries), })) } @@ -138,9 +138,9 @@ pub fn create_ordered_map(cx: &mut ExtCtxt, let key = state.key; MacEager::expr(quote_expr!(cx, ::phf::OrderedMap { key: $key, - disps: &$disps, - idxs: &$idxs, - entries: &$entries, + disps: ::phf::Slice::Static(&$disps), + idxs: ::phf::Slice::Static(&$idxs), + entries: ::phf::Slice::Static(&$entries), })) }