forked from rwf2/Rocket
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6857b82
commit 56e7fa6
Showing
15 changed files
with
212 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
mod catcher; | ||
mod handler; | ||
mod types; | ||
|
||
pub use catcher::*; | ||
pub use handler::*; | ||
pub use types::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use transient::{Any, CanRecoverFrom, Co, Transient, Downcast}; | ||
|
||
pub type ErasedError<'r> = Box<dyn Any<Co<'r>> + Send + Sync + 'r>; | ||
pub type ErasedErrorRef<'r> = dyn Any<Co<'r>> + Send + Sync + 'r; | ||
|
||
pub fn default_error_type<'r>() -> ErasedError<'r> { | ||
Box::new(()) | ||
} | ||
|
||
pub fn downcast<'a, 'r, T: Transient + 'r>(v: &'a ErasedErrorRef<'r>) -> Option<&'a T> | ||
where T::Transience: CanRecoverFrom<Co<'r>> | ||
{ | ||
v.downcast_ref() | ||
} | ||
|
||
// /// Chosen not to expose this macro, since it's pretty short and sweet | ||
// #[doc(hidden)] | ||
// #[macro_export] | ||
// macro_rules! resolve_typed_catcher { | ||
// ($T:expr) => ({ | ||
// #[allow(unused_imports)] | ||
// use $crate::catcher::types::Resolve; | ||
// | ||
// Resolve::new($T).cast() | ||
// }) | ||
// } | ||
|
||
// pub use resolve_typed_catcher; | ||
|
||
pub mod resolution { | ||
use std::marker::PhantomData; | ||
|
||
use transient::{CanTranscendTo, Transient}; | ||
|
||
use super::*; | ||
|
||
/// The *magic*. | ||
/// | ||
/// `Resolve<T>::item` for `T: Transient` is `<T as Transient>::item`. | ||
/// `Resolve<T>::item` for `T: !Transient` is `DefaultTypeErase::item`. | ||
/// | ||
/// This _must_ be used as `Resolve::<T>:item` for resolution to work. This | ||
/// is a fun, static dispatch hack for "specialization" that works because | ||
/// Rust prefers inherent methods over blanket trait impl methods. | ||
pub struct Resolve<'r, T: 'r>(T, PhantomData<&'r ()>); | ||
|
||
impl<'r, T: 'r> Resolve<'r, T> { | ||
pub fn new(val: T) -> Self { | ||
Self(val, PhantomData) | ||
} | ||
} | ||
|
||
/// Fallback trait "implementing" `Transient` for all types. This is what | ||
/// Rust will resolve `Resolve<T>::item` to when `T: !Transient`. | ||
pub trait DefaultTypeErase<'r>: Sized { | ||
const SPECIALIZED: bool = false; | ||
|
||
fn cast(self) -> ErasedError<'r> { Box::new(()) } | ||
} | ||
|
||
impl<'r, T: 'r> DefaultTypeErase<'r> for Resolve<'r, T> {} | ||
|
||
/// "Specialized" "implementation" of `Transient` for `T: Transient`. This is | ||
/// what Rust will resolve `Resolve<T>::item` to when `T: Transient`. | ||
impl<'r, T: Transient + Send + Sync + 'r> Resolve<'r, T> | ||
where T::Transience: CanTranscendTo<Co<'r>> | ||
{ | ||
pub const SPECIALIZED: bool = true; | ||
|
||
pub fn cast(self) -> ErasedError<'r> { Box::new(self.0) } | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
// use std::any::TypeId; | ||
|
||
use transient::{Transient, TypeId}; | ||
|
||
use super::resolution::{Resolve, DefaultTypeErase}; | ||
|
||
struct NotAny; | ||
#[derive(Transient)] | ||
struct YesAny; | ||
|
||
#[test] | ||
fn check_can_determine() { | ||
let not_any = Resolve::new(NotAny).cast(); | ||
assert_eq!(not_any.type_id(), TypeId::of::<()>()); | ||
|
||
let yes_any = Resolve::new(YesAny).cast(); | ||
assert_ne!(yes_any.type_id(), TypeId::of::<()>()); | ||
} | ||
|
||
// struct HasSentinel<T>(T); | ||
|
||
// #[test] | ||
// fn parent_works() { | ||
// let child = resolve!(YesASentinel, HasSentinel<YesASentinel>); | ||
// assert!(child.type_name.ends_with("YesASentinel")); | ||
// assert_eq!(child.parent.unwrap(), TypeId::of::<HasSentinel<YesASentinel>>()); | ||
// assert!(child.specialized); | ||
|
||
// let not_a_direct_sentinel = resolve!(HasSentinel<YesASentinel>); | ||
// assert!(not_a_direct_sentinel.type_name.contains("HasSentinel")); | ||
// assert!(not_a_direct_sentinel.type_name.contains("YesASentinel")); | ||
// assert!(not_a_direct_sentinel.parent.is_none()); | ||
// assert!(!not_a_direct_sentinel.specialized); | ||
// } | ||
} |
Oops, something went wrong.