Skip to content

Commit

Permalink
Finalize refactoring of Value wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
DoumanAsh committed Nov 24, 2023
1 parent 163ba1b commit 9c3f66c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
19 changes: 10 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
//! ```

#![warn(missing_docs)]
#![allow(private_interfaces)]
#![cfg_attr(feature = "cargo-clippy", allow(clippy::style))]

#[cfg(not(debug_assertions))]
Expand All @@ -45,7 +46,7 @@ macro_rules! unreach {
}

mod typ;
pub use typ::Type;
pub use typ::{Type, RawType};
mod value;
pub use value::Value;
mod hash;
Expand Down Expand Up @@ -115,25 +116,25 @@ impl TypeMap {
#[inline]
///Access element in the map, returning reference to it, if present
pub fn get<T: Type>(&self) -> Option<&T> {
self.get_raw::<T>().map(Value::downcast_ref)
self.inner.get(&T::id()).map(|raw| Value::<T>::new_inner_ref(raw).downcast_ref())
}

#[inline]
///Access element in the map, returning reference to it, if present
pub fn get_raw<T: Type>(&self) -> Option<&Value<T>> {
self.inner.get(&T::id()).map(Value::new_inner_ref)
pub fn get_raw(&self, id: &Key) -> Option<&Value<RawType>> {
self.inner.get(id).map(Value::new_inner_ref)
}

#[inline]
///Access element in the map, returning mutable reference to it, if present
pub fn get_mut<T: Type>(&mut self) -> Option<&mut T> {
self.get_mut_raw::<T>().map(Value::downcast_mut)
self.inner.get_mut(&T::id()).map(|raw| Value::<T>::new_inner_mut(raw).downcast_mut())
}

#[inline]
///Access element in the map, returning mutable reference to it, if present
pub fn get_mut_raw<T: Type>(&mut self) -> Option<&mut Value<T>> {
self.inner.get_mut(&T::id()).map(Value::new_inner_mut)
pub fn get_mut_raw(&mut self, id: &Key) -> Option<&mut Value<RawType>> {
self.inner.get_mut(id).map(Value::new_inner_mut)
}

#[inline]
Expand Down Expand Up @@ -189,8 +190,8 @@ impl TypeMap {

#[inline]
///Attempts to remove element from the map, returning boxed `Some` if it is present.
pub fn remove_raw<T: Type>(&mut self) -> Option<Value<T>> {
self.inner.remove(&T::id()).map(Value::new_inner)
pub fn remove_raw(&mut self, id: &Key) -> Option<Value<RawType>> {
self.inner.remove(id).map(Value::new_inner)
}

#[inline]
Expand Down
3 changes: 3 additions & 0 deletions src/typ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ pub trait Type: 'static + Send + Sync {
}

impl<T: 'static + Send + Sync> Type for T {}

///Tag to indicate Raw boxed value
pub struct RawType;
42 changes: 41 additions & 1 deletion src/value.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{Type, ValueBox};
use crate::typ::{Type, RawType};
use crate::ValueBox;

use core::marker::PhantomData;

Expand All @@ -9,6 +10,29 @@ pub struct Value<T> {
_typ: PhantomData<T>
}

impl Value<RawType> {
#[inline]
///Attempts downcast self into specified type
pub fn try_downcast<O: Type>(self) -> Result<Box<O>, Self> {
match self.inner.downcast() {
Ok(res) => Ok(res),
Err(inner) => Err(Self::new_inner(inner)),
}
}

#[inline]
///Attempts to downcast self into concrete type
pub fn try_downcast_ref<O: Type>(&self) -> Option<&O> {
self.inner.downcast_ref()
}

#[inline]
///Attempts to downcast self into concrete type
pub fn try_downcast_mut<O: Type>(&mut self) -> Option<&mut O> {
self.inner.downcast_mut()
}
}

impl<T: Type> Value<T> {
#[inline(always)]
///Creates new raw Value trusting user to specify correct type
Expand Down Expand Up @@ -47,15 +71,26 @@ impl<T: Type> Value<T> {
#[inline]
///Downcasts self into concrete type
pub fn downcast(self) -> Box<T> {
//dum dum no specialization
if T::id() == RawType::id() {
panic!("Raw box cannot use this method")
}

match self.inner.downcast() {
Ok(res) => res,
Err(_) => unreach!(),
}
}


#[inline]
///Downcasts self into concrete type
pub fn downcast_ref(&self) -> &T {
//dum dum no specialization
if T::id() == RawType::id() {
panic!("Raw box cannot use this method")
}

match self.inner.downcast_ref() {
Some(res) => res,
None => unreach!(),
Expand All @@ -65,6 +100,11 @@ impl<T: Type> Value<T> {
#[inline]
///Downcasts self into concrete type
pub fn downcast_mut(&mut self) -> &mut T {
//dum dum no specialization
if T::id() == RawType::id() {
panic!("Raw box cannot use this method")
}

match self.inner.downcast_mut() {
Some(res) => res,
None => unreach!(),
Expand Down

0 comments on commit 9c3f66c

Please sign in to comment.