drylib/
drylib_decls.rs

1#[macro_export]
2macro_rules! lock { // locks given variable whether it's mutex or not
3    ($var: expr) => { $var.lock().expect("Failed to lock") };
4    ($var: expr, $expect: expr) => { $var.lock().expect($expect) };
5    ($var: ident:?) => { $var.lock().expect(&format!("Failed to lock {var}", var = stringify!($var))) };
6}
7
8#[macro_export]
9macro_rules! parse { // parses one type into another
10    ($var: expr, $type: ty) => { $var.parse::<$type>().expect("Failed to parse") };
11    ($var: expr, $type: ty, $expect: expr) => { $var.parse::<$type>().expect($expect) };
12    ($var: expr, $type: ty:?) => { $var.parse::<$type>().expect(&format!("Failed to parse {t1} into {t2}", t1 = $var, t2 = stringify!($type))) };
13}
14
15// am -> Arc Mutex
16#[macro_export]
17macro_rules! am { // creates Arc<Mutex<`your variable`>>
18    ($var: expr) => { std::sync::Arc::new(std::sync::Mutex::new($var)) };
19}
20
21#[macro_export]
22macro_rules! sleep { // puts thread on sleep
23    (i|$t: expr) => { std::thread::sleep(std::time::Duration::from_secs($t as u64)) };
24    ($t: expr) => { std::thread::sleep($t) };
25}
26
27#[macro_export]
28macro_rules! read { // reads input from stdin. 
29    // Use std::io::{Write, BufRead, BufReader} for it to work.
30    ($msg: expr, $rbuf: expr => $buf: expr) => { // rbuf basically is std::io::BufReader
31        print!("{m}", m = $msg); std::io::stdout().flush().unwrap();
32        $rbuf.read_line($buf).ok();
33    };
34    ($rbuf: expr => $buf: expr) => {
35        $rbuf.read_line($buf).ok();
36    };
37    ($buf: expr) => { stdin().read_line($buf).ok() };
38}
39
40#[macro_export]
41macro_rules! pubstruct { // creates pub struct with optional generic types, optional lifetimes, with all of the fields are public as well.
42    ($(#[$meta:meta])*
43    $name: ident($(#[$fmeta:meta])* $($t: ty), *)) => {
44        $(#[$meta])*
45        pub struct $name($(#[$fmeta])* $(pub $t), *);
46    };
47
48    ($(#[$meta:meta])*
49    $name: ident<$($T: ident), +>($(#[$fmeta:meta])* $($t: ty), *)) => {
50        $(#[$meta])*
51        pub struct $name<$($T,) *>($(#[$fmeta])* $(pub $t), *);
52    };
53
54    ($(#[$meta:meta])*
55    $name: ident<$($L: lifetime), +>($(#[$fmeta:meta])* $($t: ty), *)) => {
56        $(#[$meta])*
57        pub struct $name<$($L,) *>($(#[$fmeta])* $(pub $t), *);
58    };
59
60    ($(#[$meta:meta])*
61    $name: ident<$($L: lifetime), +, $($T: ident), +>($(#[$fmeta:meta])* $($t: ty), *)) => {
62        $(#[$meta])*
63        pub struct $name<$($L,) * $($T,) *>($(#[$fmeta])* $(pub $t), *);
64    };
65
66    ($(#[$meta:meta])*
67    $name: ident {
68        $($(#[$fmeta:meta])*
69        $field: ident: $t: ty),* $(,)?
70    }) => {
71        $(#[$meta])*
72        pub struct $name {
73            $($(#[$fmeta])*
74            pub $field: $t),*
75        }
76    };
77
78    ($(#[$meta:meta])*
79    $name: ident<$($T: ident),*> {
80        $($(#[$fmeta:meta])*
81        $field:ident: $t:ty),* $(,)?
82    }) => {
83        $(#[$meta])*
84        pub struct $name<$($T),*> {
85            $($(#[$fmeta])* pub $field: $t),*
86        }
87    };
88
89    ($(#[$meta:meta])*
90    $name: ident<$($L: lifetime),*> {
91        $($(#[$fmeta:meta])*
92        $field: ident: $t: ty),* $(,)?
93    }) => {
94        $(#[$meta])*
95        pub struct $name<$($L),*> {
96            $($(#[$fmeta])* pub $field: $t),*
97        }
98    };
99
100    ($(#[$meta: meta])*
101    $name: ident<$($L: lifetime),*, $($T: ident),*> {
102        $($(#[$fmeta: meta])*
103        $field: ident: $t: ty),* $(,)?
104    }) => {
105        $(#[$meta])*
106        pub struct $name<$($L),*, $($T),*> {
107            $($(#[$fmeta])* pub $field: $t),*
108        }
109    };
110}
111
112#[macro_export]
113macro_rules! colored {
114    (pr | $($args: tt), *) => { // pr -> print red 
115        println!("\x1b[31m{}\x1b[0m", format_args!($($args)*))
116    };
117    (fr | $($args: tt), *) => { // fr -> format red
118        format!("\x1b[31m{}\x1b[0m", format_args!($($args)*))
119    };
120    (pg | $($args: tt), *) => { // pg -> print gray 
121        println!("\x1b[90m{}\x1b[0m", format_args!($($args)*))
122    };
123    (fg | $($args: tt), *) => { // fg -> format gray
124        format!("\x1b[90m{}\x1b[0m", format_args!($($args)*))
125    };
126}