@@ -317,33 +317,13 @@ fn generate_unions(w: &mut Write, env: &Env, items: &[&Union]) -> Result<()> {
317
317
318
318
for item in items {
319
319
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
- }*/
340
320
let ( lines, commented) = generate_fields ( env, & item. name , & item. fields ) ;
341
321
342
322
let comment = if commented { "//" } else { "" } ;
343
323
if lines. is_empty ( ) {
344
324
try!( writeln ! (
345
325
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 " ,
347
327
comment,
348
328
c_type
349
329
) ) ;
@@ -355,13 +335,13 @@ fn generate_unions(w: &mut Write, env: &Env, items: &[&Union]) -> Result<()> {
355
335
c_type
356
336
) ) ;
357
337
358
- for line in lines {
338
+ for line in & lines {
359
339
try!( writeln ! ( w, "{}{}" , comment, line) ) ;
360
340
}
361
341
try!( writeln ! ( w, "{}}}\n " , comment) ) ;
362
342
}
363
343
if comment. is_empty ( ) {
364
- try!( generate_debug_with_fields ( w, name , & lines) ) ;
344
+ try!( generate_debug_with_fields ( w, c_type , & lines, false ) ) ;
365
345
}
366
346
}
367
347
}
@@ -380,7 +360,7 @@ fn generate_debug_impl(w: &mut Write, name: &str, impl_content: &str) -> Result<
380
360
impl_content)
381
361
}
382
362
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 < ( ) > {
384
364
let fields =
385
365
lines. iter ( )
386
366
. filter_map ( |field| {
@@ -400,12 +380,23 @@ fn generate_debug_with_fields(w: &mut Write, name: &str, lines: &[String]) -> Re
400
380
generate_debug_impl (
401
381
w,
402
382
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 \
404
385
{fields}\
405
386
\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
+ } ,
409
400
)
410
401
}
411
402
@@ -436,7 +427,8 @@ fn generate_classes_structs(w: &mut Write, env: &Env, classes: &[&Class]) -> Res
436
427
w,
437
428
"{comment}#[repr(C)]\n {debug}{comment}pub struct {name} {{" ,
438
429
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 " } ,
440
432
name = klass. c_type,
441
433
) ) ;
442
434
@@ -446,7 +438,7 @@ fn generate_classes_structs(w: &mut Write, env: &Env, classes: &[&Class]) -> Res
446
438
try!( writeln ! ( w, "{}}}\n " , comment) ) ;
447
439
448
440
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 ) ) ;
450
442
}
451
443
}
452
444
}
@@ -519,7 +511,7 @@ fn generate_records(w: &mut Write, env: &Env, records: &[&Record]) -> Result<()>
519
511
"{}#[repr(C)]\n {}{0}pub struct {} {{" ,
520
512
comment,
521
513
if can_generate_fields_debug( & record. fields) { "#[derive(Copy,Clone,Debug)]\n " }
522
- else { "" } ,
514
+ else { "#[derive(Copy,Clone)] \n " } ,
523
515
record. c_type
524
516
) ) ;
525
517
for line in & lines {
@@ -578,8 +570,7 @@ fn generate_fields(env: &Env, struct_name: &str, fields: &[Field]) -> (Vec<Strin
578
570
}
579
571
} ;
580
572
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
583
574
{
584
575
warn ! (
585
576
"Field `{}::{}` not expressible in Rust, truncated" ,
0 commit comments