Skip to content

Commit 57003b0

Browse files
committed
Inline writing include guard
1 parent f7a592b commit 57003b0

File tree

1 file changed

+40
-24
lines changed

1 file changed

+40
-24
lines changed

gen/src/write.rs

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,16 @@ fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
314314
}
315315

316316
fn write_struct(out: &mut OutFile, strct: &Struct) {
317-
write_include_guard_start(out, &strct.ident);
317+
writeln!(
318+
out,
319+
"#ifndef CXXBRIDGE03_STRUCT_{}{}",
320+
out.namespace, strct.ident,
321+
);
322+
writeln!(
323+
out,
324+
"#define CXXBRIDGE03_STRUCT_{}{}",
325+
out.namespace, strct.ident,
326+
);
318327
for line in strct.doc.to_string().lines() {
319328
writeln!(out, "//{}", line);
320329
}
@@ -325,7 +334,11 @@ fn write_struct(out: &mut OutFile, strct: &Struct) {
325334
writeln!(out, "{};", field.ident);
326335
}
327336
writeln!(out, "}};");
328-
write_include_guard_end(out, &strct.ident);
337+
writeln!(
338+
out,
339+
"#endif // CXXBRIDGE03_STRUCT_{}{}",
340+
out.namespace, strct.ident,
341+
);
329342
}
330343

331344
fn write_struct_decl(out: &mut OutFile, ident: &Ident) {
@@ -336,31 +349,17 @@ fn write_struct_using(out: &mut OutFile, ident: &Ident) {
336349
writeln!(out, "using {} = {};", ident, ident);
337350
}
338351

339-
const INCLUDE_GUARD_PREFIX: &'static str = "CXXBRIDGE03_TYPE_";
340-
341-
fn write_include_guard_start(out: &mut OutFile, ident: &Ident) {
342-
writeln!(
343-
out,
344-
"#ifndef {}{}{}",
345-
INCLUDE_GUARD_PREFIX, out.namespace, ident
346-
);
352+
fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
347353
writeln!(
348354
out,
349-
"#define {}{}{}",
350-
INCLUDE_GUARD_PREFIX, out.namespace, ident
355+
"#ifndef CXXBRIDGE03_STRUCT_{}{}",
356+
out.namespace, ety.ident,
351357
);
352-
}
353-
354-
fn write_include_guard_end(out: &mut OutFile, ident: &Ident) {
355358
writeln!(
356359
out,
357-
"#endif // {}{}{}",
358-
INCLUDE_GUARD_PREFIX, out.namespace, ident
360+
"#define CXXBRIDGE03_STRUCT_{}{}",
361+
out.namespace, ety.ident,
359362
);
360-
}
361-
362-
fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&ExternFn]) {
363-
write_include_guard_start(out, &ety.ident);
364363
for line in ety.doc.to_string().lines() {
365364
writeln!(out, "//{}", line);
366365
}
@@ -375,11 +374,24 @@ fn write_struct_with_methods(out: &mut OutFile, ety: &ExternType, methods: &[&Ex
375374
writeln!(out, ";");
376375
}
377376
writeln!(out, "}};");
378-
write_include_guard_end(out, &ety.ident);
377+
writeln!(
378+
out,
379+
"#endif // CXXBRIDGE03_STRUCT_{}{}",
380+
out.namespace, ety.ident,
381+
);
379382
}
380383

381384
fn write_enum(out: &mut OutFile, enm: &Enum) {
382-
write_include_guard_start(out, &enm.ident);
385+
writeln!(
386+
out,
387+
"#ifndef CXXBRIDGE03_ENUM_{}{}",
388+
out.namespace, enm.ident,
389+
);
390+
writeln!(
391+
out,
392+
"#define CXXBRIDGE03_ENUM_{}{}",
393+
out.namespace, enm.ident,
394+
);
383395
for line in enm.doc.to_string().lines() {
384396
writeln!(out, "//{}", line);
385397
}
@@ -390,7 +402,11 @@ fn write_enum(out: &mut OutFile, enm: &Enum) {
390402
writeln!(out, " {} = {},", variant.ident, variant.discriminant);
391403
}
392404
writeln!(out, "}};");
393-
write_include_guard_end(out, &enm.ident);
405+
writeln!(
406+
out,
407+
"#endif // CXXBRIDGE03_ENUM_{}{}",
408+
out.namespace, enm.ident,
409+
);
394410
}
395411

396412
fn check_enum(out: &mut OutFile, enm: &Enum) {

0 commit comments

Comments
 (0)