macro_rules! new_box {
($dyn_trait:ty, $ctor:expr) => { ... };
}
Expand description
Defines the template used to create every instance in a linked::Box<T>
object family.
This macro is meant to be used in the context of creating a new instance of a linked object
type T
that is meant to be always expressed via an abstraction (dyn SomeTrait
).
§Arguments
$dyn_trait
- The trait object that the linked object is to be used as (e.g.dyn SomeTrait
).$ctor
- The template for constructing new instances of the linked object on demand. This will be used in a factory function and it will move-capture any referenced state. All captured values must be thread-safe (Send
+Sync
+'static
).
§Example
// If using linked::Box, do not put `#[linked::object]` on the struct.
// The linked::Box itself is the linked object and our struct is only its contents.
struct XmlConfig {
config: String
}
impl XmlConfig {
pub fn new_as_config_source() -> linked::Box<dyn ConfigSource> {
linked::new_box!(
dyn ConfigSource,
Self {
config: "xml".to_string(),
}
)
}
}
See examples/linked_box.rs
for a complete example.