Skip to content

Commit 66a6071

Browse files
authored
Add derive(Debug) to generated struct types. (#39)
* Add `derive(Debug)` to generated struct types. Fixes #34 and #35. * Don't derive Debug for types that contain unions. This also updates the WASI submodule and the autogenerated content.
1 parent faebf3e commit 66a6071

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

crates/generate-raw/src/lib.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,12 @@ fn render_union(src: &mut String, name: &str, u: &UnionDatatype) {
122122

123123
fn render_struct(src: &mut String, name: &str, s: &StructDatatype) {
124124
src.push_str("#[repr(C)]\n");
125-
src.push_str("#[derive(Copy, Clone)]\n");
125+
if struct_contains_union(s) {
126+
// Unions can't automatically derive `Debug`.
127+
src.push_str("#[derive(Copy, Clone)]\n");
128+
} else {
129+
src.push_str("#[derive(Copy, Clone, Debug)]\n");
130+
}
126131
src.push_str(&format!("pub struct {} {{\n", name.to_camel_case()));
127132
for member in s.members.iter() {
128133
rustdoc(&member.docs, src);
@@ -543,3 +548,18 @@ fn rustdoc_params(docs: &[InterfaceFuncParam], header: &str, dst: &mut String) {
543548
}
544549
}
545550
}
551+
552+
fn struct_contains_union(s: &StructDatatype) -> bool {
553+
s.members
554+
.iter()
555+
.any(|member| type_contains_union(&member.tref.type_()))
556+
}
557+
558+
fn type_contains_union(ty: &Type) -> bool {
559+
match ty {
560+
Type::Union(_) => true,
561+
Type::Array(tref) => type_contains_union(&tref.type_()),
562+
Type::Struct(st) => struct_contains_union(st),
563+
_ => false,
564+
}
565+
}

src/lib_generated.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -332,15 +332,15 @@ pub const RIGHTS_POLL_FD_READWRITE: Rights = 0x8000000;
332332
pub const RIGHTS_SOCK_SHUTDOWN: Rights = 0x10000000;
333333
pub type Fd = u32;
334334
#[repr(C)]
335-
#[derive(Copy, Clone)]
335+
#[derive(Copy, Clone, Debug)]
336336
pub struct Iovec {
337337
/// The address of the buffer to be filled.
338338
pub buf: *mut u8,
339339
/// The length of the buffer to be filled.
340340
pub buf_len: Size,
341341
}
342342
#[repr(C)]
343-
#[derive(Copy, Clone)]
343+
#[derive(Copy, Clone, Debug)]
344344
pub struct Ciovec {
345345
/// The address of the buffer to be written.
346346
pub buf: *const u8,
@@ -378,7 +378,7 @@ pub const FILETYPE_SOCKET_STREAM: Filetype = 6;
378378
/// The file refers to a symbolic link inode.
379379
pub const FILETYPE_SYMBOLIC_LINK: Filetype = 7;
380380
#[repr(C)]
381-
#[derive(Copy, Clone)]
381+
#[derive(Copy, Clone, Debug)]
382382
pub struct Dirent {
383383
/// The offset of the next directory entry stored in this directory.
384384
pub d_next: Dircookie,
@@ -416,7 +416,7 @@ pub const FDFLAGS_RSYNC: Fdflags = 0x8;
416416
/// may also synchronously update the file's metadata.
417417
pub const FDFLAGS_SYNC: Fdflags = 0x10;
418418
#[repr(C)]
419-
#[derive(Copy, Clone)]
419+
#[derive(Copy, Clone, Debug)]
420420
pub struct Fdstat {
421421
/// File type.
422422
pub fs_filetype: Filetype,
@@ -452,7 +452,7 @@ pub const OFLAGS_EXCL: Oflags = 0x4;
452452
pub const OFLAGS_TRUNC: Oflags = 0x8;
453453
pub type Linkcount = u64;
454454
#[repr(C)]
455-
#[derive(Copy, Clone)]
455+
#[derive(Copy, Clone, Debug)]
456456
pub struct Filestat {
457457
/// Device ID of device containing the file.
458458
pub dev: Device,
@@ -486,15 +486,15 @@ pub type Eventrwflags = u16;
486486
/// The peer of this socket has closed or disconnected.
487487
pub const EVENTRWFLAGS_FD_READWRITE_HANGUP: Eventrwflags = 0x1;
488488
#[repr(C)]
489-
#[derive(Copy, Clone)]
489+
#[derive(Copy, Clone, Debug)]
490490
pub struct EventFdReadwrite {
491491
/// The number of bytes available for reading or writing.
492492
pub nbytes: Filesize,
493493
/// The state of the file descriptor.
494494
pub flags: Eventrwflags,
495495
}
496496
#[repr(C)]
497-
#[derive(Copy, Clone)]
497+
#[derive(Copy, Clone, Debug)]
498498
pub struct Event {
499499
/// User-provided value that got attached to `subscription::userdata`.
500500
pub userdata: Userdata,
@@ -514,7 +514,7 @@ pub type Subclockflags = u16;
514514
/// current time value of clock `subscription_clock::id`.
515515
pub const SUBCLOCKFLAGS_SUBSCRIPTION_CLOCK_ABSTIME: Subclockflags = 0x1;
516516
#[repr(C)]
517-
#[derive(Copy, Clone)]
517+
#[derive(Copy, Clone, Debug)]
518518
pub struct SubscriptionClock {
519519
/// The clock against which to compare the timestamp.
520520
pub id: Clockid,
@@ -527,7 +527,7 @@ pub struct SubscriptionClock {
527527
pub flags: Subclockflags,
528528
}
529529
#[repr(C)]
530-
#[derive(Copy, Clone)]
530+
#[derive(Copy, Clone, Debug)]
531531
pub struct SubscriptionFdReadwrite {
532532
/// The file descriptor on which to wait for it to become ready for reading or writing.
533533
pub file_descriptor: Fd,
@@ -668,7 +668,7 @@ pub type Preopentype = u8;
668668
/// A pre-opened directory.
669669
pub const PREOPENTYPE_DIR: Preopentype = 0;
670670
#[repr(C)]
671-
#[derive(Copy, Clone)]
671+
#[derive(Copy, Clone, Debug)]
672672
pub struct PrestatDir {
673673
/// The length of the directory name for use with `fd_prestat_dir_name`.
674674
pub pr_name_len: Size,
@@ -724,21 +724,23 @@ pub unsafe fn environ_get(environ: *mut *mut u8, environ_buf: *mut u8) -> Result
724724
}
725725
}
726726

727-
/// Return command-line argument data sizes.
727+
/// Return environment variable data sizes.
728728
///
729729
/// ## Return
730730
///
731-
/// * `argc` - The number of arguments.
732-
/// * `argv_buf_size` - The size of the argument string data.
731+
/// * `environc` - The number of environment variable arguments.
732+
/// * `environ_buf_size` - The size of the environment variable data.
733733
pub unsafe fn environ_sizes_get() -> Result<(Size, Size)> {
734-
let mut argc = MaybeUninit::uninit();
735-
let mut argv_buf_size = MaybeUninit::uninit();
736-
let rc =
737-
wasi_snapshot_preview1::environ_sizes_get(argc.as_mut_ptr(), argv_buf_size.as_mut_ptr());
734+
let mut environc = MaybeUninit::uninit();
735+
let mut environ_buf_size = MaybeUninit::uninit();
736+
let rc = wasi_snapshot_preview1::environ_sizes_get(
737+
environc.as_mut_ptr(),
738+
environ_buf_size.as_mut_ptr(),
739+
);
738740
if let Some(err) = Error::from_raw_error(rc) {
739741
Err(err)
740742
} else {
741-
Ok((argc.assume_init(), argv_buf_size.assume_init()))
743+
Ok((environc.assume_init(), environ_buf_size.assume_init()))
742744
}
743745
}
744746

@@ -1591,8 +1593,8 @@ pub mod wasi_snapshot_preview1 {
15911593
/// Read environment variable data.
15921594
/// The sizes of the buffers should match that returned by `environ_sizes_get`.
15931595
pub fn environ_get(environ: *mut *mut u8, environ_buf: *mut u8) -> Errno;
1594-
/// Return command-line argument data sizes.
1595-
pub fn environ_sizes_get(argc: *mut Size, argv_buf_size: *mut Size) -> Errno;
1596+
/// Return environment variable data sizes.
1597+
pub fn environ_sizes_get(environc: *mut Size, environ_buf_size: *mut Size) -> Errno;
15961598
/// Return the resolution of a clock.
15971599
/// Implementations are required to provide a non-zero value for supported clocks. For unsupported clocks,
15981600
/// return `errno::inval`.

0 commit comments

Comments
 (0)