From 573754c3a3bf11626d599998b138f3fe20e6f3dc Mon Sep 17 00:00:00 2001 From: zephyr Date: Wed, 20 Apr 2022 19:26:39 +0900 Subject: [PATCH] add type cast --- Cargo.lock | 2 +- kaminari/Cargo.toml | 2 +- kaminari/src/mix.rs | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4c00d6a..7f447fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -144,7 +144,7 @@ dependencies = [ [[package]] name = "kaminari" -version = "0.5.7" +version = "0.6.0" dependencies = [ "lazy_static", "lightws", diff --git a/kaminari/Cargo.toml b/kaminari/Cargo.toml index fdecaed..6a67b4a 100644 --- a/kaminari/Cargo.toml +++ b/kaminari/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kaminari" -version = "0.5.7" +version = "0.6.0" edition = "2021" authors = ["zephyr "] keywords = ["lightws", "network"] diff --git a/kaminari/src/mix.rs b/kaminari/src/mix.rs index f2c8e3b..6040919 100644 --- a/kaminari/src/mix.rs +++ b/kaminari/src/mix.rs @@ -218,3 +218,38 @@ mod stream { impl_async_read!(MixServerStream); impl_async_write!(MixServerStream); } + +// ========== type cast ========== + +macro_rules! impl_type_cast { + ($mix: ident || $([$func: ident :: $member: ident => $ret: ty], )+ ) => { + impl $mix { + $( + pub fn $func(&self) -> Option<&$ret> { + use $mix::*; + if let $member(x) = self { + Some(x) + } else { + None + } + } + )+ + } + }; +} + +impl_type_cast!( + MixConnect || + [as_plain :: Plain => NopConnect], + [as_ws :: Ws => WsConnect], + [as_tls :: Tls => TlsConnect], + [as_wss :: Wss => WsConnect>], +); + +impl_type_cast!( + MixAccept || + [as_plain :: Plain => NopAccept], + [as_ws :: Ws => WsAccept], + [as_tls :: Tls => TlsAccept], + [as_wss :: Wss => WsAccept>], +);