Skip to content

Commit

Permalink
Run rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnTitor committed Jun 17, 2021
1 parent 9adc370 commit dd86c6c
Show file tree
Hide file tree
Showing 14 changed files with 325 additions and 194 deletions.
12 changes: 6 additions & 6 deletions phf/src/lib.rs
Expand Up @@ -44,7 +44,7 @@
//!
//! [#183]: https://github.com/rust-phf/rust-phf/issues/183
//! [#196]: https://github.com/rust-phf/rust-phf/issues/196
#![doc(html_root_url="https://docs.rs/phf/0.9")]
#![doc(html_root_url = "https://docs.rs/phf/0.9")]
#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]

Expand All @@ -71,7 +71,7 @@ extern crate std as core;
/// }
/// ```
#[::proc_macro_hack::proc_macro_hack]
pub use phf_macros:: phf_map;
pub use phf_macros::phf_map;

#[cfg(feature = "macros")]
/// Macro to create a `static` (compile-time) [`OrderedMap`].
Expand Down Expand Up @@ -111,20 +111,20 @@ pub use phf_macros::phf_ordered_set;

use core::ops::Deref;

pub use phf_shared::PhfHash;
#[doc(inline)]
pub use self::map::Map;
#[doc(inline)]
pub use self::set::Set;
#[doc(inline)]
pub use self::ordered_map::OrderedMap;
#[doc(inline)]
pub use self::ordered_set::OrderedSet;
#[doc(inline)]
pub use self::set::Set;
pub use phf_shared::PhfHash;

pub mod map;
pub mod set;
pub mod ordered_map;
pub mod ordered_set;
pub mod set;

// WARNING: this is not considered part of phf's public API and is subject to
// change at any time.
Expand Down
56 changes: 38 additions & 18 deletions phf/src/map.rs
@@ -1,10 +1,10 @@
//! An immutable map constructed at compile time.
use core::ops::Index;
use core::slice;
use crate::Slice;
use core::fmt;
use core::iter::IntoIterator;
use phf_shared::{self, PhfHash, PhfBorrow, HashKey};
use crate::Slice;
use core::ops::Index;
use core::slice;
use phf_shared::{self, HashKey, PhfBorrow, PhfHash};

/// An immutable map constructed at compile time.
///
Expand All @@ -22,13 +22,21 @@ pub struct Map<K: 'static, V: 'static> {
pub entries: Slice<(K, V)>,
}

impl<K, V> fmt::Debug for Map<K, V> where K: fmt::Debug, V: fmt::Debug {
impl<K, V> fmt::Debug for Map<K, V>
where
K: fmt::Debug,
V: fmt::Debug,
{
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_map().entries(self.entries()).finish()
}
}

