Skip to content

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

@jonnyboyC

Description

@jonnyboyC

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    refactorWork around internal structural changes.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions