pub trait Object: Display + Debug + Any + Sync + Send {
fn get_attr(&self, name: &str) -> Option<Value> { ... }
fn attributes(&self) -> &[&str] { ... }
fn call_method(
&self,
state: &State<'_, '_>,
name: &str,
args: &[Value]
) -> Result<Value, Error> { ... }
fn call(&self, state: &State<'_, '_>, args: &[Value]) -> Result<Value, Error> { ... }
}
Expand description
A utility trait that represents a dynamic object.
The engine uses the Value
type to represent values that the engine
knows about. Most of these values are primitives such as integers, strings
or maps. However it is also possible to expose custom types without
undergoing a serialization step to the engine. For this to work a type
needs to implement the Object
trait and be wrapped in a value with
Value::from_object
. The ownership of
the object will then move into the value type.
The engine uses reference counted objects with interior mutability in the
value type. This means that all trait methods take &self
and types like
Mutex
need to be used to enable mutability.
Objects need to implement Display
which is used by
the engine to convert the object into a string if needed. Additionally
Debug
is required as well.
Provided Methods
Invoked by the engine to get the attribute of an object.
Where possible it’s a good idea for this to align with the return value
of attributes
but it’s not necessary.
If an attribute does not exist, None
shall be returned.
fn attributes(&self) -> &[&str]
fn attributes(&self) -> &[&str]
An enumeration of attributes that are known to exist on this object.
The default implementation returns an empty slice. If it’s not possible
to implement this, it’s fine for the implementation to be omitted. The
enumeration here is used by the for
loop to iterate over the attributes
on the value.
Called when the engine tries to call a method on the object.
It’s the responsibility of the implementer to ensure that an error is generated if an invalid method is invoked.
To convert the arguments into arguments use the
from_args
function.