impl<'a, K, V, T: ?Sized> Index<&'a T> for Map<K, V> where T: Eq + PhfHash, K: PhfBorrow<T> {
impl<'a, K, V, T: ?Sized> Index<&'a T> for Map<K, V>
where
T: Eq + PhfHash,
K: PhfBorrow<T>,
{
type Output = V;

fn index(&self, k: &'a T) -> &V {
Expand All @@ -49,16 +57,18 @@ impl<K, V> Map<K, V> {

/// Determines if `key` is in the `Map`.
pub fn contains_key<T: ?Sized>(&self, key: &T) -> bool
where T: Eq + PhfHash,
K: PhfBorrow<T>
where
T: Eq + PhfHash,
K: PhfBorrow<T>,
{
self.get(key).is_some()
}

/// Returns a reference to the value that `key` maps to.
pub fn get<T: ?Sized>(&self, key: &T) -> Option<&V>
where T: Eq + PhfHash,
K: PhfBorrow<T>
where
T: Eq + PhfHash,
K: PhfBorrow<T>,
{
self.get_entry(key).map(|e| e.1)
}
Expand All @@ -68,18 +78,22 @@ impl<K, V> Map<K, V> {
///
/// This can be useful for interning schemes.
pub fn get_key<T: ?Sized>(&self, key: &T) -> Option<&K>
where T: Eq + PhfHash,
K: PhfBorrow<T>
where
T: Eq + PhfHash,
K: PhfBorrow<T>,
{
self.get_entry(key).map(|e| e.0)
}

/// Like `get`, but returns both the key and the value.
pub fn get_entry<T: ?Sized>(&self, key: &T) -> Option<(&K, &V)>
where T: Eq + PhfHash,
K: PhfBorrow<T>
where
T: Eq + PhfHash,
K: PhfBorrow<T>,
{
if self.disps.len() == 0 { return None; } //Prevent panic on empty map
if self.disps.len() == 0 {
return None;
} //Prevent panic on empty map
let hashes = phf_shared::hash(key, &self.key);
let index = phf_shared::get_index(&hashes, &*self.disps, self.entries.len());
let entry = &self.entries[index as usize];
Expand All @@ -95,21 +109,27 @@ impl<K, V> Map<K, V> {
///
/// Entries are returned in an arbitrary but fixed order.
pub fn entries(&self) -> Entries<K, V> {
Entries { iter: self.entries.iter() }
Entries {
iter: self.entries.iter(),
}
}

/// Returns an iterator over the keys in the map.
///
/// Keys are returned in an arbitrary but fixed order.
pub fn keys(&self) -> Keys<K, V> {
Keys { iter: self.entries() }
Keys {
iter: self.entries(),
}
}

/// Returns an iterator over the values in the map.
///
/// Values are returned in an arbitrary but fixed order.
pub fn values(&self) -> Values<K, V> {
Values { iter: self.entries() }
Values {
iter: self.entries(),
}
}
}

Expand Down
62 changes: 42 additions & 20 deletions phf/src/ordered_map.rs
@@ -1,9 +1,9 @@
//! An order-preserving immutable map constructed at compile time.
use core::fmt;
use core::iter::IntoIterator;
use core::ops::Index;
use core::fmt;
use core::slice;
use phf_shared::{self, PhfHash, PhfBorrow, HashKey};
use phf_shared::{self, HashKey, PhfBorrow, PhfHash};

use crate::Slice;

Expand All @@ -28,13 +28,21 @@ pub struct OrderedMap<K: 'static, V: 'static> {
pub entries: Slice<(K, V)>,
}

impl<K, V> fmt::Debug for OrderedMap<K, V> where K: fmt::Debug, V: fmt::Debug {
impl<K, V> fmt::Debug for OrderedMap<K, V>
where
K: fmt::Debug,
V: fmt::Debug,
{
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_map().entries(self.entries()).finish()
}
}

impl<'a, K, V, T: ?Sized> Index<&'a T> for OrderedMap<K, V> where T: Eq + PhfHash, K: PhfBorrow<T> {
impl<'a, K, V, T: ?Sized> Index<&'a T> for OrderedMap<K, V>
where
T: Eq + PhfHash,
K: PhfBorrow<T>,
{
type Output = V;

fn index(&self, k: &'a T) -> &V {
Expand All @@ -55,8 +63,9 @@ impl<K, V> OrderedMap<K, V> {

/// Returns a reference to the value that `key` maps to.
pub fn get<T: ?Sized>(&self, key: &T) -> Option<&V>
where T: Eq + PhfHash,
K: PhfBorrow<T>
where
T: Eq + PhfHash,
K: PhfBorrow<T>,
{
self.get_entry(key).map(|e| e.1)
}
Expand All @@ -66,25 +75,28 @@ impl<K, V> OrderedMap<K, V> {
///
/// This can be useful for interning schemes.
pub fn get_key<T: ?Sized>(&self, key: &T) -> Option<&K>
where T: Eq + PhfHash,
K: PhfBorrow<T>
where
T: Eq + PhfHash,
K: PhfBorrow<T>,
{
self.get_entry(key).map(|e| e.0)
}

/// Determines if `key` is in the `Map`.
pub fn contains_key<T: ?Sized>(&self, key: &T) -> bool
where T: Eq + PhfHash,
K: PhfBorrow<T>
where
T: Eq + PhfHash,
K: PhfBorrow<T>,
{
self.get(key).is_some()
}

/// Returns the index of the key within the list used to initialize
/// the ordered map.
pub fn get_index<T: ?Sized>(&self, key: &T) -> Option<usize>
where T: Eq + PhfHash,
K: PhfBorrow<T>
where
T: Eq + PhfHash,
K: PhfBorrow<T>,
{
self.get_internal(key).map(|(i, _)| i)
}
Expand All @@ -97,17 +109,21 @@ impl<K, V> OrderedMap<K, V> {

/// Like `get`, but returns both the key and the value.
pub fn get_entry<T: ?Sized>(&self, key: &T) -> Option<(&K, &V)>
where T: Eq + PhfHash,
K: PhfBorrow<T>
where
T: Eq + PhfHash,
K: PhfBorrow<T>,
{
self.get_internal(key).map(|(_, e)| e)
}

fn get_internal<T: ?Sized>(&self, key: &T) -> Option<(usize, (&K, &V))>
where T: Eq + PhfHash,
K: PhfBorrow<T>
where
T: Eq + PhfHash,
K: PhfBorrow<T>,
{
if self.disps.len() == 0 { return None; } //Prevent panic on empty map
if self.disps.len() == 0 {
return None;
} //Prevent panic on empty map
let hashes = phf_shared::hash(key, &self.key);
let idx_index = phf_shared::get_index(&hashes, &*self.disps, self.idxs.len());
let idx = self.idxs[idx_index as usize];
Expand All @@ -125,21 +141,27 @@ impl<K, V> OrderedMap<K, V> {
///
/// Entries are returned in the same order in which they were defined.
pub fn entries(&self) -> Entries<K, V> {
Entries { iter: self.entries.iter() }
Entries {
iter: self.entries.iter(),
}
}

/// Returns an iterator over the keys in the map.
///
/// Keys are returned in the same order in which they were defined.
pub fn keys(&self) -> Keys<K, V> {
Keys { iter: self.entries() }
Keys {
iter: self.entries(),
}
}

/// Returns an iterator over the values in the map.
///
/// Values are returned in the same order in which they were defined.
pub fn values(&self) -> Values<K, V> {
Values { iter: self.entries() }
Values {
iter: self.entries(),
}
}
}

Expand Down
33 changes: 22 additions & 11 deletions phf/src/ordered_set.rs
@@ -1,8 +1,8 @@
//! An order-preserving immutable set constructed at compile time.
use core::iter::IntoIterator;
use crate::{ordered_map, OrderedMap, PhfHash};
use core::fmt;
use core::iter::IntoIterator;
use phf_shared::PhfBorrow;
use crate::{ordered_map, PhfHash, OrderedMap};

/// An order-preserving immutable set constructed at compile time.
///
Expand All @@ -19,7 +19,10 @@ pub struct OrderedSet<T: 'static> {
pub map: OrderedMap<T, ()>,
}

impl<T> fmt::Debug for OrderedSet<T> where T: fmt::Debug {
impl<T> fmt::Debug for OrderedSet<T>
where
T: fmt::Debug,
{
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_set().entries(self).finish()
}
Expand All @@ -41,17 +44,19 @@ impl<T> OrderedSet<T> {
///
/// This can be useful for interning schemes.
pub fn get_key<U: ?Sized>(&self, key: &U) -> Option<&T>
where U: Eq + PhfHash,
T: PhfBorrow<U>
where
U: Eq + PhfHash,
T: PhfBorrow<U>,
{
self.map.get_key(key)
}

/// Returns the index of the key within the list used to initialize
/// the ordered set.
pub fn get_index<U: ?Sized>(&self, key: &U) -> Option<usize>
where U: Eq + PhfHash,
T: PhfBorrow<U>
where
U: Eq + PhfHash,
T: PhfBorrow<U>,
{
self.map.get_index(key)
}
Expand All @@ -64,8 +69,9 @@ impl<T> OrderedSet<T> {

/// Returns true if `value` is in the `Set`.
pub fn contains<U: ?Sized>(&self, value: &U) -> bool
where U: Eq + PhfHash,
T: PhfBorrow<U>
where
U: Eq + PhfHash,
T: PhfBorrow<U>,
{
self.map.contains_key(value)
}
Expand All @@ -74,11 +80,16 @@ impl<T> OrderedSet<T> {
///
/// Values are returned in the same order in which they were defined.
pub fn iter(&self) -> Iter<T> {
Iter { iter: self.map.keys() }
Iter {
iter: self.map.keys(),
}
}
}

impl<T> OrderedSet<T> where T: Eq + PhfHash + PhfBorrow<T> {
impl<T> OrderedSet<T>
where
T: Eq + PhfHash + PhfBorrow<T>,
{
/// Returns true if `other` shares no elements with `self`.
#[inline]
pub fn is_disjoint(&self, other: &OrderedSet<T>) -> bool {
Expand Down

0 comments on commit dd86c6c

Please sign in to comment.