Skip to content

Commit 04f6257

Browse files
authored
extend the lifetime of borrowed resource get (#1406)
* make as_ptr pub * fix * extend get lifetime * fix * add a test * fix
1 parent 56f7fce commit 04f6257

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

crates/rust/src/interface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2735,7 +2735,7 @@ impl<'a> {camel}Borrow<'a>{{
27352735
}}
27362736
27372737
/// Gets access to the underlying `T` in this resource.
2738-
pub fn get<T: Guest{camel}>(&self) -> &T {{
2738+
pub fn get<T: Guest{camel}>(&self) -> &'a T {{
27392739
let ptr = unsafe {{ &mut *self.as_ptr::<T>() }};
27402740
ptr.as_ref().unwrap()
27412741
}}

tests/runtime/resource-borrow/test.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ pub struct MyThing {
1010
val: u32,
1111
}
1212

13+
fn get_val<'a>(v: &ThingBorrow<'a>) -> &'a u32 {
14+
&v.get::<MyThing>().val
15+
}
16+
1317
impl Guest for Test {
1418
type Thing = MyThing;
1519

1620
fn foo(v: ThingBorrow<'_>) -> u32 {
17-
v.get::<MyThing>().val + 2
21+
get_val(&v) + 2
1822
}
1923
}
2024

0 commit comments

Comments
 (0)