Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using declare_types with explicit lifetime #207

Closed
jugglingcats opened this issue May 19, 2017 · 3 comments
Closed

Using declare_types with explicit lifetime #207

jugglingcats opened this issue May 19, 2017 · 3 comments

Comments

@jugglingcats
Copy link

I believe this is a direct consequence of rust-lang/rust#34303 which hopefully will be fixed, but am adding here for awareness.

It doesn't seem possible to have a lifetime on the underlying struct for a Javascript class, eg:

pub struct TestMe<'a> {
    x: &'a mut i32
}

declare_types! {
    pub class JsTestMe for TestMe {
        init(call) {
            Ok(TestMe {x: &5})
        }
    }
}

This fails with compile error:

error[E0107]: wrong number of lifetime parameters: expected 1, found 0
  --> src\shm/mod.rs:99:35
   |
99 |     pub class JsTestMe for TestMe {
   |                                   ^ expected 1 lifetime parameter
@eira-fransham
Copy link

Workaround: use pub class JsTestMe as TestMe for TestMe<'static>. There is no way to do this with a generic parameter right now, but you can make it concrete.

@ryantate13
Copy link

Is it possible to get this to work with lifetimes other than 'static? I get a use of undeclared lifetime name error when I try.

@kjvalencik
Copy link
Member

@ryantate13 It is not because the lifetime must be 'static. The object is being handed over to v8 for tracking and garbage collection. Therefore, it must be bounded by the 'static lifetime. I.e., the struct must be capable of being held for the entirety of the program.

If the struct were valid for a shorter time period, it might become invalid while it is still being held by Javascript. Dereferencing this struct would be a use-after-free bug.

Referencing counting (e.g., Arc) is a common way to upgrade something to a 'static lifetime.

I'm going to close this issue because it sounds like 'static is already supported and lifetimes other than 'static cannot be supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants