From 613cd7129439d04f576325bcf55d276847efb136 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 24 Jul 2022 11:02:33 +0200 Subject: refactor: clean up intertrait lib --- src/libs/intertrait/cast_box.rs | 10 +++--- src/libs/intertrait/cast_rc.rs | 10 +++--- src/libs/intertrait/hasher.rs | 48 ---------------------------- src/libs/intertrait/mod.rs | 71 +++++++++++++++++++---------------------- 4 files changed, 42 insertions(+), 97 deletions(-) delete mode 100644 src/libs/intertrait/hasher.rs (limited to 'src') diff --git a/src/libs/intertrait/cast_box.rs b/src/libs/intertrait/cast_box.rs index 793efd0..b2ec77e 100644 --- a/src/libs/intertrait/cast_box.rs +++ b/src/libs/intertrait/cast_box.rs @@ -15,16 +15,16 @@ use crate::libs::intertrait::{caster, CastFrom}; pub trait CastBox { - /// Casts a box to this trait into that of type `T`. If fails, returns the receiver. - fn cast(self: Box) -> Result, Box>; + /// Casts a box to this trait into that of `Trait`. If fails, returns the receiver. + fn cast(self: Box) -> Result, Box>; } /// A blanket implementation of `CastBox` for traits extending `CastFrom`. -impl CastBox for S +impl CastBox for CastableFrom { - fn cast(self: Box) -> Result, Box> + fn cast(self: Box) -> Result, Box> { - match caster::((*self).type_id()) { + match caster::((*self).type_id()) { Some(caster) => Ok((caster.cast_box)(self.box_any())), None => Err(self), } diff --git a/src/libs/intertrait/cast_rc.rs b/src/libs/intertrait/cast_rc.rs index 79d8d60..5901b5e 100644 --- a/src/libs/intertrait/cast_rc.rs +++ b/src/libs/intertrait/cast_rc.rs @@ -17,16 +17,16 @@ use crate::libs::intertrait::{caster, CastFrom}; pub trait CastRc { - /// Casts an `Rc` for this trait into that for type `T`. - fn cast(self: Rc) -> Result, Rc>; + /// Casts an `Rc` for this trait into that for `Trait`. + fn cast(self: Rc) -> Result, Rc>; } /// A blanket implementation of `CastRc` for traits extending `CastFrom`. -impl CastRc for S +impl CastRc for CastableFrom { - fn cast(self: Rc) -> Result, Rc> + fn cast(self: Rc) -> Result, Rc> { - match caster::((*self).type_id()) { + match caster::((*self).type_id()) { Some(caster) => Ok((caster.cast_rc)(self.rc_any())), None => Err(self), } diff --git a/src/libs/intertrait/hasher.rs b/src/libs/intertrait/hasher.rs deleted file mode 100644 index e7f110d..0000000 --- a/src/libs/intertrait/hasher.rs +++ /dev/null @@ -1,48 +0,0 @@ -#![allow(clippy::module_name_repetitions)] - -/** - * Originally from Intertrait by CodeChain - * - * - * - * - * Licensed under either of - * - * Apache License, Version 2.0 (LICENSE-APACHE or ) - * MIT license (LICENSE-MIT or ) - - * at your option. -*/ -use std::convert::TryInto; -use std::hash::{BuildHasherDefault, Hasher}; -use std::mem::size_of; - -/// A simple `Hasher` implementation tuned for performance. -#[derive(Default)] -pub struct FastHasher(u64); - -/// A `BuildHasher` for `FastHasher`. -pub type BuildFastHasher = BuildHasherDefault; - -impl Hasher for FastHasher -{ - fn finish(&self) -> u64 - { - self.0 - } - - fn write(&mut self, bytes: &[u8]) - { - let mut bytes = bytes; - while bytes.len() > size_of::() { - let (u64_bytes, remaining) = bytes.split_at(size_of::()); - - self.0 ^= u64::from_ne_bytes(u64_bytes.try_into().unwrap()); - - bytes = remaining; - } - self.0 ^= bytes - .iter() - .fold(0u64, |result, b| (result << 8) | u64::from(*b)); - } -} diff --git a/src/libs/intertrait/mod.rs b/src/libs/intertrait/mod.rs index 1daca64..2cdc67d 100644 --- a/src/libs/intertrait/mod.rs +++ b/src/libs/intertrait/mod.rs @@ -12,64 +12,62 @@ * at your option. */ use std::any::{Any, TypeId}; -use std::collections::HashMap; use std::rc::Rc; -use std::sync::Arc; +use ahash::AHashMap; use linkme::distributed_slice; use once_cell::sync::Lazy; -mod hasher; - -use hasher::BuildFastHasher; - pub mod cast_box; pub mod cast_rc; pub type BoxedCaster = Box; +type CasterFn = fn() -> (TypeId, BoxedCaster); + #[distributed_slice] -pub static CASTERS: [fn() -> (TypeId, BoxedCaster)] = [..]; - -static CASTER_MAP: Lazy> = - Lazy::new(|| { - CASTERS - .iter() - .map(|f| { - let (type_id, caster) = f(); - ((type_id, (*caster).type_id()), caster) - }) - .collect() - }); - -pub struct Caster +pub static CASTERS: [CasterFn] = [..]; + +static CASTER_MAP: Lazy> = Lazy::new(|| { + CASTERS + .iter() + .map(|caster_fn| { + let (type_id, caster) = caster_fn(); + + ((type_id, (*caster).type_id()), caster) + }) + .collect() +}); + +pub struct Caster { /// Casts a `Box` holding a trait object for `Any` to another `Box` holding a trait object - /// for trait `T`. - pub cast_box: fn(from: Box) -> Box, + /// for `Trait`. + pub cast_box: fn(from: Box) -> Box, /// Casts an `Rc` holding a trait object for `Any` to another `Rc` holding a trait object - /// for trait `T`. - pub cast_rc: fn(from: Rc) -> Rc, + /// for `Trait`. + pub cast_rc: fn(from: Rc) -> Rc, } -impl Caster +impl Caster { pub fn new( - cast_box: fn(from: Box) -> Box, - cast_rc: fn(from: Rc) -> Rc, - ) -> Caster + cast_box: fn(from: Box) -> Box, + cast_rc: fn(from: Rc) -> Rc, + ) -> Caster { - Caster:: { cast_box, cast_rc } + Caster:: { cast_box, cast_rc } } } -/// Returns a `Caster` from a concrete type `S` to a trait `T` implemented by it. -fn caster(type_id: TypeId) -> Option<&'static Caster> +/// Returns a `Caster` from a concrete type `Implementation` +/// from inside `CASTER_MAP` to a `Trait` implemented by it. +fn caster(type_id: TypeId) -> Option<&'static Caster> { CASTER_MAP - .get(&(type_id, TypeId::of::>())) - .and_then(|caster| caster.downcast_ref::>()) + .get(&(type_id, TypeId::of::>())) + .and_then(|caster| caster.downcast_ref::>()) } /// `CastFrom` must be extended by a trait that wants to allow for casting into another trait. @@ -92,12 +90,7 @@ pub trait CastFrom: Any + 'static fn rc_any(self: Rc) -> Rc; } -pub trait CastFromSync: CastFrom + Sync + Send + 'static -{ - fn arc_any(self: Arc) -> Arc; -} - -impl CastFrom for T +impl CastFrom for Trait { fn box_any(self: Box) -> Box { -- cgit v1.2.3-18-g5258