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

Investigate adding std::any::Any to instance #28

Open
jonnyboyC opened this issue Oct 15, 2020 · 0 comments
Open

Investigate adding std::any::Any to instance #28

jonnyboyC opened this issue Oct 15, 2020 · 0 comments
Labels
refactor Work around internal structural changes.

Comments

@jonnyboyC
Copy link
Collaborator

I'm thinking there may be some benefit to adding some generic fields to the Instance struct. Something like

#[derive(PartialEq, Clone)]
pub struct Instance {
  pub class: Managed<Class>,
  fields: Box<[Value]>,
  native_data: Option<Managed<ManagedAny>>
} 

I think this gives us some options to store extra data related to native functionality without need several new types to values. The example in mind is the beginnings of the regexp module. Here I'm primarily just wrapping the regex crate. For each of the three test methods I have test, match and captures I have currently construct a new Regex struct each time this is invoked which is quite wasteful. Instead I could make a new trait ManagedAny something like

trait ManagedAny : Manage + any::Any {}

This way I can store essentially arbitrary extra data on an instance such as a Regex struct and implement the normal Manage members. Inside methods on my Regexp class I should be able to call.

let instance = this.unwrap().to_instance();
let regex = match instance.native_data.downcast_ref::<Regex>().expect("Expected Regex")
// use regex
@jonnyboyC jonnyboyC added the refactor Work around internal structural changes. label Oct 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Work around internal structural changes.
Projects
None yet
Development

No branches or pull requests

1 participant