Skip to content

Commit

Permalink
Merge pull request #13 from ArtBlnd/ci-init
Browse files Browse the repository at this point in the history
Initialize CI & Formatting
  • Loading branch information
ArtBlnd authored Sep 29, 2023
2 parents c5c9b27 + 4491ffa commit 025aefb
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 86 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: CI

on:
push:
branches: ["master"]
pull_request:
branches: ["master"]

env:
CARGO_TERM_COLOR: always

jobs:
format:
name: rustfmt
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
components: rustfmt, clippy
- uses: actions-rs/cargo@v1
with:
command: fmt
args: -- --check

test:
name: cargo test
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- uses: actions-rs/cargo@v1
with:
command: test
args: --all-targets --all-features
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
//! ```
//! use std::thread::sleep;
//! use std::time::Duration;
//!
//!
//! use endorphin::policy::TTLPolicy;
//! use endorphin::HashMap;
//!
//!
//! fn main() {
//! let mut cache = HashMap::new(TTLPolicy::new());
//!
//!
//! cache.insert("Still", "Alive", Duration::from_secs(3));
//! cache.insert("Gonna", "Die", Duration::from_secs(1));
//!
//!
//! sleep(Duration::from_secs(1));
//!
//!
//! assert_eq!(cache.get(&"Still"), Some(&"Alive"));
//! assert_eq!(cache.get(&"Gonna"), None);
//!}
//! }
//! ```
//! For more examples, visit [here]
//!
Expand Down
86 changes: 43 additions & 43 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ where
}

