File tree Expand file tree Collapse file tree 2 files changed +67
-0
lines changed
Expand file tree Collapse file tree 2 files changed +67
-0
lines changed Original file line number Diff line number Diff line change @@ -833,6 +833,26 @@ impl InterfaceGenerator<'_> {
833833 }}
834834 }}
835835
836+ impl core::ops::DerefMut for Own{camel} {{
837+ fn deref_mut(&mut self) -> &mut Rep{camel} {{
838+ unsafe {{
839+ #[cfg(target_arch = "wasm32")]
840+ #[link(wasm_import_module = "[export]{module}")]
841+ extern "C" {{
842+ #[link_name = "[resource-rep]{name}"]
843+ fn wit_import(_: i32) -> i32;
844+ }}
845+
846+ #[cfg(not(target_arch = "wasm32"))]
847+ unsafe fn wit_import(_n: i32) -> i32 {{ unreachable!() }}
848+
849+ ::core::mem::transmute::<isize, &mut Rep{camel}>(
850+ wit_import(self.handle).try_into().unwrap()
851+ )
852+ }}
853+ }}
854+ }}
855+
836856 impl Drop for Own{camel} {{
837857 fn drop(&mut self) {{
838858 unsafe {{
Original file line number Diff line number Diff line change @@ -225,3 +225,50 @@ mod alternative_bitflags_path {
225225 }
226226 }
227227}
228+
229+ mod owned_resource_deref_mut {
230+ wit_bindgen:: generate!( {
231+ inline: "
232+ package my:inline
233+
234+ interface foo {
235+ resource bar {
236+ constructor(data: u32)
237+ get-data: func() -> u32
238+ consume: static func(%self: bar) -> u32
239+ }
240+ }
241+
242+ world baz {
243+ export foo
244+ }
245+ " ,
246+ exports: {
247+ "my:inline/foo/bar" : Resource
248+ }
249+ } ) ;
250+
251+ pub struct Resource {
252+ data : u32 ,
253+ }
254+
255+ impl exports:: my:: inline:: foo:: Bar for Resource {
256+ fn new ( data : u32 ) -> Self {
257+ Self { data }
258+ }
259+
260+ fn get_data ( & self ) -> u32 {
261+ self . data
262+ }
263+
264+ fn consume ( mut this : exports:: my:: inline:: foo:: OwnBar ) -> u32 {
265+ // Check that Deref<Target = Self> is implemented
266+ let prior_data: & u32 = & this. data ;
267+ let new_data = prior_data + 1 ;
268+ // Check that DerefMut<Target = Self> is implemented
269+ let mutable_data: & mut u32 = & mut this. data ;
270+ * mutable_data = new_data;
271+ this. data
272+ }
273+ }
274+ }
You can’t perform that action at this time.
0 commit comments