1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//! `goto` and `label` for Rust!
//! Now you never have to use `while`, `for`, or `loop` again!
//!
//! # Example
//! ```
//! use goto::{goto, label};
//! use self::might_skip;
//!
//! #[no_mangle] // Needed to prevent foo() from being optimized away
//! unsafe fn foo() {
//! println!("This text will never be printed!");
//!
//! label!("label1");
//! print!("Hello");
//! goto!("label2");
//!
//! println!("Neither will this be printed!");
//! }
//!
//! unsafe fn hello_world() {
//! goto!("label1");
//! println!("This won't be printed either!");
//!
//! label!("label2");
//! println!(" World!");
//! }
//!
//! unsafe {
//! hello_world();
//! }
//! ```
/// Create a label
///
/// This will create a linker symbol. Be careful that the label you use does not clash with other
/// symbols.
///
/// # Example
/// ```
/// use goto_label::label;
///
/// // Create a label named "foo"
/// unsafe {
/// label!("foo");
/// }
/// ```
// Inform the compiler that this expression might be skipped by `goto!`
//
// This attempts to prevent segfaults in optimized builds by preventing optimization with
// surrounding code. It doesn't work well enough to keep as a documented public macro.
//extern crate proc_macro;
/// Jump to label
///
/// # Example
/// ```
/// use goto_label::{goto, label};
///
/// unsafe {
/// // Jump to label named "foo"
/// goto!("foo");
/// println!("This line will never be printed!");
///
/// // Label is defined here
/// label!("foo");
/// }
/// ```
/*#[cfg(test)]
mod tests {
#[test]
fn basic() {
unsafe {
let mut x = 0;
assert_eq!(x, 0);
goto!("end0");
x = 42;
label!("end0");
assert_eq!(x, 0);
}
}
} */
// https://github.com/Property404/goto-label-rs/blob/main/src/lib.rs
/*
#[proc_macro_attribute]
pub fn add_log(_attr: TokenStream, item: TokenStream) -> TokenStream {
let mut input = parse_macro_input!(item as ItemFn);
let stmts = &mut input.block.stmts;
// Insert a new statement at the beginning of the function body
stmts.insert(0, syn::parse_quote! {
println!("Function called!");
});
// Generate the modified function
TokenStream::from(quote! { #input })
}
*/