if let Some(backlog) = self.exp_backlog.pop() {
for entry_id in backlog.into_iter().filter_map(|v| v) {
for entry_id in backlog.into_iter().flatten() {
// bucket could already removed and its empty storage.
if let Some(bucket) = self.exp_bucket_table.release_slot(entry_id) {
unsafe {
Expand All @@ -162,7 +162,7 @@ where
) -> Bucket<(K, V, Storage<P::Storage>)> {
let mut has_backlog = false;
while let Some(backlog) = self.exp_backlog.pop() {
for entry_id in backlog.into_iter().filter_map(|v| v) {
for entry_id in backlog.into_iter().flatten() {
// bucket could already removed and its empty storage.
if let Some(bucket) = self.exp_bucket_table.release_slot(entry_id) {
unsafe {
Expand Down Expand Up @@ -208,7 +208,7 @@ where
Command::Noop => return,
}

removed.iter().cloned().filter_map(|v| v).for_each(|v| {
removed.iter().cloned().flatten().for_each(|v| {
if let Some(bucket) = self.exp_bucket_table.get(v) {
let (_, _, s) = unsafe { bucket.as_ref() };

Expand Down Expand Up @@ -266,7 +266,7 @@ where
self.exp_bucket_table
.set_bucket(s.entry_id, Some(bucket.clone()));

self.handle_status(self.exp_policy.on_insert(s.entry_id, &mut s.storage));
self.handle_status(self.exp_policy.on_insert(s.entry_id, &s.storage));

(bucket, old_v)
}
Expand Down Expand Up @@ -298,18 +298,18 @@ where
K: Borrow<Q>,
Q: Hash + Eq,
{
let hash = make_hash::<K, Q, H>(&self.hash_builder, &k);
let hash = make_hash::<K, Q, H>(&self.hash_builder, k);
let bucket = match self.table.find(hash, equivalent_key(k)) {
Some(bucket) => bucket,
None => return None,
};

// fire on_insert event.
let (_, v, s) = unsafe { bucket.as_mut() };
self.handle_status(self.exp_policy.on_access(s.entry_id, &mut s.storage));
self.handle_status(self.exp_policy.on_access(s.entry_id, &s.storage));

// don't give access to entry if entry expired.
if self.exp_policy.is_expired(s.entry_id, &mut s.storage) || unlikely(s.is_removed()) {
if self.exp_policy.is_expired(s.entry_id, &s.storage) || unlikely(s.is_removed()) {
None
} else {
Some(v)
Expand Down Expand Up @@ -344,18 +344,18 @@ where
K: Borrow<Q>,
Q: Hash + Eq,
{
let hash = make_hash::<K, Q, H>(&self.hash_builder, &k);
let hash = make_hash::<K, Q, H>(&self.hash_builder, k);
let bucket = match self.table.find(hash, equivalent_key(k)) {
Some(bucket) => bucket,
None => return None,
};

// fire on_insert event.
let (k, v, s) = unsafe { bucket.as_mut() };
self.handle_status(self.exp_policy.on_access(s.entry_id, &mut s.storage));
self.handle_status(self.exp_policy.on_access(s.entry_id, &s.storage));

// don't give access to entry if entry expired.
if self.exp_policy.is_expired(s.entry_id, &mut s.storage) || unlikely(s.is_removed()) {
if self.exp_policy.is_expired(s.entry_id, &s.storage) || unlikely(s.is_removed()) {
None
} else {
Some((k, v))
Expand Down Expand Up @@ -391,18 +391,18 @@ where
K: Borrow<Q>,
Q: Hash + Eq,
{
let hash = make_hash::<K, Q, H>(&self.hash_builder, &k);
let hash = make_hash::<K, Q, H>(&self.hash_builder, k);
let bucket = match self.table.find(hash, equivalent_key(k)) {
Some(bucket) => bucket,
None => return None,
};

// fire on_insert event.
let (_, v, s) = unsafe { bucket.as_mut() };
self.handle_status(self.exp_policy.on_access(s.entry_id, &mut s.storage));
self.handle_status(self.exp_policy.on_access(s.entry_id, &s.storage));

// don't give access to entry if entry expired.
if self.exp_policy.is_expired(s.entry_id, &mut s.storage) || unlikely(s.is_removed()) {
if self.exp_policy.is_expired(s.entry_id, &s.storage) || unlikely(s.is_removed()) {
None
} else {
Some(v)
Expand Down Expand Up @@ -464,7 +464,7 @@ where
pub fn insert(&mut self, k: K, v: V, init: P::Info) -> Option<V> {
let (_, old_v) = unsafe { self.raw_insert(k, v, init) };

return old_v;
old_v
}

/// Removes a key from the `HashMap`, returning the value at the key if the key was previously in the `HashMap`.
Expand Down Expand Up @@ -499,7 +499,7 @@ where
self.process_single_backlog();

// Avoid `Option::map` because it bloats LLVM IR.
let hash = make_hash::<K, Q, H>(&self.hash_builder, &k);
let hash = make_hash::<K, Q, H>(&self.hash_builder, k);
let entry = match self.table.remove_entry(hash, equivalent_key(k)) {
Some((_, v, s)) => {
self.exp_bucket_table.set_bucket(s.entry_id, None);
Expand All @@ -513,7 +513,7 @@ where
None => None,
};

return entry;
entry
}

/// Removes a key from the `HashMap`, returning the stored key and value if the key was previously in the `HashMap`.
Expand Down Expand Up @@ -547,7 +547,7 @@ where
self.process_single_backlog();

// Avoid `Option::map` because it bloats LLVM IR.
let hash = make_hash::<K, Q, H>(&self.hash_builder, &k);
let hash = make_hash::<K, Q, H>(&self.hash_builder, k);
let entry = match self.table.remove_entry(hash, equivalent_key(k)) {
Some((k, v, s)) => {
self.exp_bucket_table.set_bucket(s.entry_id, None);
Expand All @@ -561,7 +561,7 @@ where
None => None,
};

return entry;
entry
}

#[inline]
Expand Down Expand Up @@ -1553,7 +1553,7 @@ where
pub fn get(&self) -> &V {
let (_, v, s) = unsafe { self.elem.as_mut() };
self.table
.handle_status(self.table.exp_policy.on_access(s.entry_id, &mut s.storage));
.handle_status(self.table.exp_policy.on_access(s.entry_id, &s.storage));
v
}

Expand Down Expand Up @@ -1587,7 +1587,7 @@ where
pub fn get_mut(&mut self) -> &mut V {
let (_, v, s) = unsafe { self.elem.as_mut() };
self.table
.handle_status(self.table.exp_policy.on_access(s.entry_id, &mut s.storage));
.handle_status(self.table.exp_policy.on_access(s.entry_id, &s.storage));
v
}

Expand Down Expand Up @@ -1621,7 +1621,7 @@ where
pub fn into_mut(self) -> &'a mut V {
let (_, v, s) = unsafe { self.elem.as_mut() };
self.table
.handle_status(self.table.exp_policy.on_access(s.entry_id, &mut s.storage));
.handle_status(self.table.exp_policy.on_access(s.entry_id, &s.storage));
v
}

Expand Down Expand Up @@ -1649,12 +1649,12 @@ where
let k = unsafe { &self.elem.as_ref().0 };

let s = self.table.exp_policy.init_storage(init);
let mut storage = Storage::new(s, self.table.exp_bucket_table.acquire_slot());
let storage = Storage::new(s, self.table.exp_bucket_table.acquire_slot());

self.table.handle_status(
self.table
.exp_policy
.on_insert(storage.entry_id, &mut storage.storage),
.on_insert(storage.entry_id, &storage.storage),
);

let (_, old_v, old_s) = self
Expand Down Expand Up @@ -1736,7 +1736,7 @@ where
self.table.handle_status(
self.table
.exp_policy
.on_access(entry.2.entry_id, &mut entry.2.storage),
.on_access(entry.2.entry_id, &entry.2.storage),
);

let old_key = mem::replace(&mut entry.0, self.key.unwrap());
Expand Down Expand Up @@ -1779,7 +1779,7 @@ where
self.table.handle_status(
self.table
.exp_policy
.on_access(entry.2.entry_id, &mut entry.2.storage),
.on_access(entry.2.entry_id, &entry.2.storage),
);

mem::replace(&mut entry.0, self.key.unwrap())
Expand Down Expand Up @@ -1857,7 +1857,7 @@ where
.exp_bucket_table
.set_bucket(s.entry_id, Some(elem.clone()));
self.table
.handle_status(self.table.exp_policy.on_access(s.entry_id, &mut s.storage));
.handle_status(self.table.exp_policy.on_access(s.entry_id, &s.storage));

if let Some(key) = spare_key {
Entry::Vacant(VacantEntry {
Expand Down Expand Up @@ -2110,34 +2110,34 @@ mod test_map {
fn test_contains_key() {
let mut map = HashMap::new(MockPolicy::new());

assert_eq!(map.contains_key(&0), false);
assert_eq!(map.contains_key(&1), false);
assert!(!map.contains_key(&0));
assert!(!map.contains_key(&1));

assert!(map.insert(0, 0, ()).is_none());

assert_eq!(map.contains_key(&0), true);
assert_eq!(map.contains_key(&1), false);
assert!(map.contains_key(&0));
assert!(!map.contains_key(&1));

assert!(map.remove(&0).is_some());

assert_eq!(map.contains_key(&0), false);
assert!(!map.contains_key(&0));
}

#[test]
fn test_remove() {
let mut map = HashMap::new(MockPolicy::new());

assert_eq!(map.contains_key(&0), false);
assert!(!map.contains_key(&0));

assert!(map.insert(0, 1, ()).is_none());

assert_eq!(map.contains_key(&0), true);
assert!(map.contains_key(&0));

assert_eq!(map.remove(&0).unwrap(), 1);

assert_eq!(map.contains_key(&0), false);
assert!(!map.contains_key(&0));

assert_eq!(map.contains_key(&10), false);
assert!(!map.contains_key(&10));

assert_eq!(map.remove(&10), None);
}
Expand All @@ -2146,15 +2146,15 @@ mod test_map {
fn test_remove_entry() {
let mut map = HashMap::new(MockPolicy::new());

assert_eq!(map.contains_key(&0), false);
assert!(!map.contains_key(&0));

assert!(map.insert(0, 1, ()).is_none());

assert_eq!(map.contains_key(&0), true);
assert!(map.contains_key(&0));

assert_eq!(map.remove_entry(&0).unwrap(), (0, 1));

assert_eq!(map.contains_key(&0), false);
assert!(!map.contains_key(&0));
}

#[test]
Expand Down Expand Up @@ -2278,13 +2278,13 @@ mod test_map {
map.insert(0, 0, ());

match map.entry(0) {
Entry::Occupied(_) => assert!(true),
Entry::Occupied(_) => (),
Entry::Vacant(_) => unreachable!(),
}

match map.entry(1) {
Entry::Occupied(_) => unreachable!(),
Entry::Vacant(_) => assert!(true),
Entry::Vacant(_) => (),
}
}

Expand Down Expand Up @@ -2352,8 +2352,8 @@ mod test_map {

let now = map
.entry(0)
.and_modify(|v| *v = *v + 5)
.and_modify(|v| *v = *v * 10);
.and_modify(|v| *v += 5)
.and_modify(|v| *v *= 10);
assert_eq!(now.or_insert(0, ()), &50);
assert_eq!(map.get(&0).unwrap(), &50);
}
Expand Down Expand Up @@ -2418,7 +2418,7 @@ mod test_map {
assert_eq!(v, &10);
*v += 10;

let v = entry.get_mut(); // not moved
let _v = entry.get_mut(); // not moved
}
Entry::Vacant(_) => unreachable!(),
}
Expand Down
6 changes: 6 additions & 0 deletions src/policy/mixed_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ impl MixedPolicy {
}
}

impl Default for MixedPolicy {
fn default() -> Self {
Self::new()
}
}

impl ExpirePolicy for MixedPolicy {
type Info = Expiration;
type Storage = Storage;
Expand Down
Loading

0 comments on commit 025aefb

Please sign in to comment.