Skip to content

Commit 612089f

Browse files
author
Luke Jones
committed
unions: rebase & fix cop,clone for unions
1 parent aeefc4d commit 612089f

File tree

1 file changed

+24
-33
lines changed

1 file changed

+24
-33
lines changed

src/codegen/sys/lib_.rs

+24-33
Original file line numberDiff line numberDiff line change
@@ -317,33 +317,13 @@ fn generate_unions(w: &mut Write, env: &Env, items: &[&Union]) -> Result<()> {
317317

318318
for item in items {
319319
if let Some(ref c_type) = item.c_type {
320-
/* #[cfg(not(feature = "use_unions"))]
321-
{
322-
// TODO: GLib/GObject special cases until we have proper union support in Rust
323-
if env.config.library_name == "GLib" && c_type == "GMutex" {
324-
// Two c_uint or a pointer => 64 bits on all platforms currently
325-
// supported by GLib but the alignment is different on 32 bit
326-
// platforms (32 bit vs. 64 bits on 64 bit platforms)
327-
try!(writeln!(
328-
w,
329-
"#[cfg(target_pointer_width = \"32\")]\n\
330-
#[repr(C)]\n\
331-
#[derive(Debug)]\n\
332-
pub struct {0}([u32; 2]);\n\
333-
#[cfg(target_pointer_width = \"64\")]\n\
334-
#[repr(C)]\n\
335-
#[derive(Debug)]\n\
336-
pub struct {0}(*mut c_void);",
337-
c_type
338-
));
339-
}*/
340320
let (lines, commented) = generate_fields(env, &item.name, &item.fields);
341321

342322
let comment = if commented { "//" } else { "" };
343323
if lines.is_empty() {
344324
try!(writeln!(
345325
w,
346-
"{0}#[repr(C)]\n{0}pub union {1}(c_void);\n",
326+
"{0}#[repr(C)]\n{0}#[derive(Copy,Clone,Debug)]\n{0}pub union {1}(u8);\n",
347327
comment,
348328
c_type
349329
));
@@ -355,13 +335,13 @@ fn generate_unions(w: &mut Write, env: &Env, items: &[&Union]) -> Result<()> {
355335
c_type
356336
));
357337

358-
for line in lines {
338+
for line in &lines {
359339
try!(writeln!(w, "{}{}", comment, line));
360340
}
361341
try!(writeln!(w, "{}}}\n", comment));
362342
}
363343
if comment.is_empty() {
364-
try!(generate_debug_with_fields(w, name, &lines));
344+
try!(generate_debug_with_fields(w, c_type, &lines, false));
365345
}
366346
}
367347
}
@@ -380,7 +360,7 @@ fn generate_debug_impl(w: &mut Write, name: &str, impl_content: &str) -> Result<
380360
impl_content)
381361
}
382362

383-
fn generate_debug_with_fields(w: &mut Write, name: &str, lines: &[String]) -> Result<()> {
363+
fn generate_debug_with_fields(w: &mut Write, name: &str, lines: &[String], safe: bool) -> Result<()> {
384364
let fields =
385365
lines.iter()
386366
.filter_map(|field| {
@@ -400,12 +380,23 @@ fn generate_debug_with_fields(w: &mut Write, name: &str, lines: &[String]) -> Re
400380
generate_debug_impl(
401381
w,
402382
name,
403-
&format!("f.debug_struct(&format!(\"{name} @ {{:?}}\", self as *const _))\n\
383+
&if safe {
384+
format!("f.debug_struct(&format!(\"{name} @ {{:?}}\", self as *const _))\n\
404385
{fields}\
405386
\t\t .finish()",
406-
name=name,
407-
fields=fields,
408-
)
387+
name=name,
388+
fields=fields,
389+
)
390+
} else {
391+
format!("unsafe {{\n\
392+
\t\t f.debug_struct(&format!(\"{name} @ {{:?}}\", self as *const _))\n\
393+
{fields}\
394+
\t\t .finish()\n\
395+
\t\t }}",
396+
name=name,
397+
fields=fields,
398+
)
399+
},
409400
)
410401
}
411402

@@ -436,7 +427,8 @@ fn generate_classes_structs(w: &mut Write, env: &Env, classes: &[&Class]) -> Res
436427
w,
437428
"{comment}#[repr(C)]\n{debug}{comment}pub struct {name} {{",
438429
comment = comment,
439-
debug = if can_generate_fields_debug { "#[derive(Debug)]\n" } else { "" },
430+
debug = if can_generate_fields_debug { "#[derive(Copy,Clone,Debug)]\n" }
431+
else { "#[derive(Copy,Clone)]\n" },
440432
name = klass.c_type,
441433
));
442434

@@ -446,7 +438,7 @@ fn generate_classes_structs(w: &mut Write, env: &Env, classes: &[&Class]) -> Res
446438
try!(writeln!(w, "{}}}\n", comment));
447439

448440
if !can_generate_fields_debug && comment.is_empty() {
449-
try!(generate_debug_with_fields(w, &klass.c_type, &lines));
441+
try!(generate_debug_with_fields(w, &klass.c_type, &lines, true));
450442
}
451443
}
452444
}
@@ -519,7 +511,7 @@ fn generate_records(w: &mut Write, env: &Env, records: &[&Record]) -> Result<()>
519511
"{}#[repr(C)]\n{}{0}pub struct {} {{",
520512
comment,
521513
if can_generate_fields_debug(&record.fields) { "#[derive(Copy,Clone,Debug)]\n" }
522-
else { "" },
514+
else { "#[derive(Copy,Clone)]\n" },
523515
record.c_type
524516
));
525517
for line in &lines {
@@ -578,8 +570,7 @@ fn generate_fields(env: &Env, struct_name: &str, fields: &[Field]) -> (Vec<Strin
578570
}
579571
};
580572

581-
if !is_gweakref && !truncated && !is_ptr && is_bits &&
582-
!is_union_special_case(&field.c_type)
573+
if !is_gweakref && !is_ghooklist && !truncated && !is_ptr && is_bits
583574
{
584575
warn!(
585576
"Field `{}::{}` not expressible in Rust, truncated",

0 commit comments

Comments
 (0)