-
Notifications
You must be signed in to change notification settings - Fork 7
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
refactor: simplify some generic type definition #85
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,48 +12,55 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use crate::core::{ | ||
adapter::{Adapter, Link}, | ||
pointer::PointerOps, | ||
}; | ||
use crate::core::{adapter::Adapter, pointer::PointerOps}; | ||
|
||
use std::fmt::Debug; | ||
|
||
pub trait Config = Send + Sync + 'static + Debug + Clone; | ||
|
||
pub trait EvictionPolicy<A>: Send + Sync + 'static | ||
where | ||
A: Adapter<Link = Self::Link>, | ||
<<A as Adapter>::PointerOps as PointerOps>::Pointer: Clone, | ||
{ | ||
type Link: Link; | ||
pub trait EvictionPolicy: Send + Sync + 'static { | ||
type Adapter: Adapter; | ||
type Config: Config; | ||
type E<'e>: Iterator<Item = &'e <A::PointerOps as PointerOps>::Pointer>; | ||
|
||
fn new(config: Self::Config) -> Self; | ||
|
||
fn insert(&mut self, ptr: <A::PointerOps as PointerOps>::Pointer); | ||
fn insert(&mut self, ptr: <<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer); | ||
|
||
fn remove( | ||
&mut self, | ||
ptr: &<A::PointerOps as PointerOps>::Pointer, | ||
) -> <A::PointerOps as PointerOps>::Pointer; | ||
ptr: &<<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer, | ||
) -> <<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer; | ||
|
||
fn access(&mut self, ptr: &<A::PointerOps as PointerOps>::Pointer); | ||
fn access(&mut self, ptr: &<<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer); | ||
|
||
fn len(&self) -> usize; | ||
|
||
fn is_empty(&self) -> bool { | ||
self.len() == 0 | ||
} | ||
|
||
fn iter(&self) -> Self::E<'_>; | ||
fn iter( | ||
&self, | ||
) -> impl Iterator<Item = &'_ <<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer> + '_; | ||
} | ||
|
||
pub trait EvictionPolicyExt: EvictionPolicy { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea! |
||
fn push(&mut self, ptr: <<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer); | ||
|
||
fn pop(&mut self) -> Option<<<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer>; | ||
|
||
fn push(&mut self, ptr: <A::PointerOps as PointerOps>::Pointer) { | ||
fn peek(&self) -> Option<&<<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer>; | ||
} | ||
|
||
impl<E: EvictionPolicy> EvictionPolicyExt for E | ||
where | ||
<<E::Adapter as Adapter>::PointerOps as PointerOps>::Pointer: Clone, | ||
{ | ||
fn push(&mut self, ptr: <<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer) { | ||
self.insert(ptr) | ||
} | ||
|
||
fn pop(&mut self) -> Option<<A::PointerOps as PointerOps>::Pointer> { | ||
fn pop(&mut self) -> Option<<<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer> { | ||
let ptr = { | ||
let mut iter = self.iter(); | ||
let ptr = iter.next(); | ||
|
@@ -62,7 +69,7 @@ where | |
ptr.map(|ptr| self.remove(&ptr)) | ||
} | ||
|
||
fn peek(&self) -> Option<&<A::PointerOps as PointerOps>::Pointer> { | ||
fn peek(&self) -> Option<&<<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer> { | ||
self.iter().next() | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
#![feature(associated_type_bounds)] | ||
#![feature(ptr_metadata)] | ||
#![feature(trait_alias)] | ||
#![feature(return_position_impl_trait_in_trait)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting feature! |
||
#![allow(clippy::new_without_default)] | ||
#![allow(clippy::wrong_self_convention)] | ||
#![allow(clippy::vtable_address_comparisons)] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,12 +25,9 @@ use parking_lot::Mutex; | |
use std::hash::Hasher; | ||
use twox_hash::XxHash64; | ||
|
||
pub struct CacheConfig<K, V, E, EL> | ||
pub struct CacheConfig<E> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice save! |
||
where | ||
K: Key, | ||
V: Value, | ||
E: EvictionPolicy<CacheItemEpAdapter<K, V, EL>, Link = EL>, | ||
EL: Link, | ||
E: EvictionPolicy, | ||
{ | ||
capacity: usize, | ||
shard_bits: usize, | ||
|
@@ -42,7 +39,7 @@ pub struct Cache<K, V, E, EL> | |
where | ||
K: Key, | ||
V: Value, | ||
E: EvictionPolicy<CacheItemEpAdapter<K, V, EL>, Link = EL>, | ||
E: EvictionPolicy<Adapter = CacheItemEpAdapter<K, V, EL>>, | ||
EL: Link, | ||
{ | ||
shards: Vec<Mutex<CacheShard<K, V, E, EL>>>, | ||
|
@@ -52,7 +49,7 @@ struct CacheShard<K, V, E, EL> | |
where | ||
K: Key, | ||
V: Value, | ||
E: EvictionPolicy<CacheItemEpAdapter<K, V, EL>, Link = EL>, | ||
E: EvictionPolicy<Adapter = CacheItemEpAdapter<K, V, EL>>, | ||
EL: Link, | ||
{ | ||
container: HashMap<K, V, CacheItemHmAdapter<K, V, EL>>, | ||
|
@@ -115,10 +112,10 @@ impl<K, V, E, EL> Cache<K, V, E, EL> | |
where | ||
K: Key, | ||
V: Value, | ||
E: EvictionPolicy<CacheItemEpAdapter<K, V, EL>, Link = EL>, | ||
E: EvictionPolicy<Adapter = CacheItemEpAdapter<K, V, EL>>, | ||
EL: Link, | ||
{ | ||
pub fn new(config: CacheConfig<K, V, E, EL>) -> Self { | ||
pub fn new(config: CacheConfig<E>) -> Self { | ||
let mut shards = Vec::with_capacity(1 << config.shard_bits); | ||
|
||
let shard_capacity = config.capacity / (1 << config.shard_bits); | ||
|
@@ -231,7 +228,7 @@ mod tests { | |
} | ||
} | ||
|
||
type FifoCacheConfig = CacheConfig<K, V, Fifo<CacheItemEpAdapter<K, V, FifoLink>>, FifoLink>; | ||
type FifoCacheConfig = CacheConfig<Fifo<CacheItemEpAdapter<K, V, FifoLink>>>; | ||
type FifoCache = Cache<K, V, Fifo<CacheItemEpAdapter<K, V, FifoLink>>, FifoLink>; | ||
|
||
#[test] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Is the empty line between automated added by IDE? Or some format setting needs to be enabled? I'll enable it by default.