Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. macro_rules! cfg_prefix {
  2. (
  3. #[cfg($($cond:tt)*)]($($pos:tt)*)
  4. #[cfg(else)]($($neg:tt)*)
  5. $($rest:tt)*
  6. ) => {
  7. #[cfg($($cond)*)]
  8. $($pos)* $($rest)*
  9.  
  10. #[cfg(not($($cond)*))]
  11. $($neg)* $($rest)*
  12. }
  13. }
  14.  
  15. pub struct MyStruct<T, #[cfg(test)] U>(Option<T>, #[cfg(test)] Option<U>);
  16.  
  17. cfg_prefix! {
  18. #[cfg(test)](impl<T, U> MyStruct<T, U>)
  19. #[cfg(else)](impl<T> MyStruct<T>)
  20. {
  21. pub fn new() -> Self {
  22. #[cfg(test)]
  23. return MyStruct(None, None);
  24.  
  25. #[cfg(not(test))]
  26. return MyStruct(None);
  27. }
  28. }
  29. }
  30.  
  31. #[test]
  32. fn test() {
  33. <MyStruct<i32, i32>>::new();
  34. panic!("In a test");
  35. }
  36.  
  37. #[cfg(not(test))]
  38. pub fn main() {
  39. <MyStruct<i32>>::new();
  40. panic!("Not in a test");
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement