-
Hello, apologies if this is a beginner (or unrelated) post. I have a trait // Non object safe
// vvvvv
pub trait Object: UserData {
// Object safe methods
}
pub struct ConcreteObject {}
impl Object for ConcreteObject {}
impl UserData for ConcreteObject {} It is possible to convert it into a userdata with I could of course create an Enum of all Objects, but I would rather not as it makes it harder to use and it cannot be extended from outside libraries. Also thought I should mention I'm not too worried about the performance of dynamic dispatch. If it is impossible to do what I'm attempting however I'll just end up making an enum. Am I overlooking something? Or is there just no way at the moment to do this with mlua? Thanks for help in advance, and if you need any clarification feel free to ask. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You can implement trait Object {}
struct ConcreteObject;
impl Object for ConcreteObject {}
impl UserData for Box<dyn Object> {}
let ud = lua.create_userdata::<Box<dyn Object>>(Box::new(ConcreteObject))?; You probably should not use |
Beta Was this translation helpful? Give feedback.
It's a bit non-trivial, but you can add proxying
__index
metatamethod toBox<dyn Object>
to forward requests to target imlementation.Something like this: