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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
// Take a look at the license at the top of the repository in the LICENSE file.
use std::rc::Rc;
use glib::clone;
#[test]
fn clone() {
let v = Rc::new(1);
let _ = clone!(@strong v => @default-return None::<i32>, move || {println!("foo"); 1});
let v = Rc::new(1);
let _ = clone!(@weak v => @default-return None::<i32>, move || {println!("foo"); Some(1)});
let v = "123";
let _ = clone!(@to-owned v => @default-return None::<i32>, move || {println!("foo"); 1});
}
const TESTS: &[(&str, &str)] = &[
(
"clone!( => move || {})",
"If you have nothing to clone, no need to use this macro!",
),
(
"clone!(|| {})",
"If you have nothing to clone, no need to use this macro!",
),
(
"clone!(|a, b| {})",
"If you have nothing to clone, no need to use this macro!",
),
(
"clone!(@weak a, @weak b => |x| {})",
r#"error: Closure needs to be "moved" so please add `move` before closure
--> test_3.rs:1:86
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@weak a, @weak b => |x| {}); }
| ^"#,
),
(
"clone!(@weak a, @weak b => || {})",
r#"error: Closure needs to be "moved" so please add `move` before closure
--> test_4.rs:1:86
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@weak a, @weak b => || {}); }
| ^"#,
),
(
"clone!(@weak a, @weak b => |x| println!(\"a\"))",
r#"error: Closure needs to be "moved" so please add `move` before closure
--> test_5.rs:1:86
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@weak a, @weak b => |x| println!("a")); }
| ^"#,
),
(
"clone!(@weak a, @weak b => || println!(\"a\"))",
r#"error: Closure needs to be "moved" so please add `move` before closure
--> test_6.rs:1:86
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@weak a, @weak b => || println!("a")); }
| ^"#,
),
(
"clone!(@weak a => |x| {})",
r#"error: Closure needs to be "moved" so please add `move` before closure
--> test_7.rs:1:77
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@weak a => |x| {}); }
| ^"#,
),
(
"clone!(@weak a => || {})",
r#"error: Closure needs to be "moved" so please add `move` before closure
--> test_8.rs:1:77
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@weak a => || {}); }
| ^"#,
),
(
"clone!(@weak a => |x| println!(\"a\"))",
r#"error: Closure needs to be "moved" so please add `move` before closure
--> test_9.rs:1:77
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@weak a => |x| println!("a")); }
| ^"#,
),
(
"clone!(@weak a => || println!(\"a\"))",
r#"error: Closure needs to be "moved" so please add `move` before closure
--> test_10.rs:1:77
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@weak a => || println!("a")); }
| ^"#,
),
(
"clone!(@strong self => move |x| {})",
r#"error: Can't use `self` as variable name. Try storing it in a temporary variable or rename it using `as`.
--> test_11.rs:1:74
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@strong self => move |x| {}); }
| ^^^^
"#,
),
(
"clone!(@strong self.v => move |x| {})",
r#"error: `self.v`: Field accesses are not allowed as is, you must rename it!
--> test_12.rs:1:79
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@strong self.v => move |x| {}); }
| ^
"#,
),
(
"clone!(@weak v => @default-return false, || {})",
r#"error: Closure needs to be "moved" so please add `move` before closure
--> test_13.rs:1:100
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@weak v => @default-return false, || {}); }
| ^"#,
),
(
"clone!(@weak v => @default-return false, || println!(\"a\"))",
r#"error: Closure needs to be "moved" so please add `move` before closure
--> test_14.rs:1:100
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@weak v => @default-return false, || println!("a")); }
| ^"#,
),
(
"clone!(@weak v => @default-return false, |bla| {})",
r#"error: Closure needs to be "moved" so please add `move` before closure
--> test_15.rs:1:100
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@weak v => @default-return false, |bla| {}); }
| ^
"#,
),
(
"clone!(@weak v => @default-return false, |bla| println!(\"a\"))",
r#"error: Closure needs to be "moved" so please add `move` before closure
--> test_16.rs:1:100
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@weak v => @default-return false, |bla| println!("a")); }
| ^
"#,
),
(
"clone!(@weak v => default-return false, move || {})",
r#"error: Missing `@` before `default-return`
--> test_17.rs:1:77
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@weak v => default-return false, move || {}); }
| ^^^^^^^
"#,
),
(
"clone!(@weak v => @default-return false move || {})",
r#"error: Expected `,` after `@default-return false`, found `move`
--> test_18.rs:1:99
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@weak v => @default-return false move || {}); }
| ^^^^
"#,
),
(
"clone!(@yolo v => move || {})",
r#"error: Unknown keyword `yolo`, only `weak`, `weak-allow-none`, `to-owned` and `strong` are allowed
--> test_19.rs:1:67
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@yolo v => move || {}); }
| ^^^^
"#,
),
(
"clone!(v => move || {})",
r#"error: Unexpected ident `v`: you need to specify if this is a weak or a strong clone.
--> test_20.rs:1:66
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(v => move || {}); }
| ^
"#,
),
(
"clone!(@strong v => {println!(\"foo\");})",
r#"error: Missing `move` and closure declaration
--> test_21.rs:1:79
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@strong v => {println!("foo");}); }
| ^^^^^^^^^^^^^^^^^^
"#,
),
(
"clone!(@strong v, @default-return lol => move || {println!(\"foo\");})",
r#"error: `@default-return` should be after `=>`
--> test_22.rs:1:78
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@strong v, @default-return lol => move || {println!("foo");}); }
| ^^^^^^^
"#,
),
(
"clone!(@default-return lol, @strong v => move || {println!(\"foo\");})",
r#"error: `@default-return` should be after `=>`
--> test_23.rs:1:67
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@default-return lol, @strong v => move || {println!("foo");}); }
| ^^^^^^^
"#,
),
// The async part!
(
"clone!(@strong v => async || {println!(\"foo\");})",
r#"error: Expected `move` after `async`, found `|`
--> test_24.rs:1:85
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@strong v => async || {println!("foo");}); }
| ^"#,
),
(
"clone!(@strong v => async {println!(\"foo\");})",
r#"error: Expected `move` after `async`, found `{`
--> test_25.rs:1:85
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@strong v => async {println!("foo");}); }
| ^^^^^^^^^^^^^^^^^^
"#,
),
(
"clone!(@strong v => move || async {println!(\"foo\");})",
r#"error: Expected `move` after `async`, found `{`
--> test_26.rs:1:93
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@strong v => move || async {println!("foo");}); }
| ^^^^^^^^^^^^^^^^^^
"#,
),
(
"clone!(@strong v => move || async println!(\"foo\");)",
r#"error: Expected `move` after `async`, found `println`
--> test_27.rs:1:93
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@strong v => move || async println!("foo");); }
| ^^^^^^^
"#,
),
(
"clone!(@strong v => move || async move println!(\"foo\");)",
r#"error: Expected block after `| async move`
--> test_28.rs:1:98
|
1 | fn main() { use glib::clone; let v = std::rc::Rc::new(1); clone!(@strong v => move || async move println!("foo");); }
| ^^^^^^^
"#,
),
];
#[test]
fn clone_failures() {
let t = trybuild2::TestCases::new();
for (index, (expr, err)) in TESTS.iter().enumerate() {
let prefix = "fn main() { use glib::clone; let v = std::rc::Rc::new(1); ";
let suffix = "; }";
let output = format!("{prefix}{expr}{suffix}");
t.compile_fail_inline_check_sub(&format!("test_{index}"), &output, err);
}
}
const NO_WARNING: &[&str] = &[
"clone!(@weak v => @default-return (), move || println!(\"{}\", v);)",
"clone!(@weak v => @default-return (()), move || println!(\"{}\", v);)",
"clone!(@weak v => @default-return ( () ), move || println!(\"{}\", v);)",
"clone!(@weak v => @default-return ( ), move || println!(\"{}\", v);)",
];
// Ensures that no warning are emitted if the default-return is a unit tuple.
#[test]
fn clone_unit_tuple_return() {
let t = trybuild2::TestCases::new();
for (index, expr) in NO_WARNING.iter().enumerate() {
let prefix = "fn main() { use glib::clone; let v = std::rc::Rc::new(1); ";
let suffix = "; }";
let output = format!("{prefix}{expr}{suffix}");
t.pass_inline(&format!("test_{index}"), &output);
}
}
|