plugger_types/lib.rs
1pub type TypeName = &'static str;
2
3#[derive(Clone)]
4pub struct Parameter {
5 pub name: String,
6 pub ty: TypeName,
7}
8
9#[derive(Clone)]
10pub struct Method
11{
12 /// A pointer to the function.
13 pub method_pointer: *mut fn(),
14
15 /// The name of the method.
16 pub name: &'static str,
17 /// The parameter list.
18 pub parameters: Vec<Parameter>,
19 /// The return type (if any).
20 pub ret: Option<TypeName>,
21
22 /// Whether the method has a receiver.
23 pub is_static: bool,
24}
25
26#[derive(Clone)]
27pub struct Field
28{
29 /// The number of bytes from the start of the structure.
30 pub field_offset: usize,
31
32 pub ty: TypeName,
33 pub name: &'static str,
34}
35
36#[derive(Clone)]
37pub struct Class
38{
39 pub name: String,
40 pub fields: Vec<Field>,
41 pub methods: Vec<Method>,
42}
43
44pub trait PluggableFields
45{
46 fn pluggable_fields(&self) -> Vec<Field>;
47}
48
49pub trait PluggableMethods
50{
51 fn pluggable_methods(&self) -> Vec<Method>;
52}
53
54/// An object that can be plugged into a scripting language.
55///
56/// Can be automatically derived by placing `#[pluggable]` on a struct.
57///
58/// When implementing `Pluggable`, it is necessary to have both a `struct` and an
59/// `impl`, with both having the `#[pluggable]` attribute.
60///
61/// This has been split up into two other traits
62///
63/// * `PluggableFields`
64/// * `PluggableMethods
65///
66/// This is because the syntax extender can not see all of the code -
67/// it can only see the thing it's operating on. This is why we need
68/// an attribute on the `struct` and the `impl`. We can't implement
69/// a trait twice, each time overriding a specific method.
70///
71/// Because of this, it is necessary to mark both the `struct` and the `impl`
72/// with the `pluggable` attribute. It's not the prettiest, but it does work.
73pub trait Pluggable : PluggableFields + PluggableMethods
74{
75 fn name(&self) -> &'static str;
76
77 fn fields(&self) -> Vec<Field> { PluggableFields::pluggable_fields(self) }
78 fn methods(&self) -> Vec<Method> { PluggableMethods::pluggable_methods(self) }
79
80 fn class(&self) -> Class {
81 Class {
82 name: self.name().to_owned(),
83 fields: self.fields(),
84 methods: self.methods(),
85 }
86 }
87}
88