Skip to content

Commit

Permalink
remove some casts
Browse files Browse the repository at this point in the history
  • Loading branch information
cpetig committed Jul 26, 2024
1 parent 57b6b33 commit a3128b7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 21 deletions.
37 changes: 32 additions & 5 deletions crates/rust/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,14 @@ impl Bindgen for FunctionBindgen<'_, '_> {
..
} => {
let op = &operands[0];
let result = format!("({op}).take_handle() as i32");
let result = format!(
"({op}).take_handle(){cast}",
cast = if self.gen.gen.opts.symmetric {
""
} else {
" as i32"
}
);
results.push(result);
}

Expand All @@ -429,7 +436,14 @@ impl Bindgen for FunctionBindgen<'_, '_> {
..
} => {
let op = &operands[0];
results.push(format!("({op}).handle() as i32"))
results.push(format!(
"({op}).handle(){cast}",
cast = if self.gen.gen.opts.symmetric {
""
} else {
" as i32"
}
))
}

Instruction::HandleLift { handle, .. } => {
Expand All @@ -443,7 +457,15 @@ impl Bindgen for FunctionBindgen<'_, '_> {

let result = if is_own {
let name = self.gen.type_path(dealiased_resource, true);
format!("{name}::from_handle({op} as u32)")

format!(
"{name}::from_handle({op}{cast})",
cast = if self.gen.gen.opts.symmetric {
""
} else {
" as u32"
}
)
} else if self.gen.is_exported_resource(*resource) {
let name = resolve.types[*resource]
.name
Expand All @@ -457,9 +479,14 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let name = self.gen.type_path(dealiased_resource, true);
format!(
"{{\n
{tmp} = {name}::from_handle({op} as u32);
{tmp} = {name}::from_handle({op}{cast});
&{tmp}
}}"
}}",
cast = if self.gen.gen.opts.symmetric {
""
} else {
" as u32"
}
)
};
results.push(result);
Expand Down
52 changes: 36 additions & 16 deletions crates/rust/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,32 +227,37 @@ impl InterfaceGenerator<'_> {
self.src,
r#"
#[doc(hidden)]
unsafe fn _resource_new(val: *mut u8) -> u32
unsafe fn _resource_new(val: *mut u8) -> {handle_type}
where Self: Sized
{{
#[link(wasm_import_module = "[export]{module}")]
extern "C" {{
#[cfg_attr(target_arch = "wasm32", link_name = "[resource-new]{resource_name}")]
fn {external_new}(_: *mut u8) -> u32;
fn {external_new}(_: *mut u8) -> {handle_type};
}}
{external_new}(val)
}}
#[doc(hidden)]
fn _resource_rep(handle: u32) -> *mut u8
fn _resource_rep(handle: {handle_type}) -> *mut u8
where Self: Sized
{{
#[link(wasm_import_module = "[export]{module}")]
extern "C" {{
#[cfg_attr(target_arch = "wasm32", link_name = "[resource-rep]{resource_name}")]
fn {external_rep}(_: u32) -> *mut u8;
fn {external_rep}(_: {handle_type}) -> *mut u8;
}}
unsafe {{
{external_rep}(handle)
}}
}}
"#
"#,
handle_type = if self.gen.opts.symmetric {
"usize"
} else {
"u32"
}
);
for method in methods {
self.src.push_str(method);
Expand Down Expand Up @@ -2334,23 +2339,28 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
impl {camel} {{
#[doc(hidden)]
pub unsafe fn from_handle(handle: u32) -> Self {{
pub unsafe fn from_handle(handle: {handle_type}) -> Self {{
Self {{
handle: {resource}::from_handle(handle),
}}
}}
#[doc(hidden)]
pub fn take_handle(&self) -> u32 {{
pub fn take_handle(&self) -> {handle_type} {{
{resource}::take_handle(&self.handle)
}}
#[doc(hidden)]
pub fn handle(&self) -> u32 {{
pub fn handle(&self) -> {handle_type} {{
{resource}::handle(&self.handle)
}}
}}
"#
"#,
handle_type = if self.gen.opts.symmetric {
"usize"
} else {
"u32"
}
);
self.wasm_import_module.to_string()
} else {
Expand Down Expand Up @@ -2407,19 +2417,19 @@ impl {camel} {{
}}
#[doc(hidden)]
pub unsafe fn from_handle(handle: u32) -> Self {{
pub unsafe fn from_handle(handle: {handle_type}) -> Self {{
Self {{
handle: {resource}::from_handle(handle),
}}
}}
#[doc(hidden)]
pub fn take_handle(&self) -> u32 {{
pub fn take_handle(&self) -> {handle_type} {{
{resource}::take_handle(&self.handle)
}}
#[doc(hidden)]
pub fn handle(&self) -> u32 {{
pub fn handle(&self) -> {handle_type} {{
{resource}::handle(&self.handle)
}}
Expand Down Expand Up @@ -2483,7 +2493,12 @@ impl<'a> {camel}Borrow<'a>{{
self.rep.cast()
}}
}}
"#
"#,
handle_type = if self.gen.opts.symmetric {
"usize"
} else {
"u32"
}
);
format!("[export]{module}")
};
Expand All @@ -2499,19 +2514,24 @@ impl<'a> {camel}Borrow<'a>{{
r#"
unsafe impl {wasm_resource} for {camel} {{
#[inline]
unsafe fn drop(_handle: u32) {{
unsafe fn drop(_handle: {handle_type}) {{
{{
#[link(wasm_import_module = "{wasm_import_module}")]
extern "C" {{
#[cfg_attr(target_arch = "wasm32", link_name = "[resource-drop]{name}")]
fn {export_name}(_: u32);
fn {export_name}(_: {handle_type});
}}
{export_name}(_handle);
}}
}}
}}
"#
"#,
handle_type = if self.gen.opts.symmetric {
"usize"
} else {
"u32"
}
);
}

Expand Down

0 comments on commit a3128b7

Please sign in to comment.