From c1887d5cb2e2838d62c5d9a75ce86e4c419ee3e9 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Mon, 13 Nov 2023 11:23:35 -0700 Subject: [PATCH] address review feedback - add `type RawRep = Option` alias - rename `Resource::take` to `Resource::into_inner` - add `Resource::lift_borrow` and use it in code generator Signed-off-by: Joel Dice --- crates/guest-rust/src/lib.rs | 20 ++++++++++++++----- crates/rust/src/bindgen.rs | 5 ++++- .../src/bin/resource_into_inner.rs | 3 +++ .../test-rust-wasm/src/bin/resource_take.rs | 3 --- tests/runtime/main.rs | 2 +- ...esource_take.rs => resource_into_inner.rs} | 8 ++++---- .../wasm.rs | 10 +++++----- .../world.wit | 4 ++-- 8 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 crates/test-rust-wasm/src/bin/resource_into_inner.rs delete mode 100644 crates/test-rust-wasm/src/bin/resource_take.rs rename tests/runtime/{resource_take.rs => resource_into_inner.rs} (58%) rename tests/runtime/{resource_take => resource_into_inner}/wasm.rs (55%) rename tests/runtime/{resource_take => resource_into_inner}/world.wit (63%) diff --git a/crates/guest-rust/src/lib.rs b/crates/guest-rust/src/lib.rs index ad6e47d4d..8ceceb2c0 100644 --- a/crates/guest-rust/src/lib.rs +++ b/crates/guest-rust/src/lib.rs @@ -172,6 +172,8 @@ pub mod rt { } } +type RawRep = Option; + /// A type which represents a component model resource, either imported or /// exported into this component. /// @@ -250,19 +252,27 @@ impl Resource { where T: RustResource, { - let _ = Box::from_raw(rep as *mut Option); + let _ = Box::from_raw(rep as *mut RawRep); } /// Takes back ownership of the object, dropping the resource handle. - pub fn take(resource: Self) -> T + pub fn into_inner(resource: Self) -> T where T: RustResource, { unsafe { let rep = T::rep(resource.handle); - Option::take(&mut *(rep as *mut Option)).unwrap() + RawRep::take(&mut *(rep as *mut RawRep)).unwrap() } } + + #[doc(hidden)] + pub unsafe fn lift_borrow<'a>(rep: usize) -> &'a T + where + T: RustResource, + { + RawRep::as_ref(&*(rep as *const RawRep)).unwrap() + } } impl Deref for Resource { @@ -271,7 +281,7 @@ impl Deref for Resource { fn deref(&self) -> &T { unsafe { let rep = T::rep(self.handle); - Option::as_ref(&*(rep as *const Option)).unwrap() + RawRep::as_ref(&*(rep as *const RawRep)).unwrap() } } } @@ -280,7 +290,7 @@ impl DerefMut for Resource { fn deref_mut(&mut self) -> &mut T { unsafe { let rep = T::rep(self.handle); - Option::as_mut(&mut *(rep as *mut Option)).unwrap() + RawRep::as_mut(&mut *(rep as *mut RawRep)).unwrap() } } } diff --git a/crates/rust/src/bindgen.rs b/crates/rust/src/bindgen.rs index ddcb267e8..6a68e2a75 100644 --- a/crates/rust/src/bindgen.rs +++ b/crates/rust/src/bindgen.rs @@ -443,7 +443,10 @@ impl Bindgen for FunctionBindgen<'_, '_> { .as_deref() .unwrap() .to_upper_camel_case(); - format!("Option::as_ref(&*({op} as u32 as usize as *const Option<{name}>)).unwrap()") + let rt = self.gen.gen.runtime_path(); + format!( + "{rt}::Resource::<{name}>::lift_borrow({op} as u32 as usize)" + ) } Handle::Own(_) => { let name = self.gen.type_path(resource, true); diff --git a/crates/test-rust-wasm/src/bin/resource_into_inner.rs b/crates/test-rust-wasm/src/bin/resource_into_inner.rs new file mode 100644 index 000000000..f283f8300 --- /dev/null +++ b/crates/test-rust-wasm/src/bin/resource_into_inner.rs @@ -0,0 +1,3 @@ +include!("../../../../tests/runtime/resource_into_inner/wasm.rs"); + +fn main() {} diff --git a/crates/test-rust-wasm/src/bin/resource_take.rs b/crates/test-rust-wasm/src/bin/resource_take.rs deleted file mode 100644 index 87b080774..000000000 --- a/crates/test-rust-wasm/src/bin/resource_take.rs +++ /dev/null @@ -1,3 +0,0 @@ -include!("../../../../tests/runtime/resource_take/wasm.rs"); - -fn main() {} diff --git a/tests/runtime/main.rs b/tests/runtime/main.rs index 043fc1867..e2a78a53c 100644 --- a/tests/runtime/main.rs +++ b/tests/runtime/main.rs @@ -27,7 +27,7 @@ mod resource_borrow_import; mod resource_borrow_in_record; mod resource_floats; mod resource_import_and_export; -mod resource_take; +mod resource_into_inner; mod resource_with_lists; mod resources; mod smoke; diff --git a/tests/runtime/resource_take.rs b/tests/runtime/resource_into_inner.rs similarity index 58% rename from tests/runtime/resource_take.rs rename to tests/runtime/resource_into_inner.rs index 61392161b..290b082c1 100644 --- a/tests/runtime/resource_take.rs +++ b/tests/runtime/resource_into_inner.rs @@ -1,16 +1,16 @@ use wasmtime::Store; -wasmtime::component::bindgen!(in "tests/runtime/resource_take"); +wasmtime::component::bindgen!(in "tests/runtime/resource_into_inner"); -use exports::test::resource_take::test::Test; +use exports::test::resource_into_inner::test::Test; #[test] fn run() -> anyhow::Result<()> { crate::run_test( - "resource_take", + "resource_into_inner", |_| Ok(()), |store, component, linker| { - let (u, e) = ResourceTake::instantiate(store, component, linker)?; + let (u, e) = ResourceIntoInner::instantiate(store, component, linker)?; Ok((u.interface0, e)) }, run_test, diff --git a/tests/runtime/resource_take/wasm.rs b/tests/runtime/resource_into_inner/wasm.rs similarity index 55% rename from tests/runtime/resource_take/wasm.rs rename to tests/runtime/resource_into_inner/wasm.rs index 344bf4c68..3888ca18c 100644 --- a/tests/runtime/resource_take/wasm.rs +++ b/tests/runtime/resource_into_inner/wasm.rs @@ -1,13 +1,13 @@ wit_bindgen::generate!({ - path: "../../tests/runtime/resource_take", + path: "../../tests/runtime/resource_into_inner", exports: { world: Test, - "test:resource-take/test": Test, - "test:resource-take/test/thing": MyThing, + "test:resource-into-inner/test": Test, + "test:resource-into-inner/test/thing": MyThing, }, }); -use exports::test::resource_take::test::{Guest, GuestThing}; +use exports::test::resource_into_inner::test::{Guest, GuestThing}; use wit_bindgen::rt::Resource; pub struct Test; @@ -17,7 +17,7 @@ impl Guest for Test { let text = "Jabberwocky"; assert_eq!( text, - &Resource::take(Resource::new(MyThing(text.to_string()))).0 + &Resource::into_inner(Resource::new(MyThing(text.to_string()))).0 ); } } diff --git a/tests/runtime/resource_take/world.wit b/tests/runtime/resource_into_inner/world.wit similarity index 63% rename from tests/runtime/resource_take/world.wit rename to tests/runtime/resource_into_inner/world.wit index 86bb42006..313a98cec 100644 --- a/tests/runtime/resource_take/world.wit +++ b/tests/runtime/resource_into_inner/world.wit @@ -1,4 +1,4 @@ -package test:resource-take; +package test:resource-into-inner; interface test { resource thing { @@ -8,6 +8,6 @@ interface test { test: func(); } -world resource-take { +world resource-into-inner { export test; }