SHARE
TWEET

Untitled

a guest Oct 14th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #![feature(const_generics)]
  2.  
  3. trait Helper {
  4.     type T;
  5. }
  6.  
  7. struct ITE<True, False, const B: bool> (
  8.     ::core::marker::PhantomData<(True, False)>,
  9. );
  10.  
  11. impl<True, False> Helper
  12.     for ITE<True, False, {true}>
  13. {
  14.     type T = True;
  15. }
  16.  
  17. impl<True, False> Helper
  18.     for ITE<True, False, {false}>
  19. {
  20.     type T = False;
  21. }
  22.  
  23. struct Foo {
  24.     x: String,
  25. }
  26.  
  27. macro_rules! type_level_conditional {(
  28.     type $T:ident =
  29.         if { $cond:expr } {
  30.             $True:ty
  31.         } else {
  32.             $False:ty
  33.         }
  34.     ;
  35. ) => (
  36.     type $T = <$crate::ITE<i32, f32, {
  37.         ::core::mem::size_of::<Foo>() == 0
  38.     }> as $crate::Helper>::T;
  39. )}
  40.  
  41. type_level_conditional! {
  42.     type T = if { ::core::mem::size_of::<Foo>() == 0 } {
  43.         i32
  44.     } else {
  45.         f32
  46.     };
  47. }
  48.  
  49. fn is_i32 (_: &i32) {}
  50. fn is_f32 (_: &f32) {}
  51.  
  52. fn check (x: T)
  53. {
  54.     is_i32(&x);
  55.     is_f32(&x);
  56. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top