Skip to content

Commit faebf3e

Browse files
author
Pat Hickey
authored
update to use tagged unions (#41)
* update to use tagged unions * latest wasi repo lays out event as flat struct * rebased wasi tagged unions branch * fix for namedtype as union tag * regenerate lib_generated.rs * restore `size` -> `usize` hack in generate-raw along with a comment describing when its safe to remove * WASI submodule: master with tagged_unions PR merged.
1 parent 72a262a commit faebf3e

File tree

3 files changed

+40
-31
lines changed

3 files changed

+40
-31
lines changed

crates/generate-raw/src/lib.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,27 @@ fn render_const(src: &mut String, name: &str, c: &IntDatatype) {
9797
fn render_union(src: &mut String, name: &str, u: &UnionDatatype) {
9898
src.push_str("#[repr(C)]\n");
9999
src.push_str("#[derive(Copy, Clone)]\n");
100-
src.push_str(&format!("pub union {} {{\n", name.to_camel_case()));
100+
src.push_str(&format!("pub union {}U {{\n", name.to_camel_case()));
101101
for variant in u.variants.iter() {
102-
rustdoc(&variant.docs, src);
103-
src.push_str("pub ");
104-
variant.name.render(src);
105-
src.push_str(": ");
106-
variant.tref.render(src);
107-
src.push_str(",\n");
102+
if let Some(ref tref) = variant.tref {
103+
rustdoc(&variant.docs, src);
104+
src.push_str("pub ");
105+
variant.name.render(src);
106+
src.push_str(": ");
107+
tref.render(src);
108+
src.push_str(",\n");
109+
}
108110
}
109-
src.push_str("}");
111+
src.push_str("}\n");
112+
src.push_str("#[repr(C)]\n");
113+
src.push_str("#[derive(Copy, Clone)]\n");
114+
src.push_str(&format!("pub struct {} {{\n", name.to_camel_case()));
115+
src.push_str(&format!(
116+
"pub tag: {},\n",
117+
u.tag.name.as_str().to_camel_case()
118+
));
119+
src.push_str(&format!("pub u: {}U,\n", name.to_camel_case()));
120+
src.push_str("}\n");
110121
}
111122

112123
fn render_struct(src: &mut String, name: &str, s: &StructDatatype) {
@@ -190,8 +201,9 @@ fn render_alias(src: &mut String, name: &str, dest: &TypeRef) {
190201
}
191202
src.push_str(" = ");
192203

193-
// Give `size_t` special treatment to translate it to `usize` in Rust
194-
// instead of `u32`, makes things a bit nicer in Rust.
204+
// Give `size` special treatment to translate it to `usize` in Rust instead of `u32`. Makes
205+
// things a bit nicer for client libraries. We can remove this hack once WASI moves to a
206+
// snapshot that uses BuiltinType::Size.
195207
if name == "size" {
196208
src.push_str("usize");
197209
} else {

src/lib_generated.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -495,21 +495,16 @@ pub struct EventFdReadwrite {
495495
}
496496
#[repr(C)]
497497
#[derive(Copy, Clone)]
498-
pub union EventU {
499-
/// When type is `eventtype::fd_read` or `eventtype::fd_write`:
500-
pub fd_readwrite: EventFdReadwrite,
501-
}
502-
#[repr(C)]
503-
#[derive(Copy, Clone)]
504498
pub struct Event {
505499
/// User-provided value that got attached to `subscription::userdata`.
506500
pub userdata: Userdata,
507501
/// If non-zero, an error that occurred while processing the subscription request.
508502
pub error: Errno,
509-
/// The type of the event that occurred.
503+
/// The type of event that occured
510504
pub r#type: Eventtype,
511-
/// The contents of the event.
512-
pub u: EventU,
505+
/// The contents of the event, if it is an `eventtype::fd_read` or
506+
/// `eventtype::fd_write`. `eventtype::clock` events ignore this field.
507+
pub fd_readwrite: EventFdReadwrite,
513508
}
514509
pub type Subclockflags = u16;
515510
/// If set, treat the timestamp provided in
@@ -539,21 +534,25 @@ pub struct SubscriptionFdReadwrite {
539534
}
540535
#[repr(C)]
541536
#[derive(Copy, Clone)]
542-
pub union SubscriptionU {
543-
/// When type is `eventtype::clock`:
537+
pub union SubscriptionUU {
544538
pub clock: SubscriptionClock,
545-
/// When type is `eventtype::fd_read` or `eventtype::fd_write`:
546-
pub fd_readwrite: SubscriptionFdReadwrite,
539+
pub fd_read: SubscriptionFdReadwrite,
540+
pub fd_write: SubscriptionFdReadwrite,
547541
}
542+
#[repr(C)]
543+
#[derive(Copy, Clone)]
544+
pub struct SubscriptionU {
545+
pub tag: Eventtype,
546+
pub u: SubscriptionUU,
547+
}
548+
548549
#[repr(C)]
549550
#[derive(Copy, Clone)]
550551
pub struct Subscription {
551552
/// User-provided value that is attached to the subscription in the
552553
/// implementation and returned through `event::userdata`.
553554
pub userdata: Userdata,
554-
/// The type of the event to which to subscribe.
555-
pub r#type: Eventtype,
556-
/// The contents of the subscription.
555+
/// The type of the event to which to subscribe, and its contents
557556
pub u: SubscriptionU,
558557
}
559558
pub type Exitcode = u32;
@@ -677,17 +676,15 @@ pub struct PrestatDir {
677676
#[repr(C)]
678677
#[derive(Copy, Clone)]
679678
pub union PrestatU {
680-
/// When type is `preopentype::dir`:
681679
pub dir: PrestatDir,
682680
}
683681
#[repr(C)]
684682
#[derive(Copy, Clone)]
685683
pub struct Prestat {
686-
/// The type of the pre-opened capability.
687-
pub pr_type: Preopentype,
688-
/// The contents of the information.
684+
pub tag: Preopentype,
689685
pub u: PrestatU,
690686
}
687+
691688
/// Read command-line argument data.
692689
/// The size of the array should match that returned by `args_sizes_get`
693690
pub unsafe fn args_get(argv: *mut *mut u8, argv_buf: *mut u8) -> Result<()> {

0 commit comments

Comments
 (0)