Trait Resource

Source
pub trait Resource:
    Send
    + Sync
    + Sized
    + 'static {
    type Args: Args;
    type Dependencies: ResourceDependencies;
    type CreationError: Into<Box<dyn Error + 'static>>;

    const NAME: &str;

    // Required method
    fn new(
        deps: Self::Dependencies,
        args: Self::Args,
        api: &mut AssemblyRuntime<'_>,
    ) -> Result<Arc<Self>, Self::CreationError>;

    // Provided method
    fn provide_as_trait<'a>(_: &'a mut TraitInstaller<'_, 'a, '_, Self>) { ... }
}
Expand description

The main unit of work in an Assembly and the trait common to each of the nodes in its DAG.

There is an attribute macro resource which can be attached to implementations of this trait which will automatically derive the definitions of all the associated types and constant so that the only thing that needs to be supplied is the new method.

Required Associated Constants§

Source

const NAME: &str

The name of this resource. Used in logs and resource graph diagnostics.

Required Associated Types§

Source

type Args: Args

Command line arguments that this Resource would like to receive. For example a resource that implements an HTTP server might use this to configure which address to listen on.

This is expected to be a struct defined like so:

#[derive(clap::Args, Debug)]
#[group(skip)]
struct Args {
    #[arg(long)]
    port: Option<u16>,
}

These args will be collected along with the args from all other resources into a clap::Parser and the individual Args instances will be handed to each resource at constrction time.

Source

type Dependencies: ResourceDependencies

Other resources that this Resource depends on. The resources in this collection will be constructed before this resource, then this structure will be filled in with Arc references to those constructed instances and passed to the constructor of the current resource.

This type should satisfy the ResourceDependencies trait by deriving it.

Source

type CreationError: Into<Box<dyn Error + 'static>>

Type of error returned by new. If returned, the creation of the whole assembly will be aborted.

Required Methods§

Source

fn new( deps: Self::Dependencies, args: Self::Args, api: &mut AssemblyRuntime<'_>, ) -> Result<Arc<Self>, Self::CreationError>

Construct a resource of this type. Called while the graph of all resources is built in crate::Assembly::new.

Returns an Arc<Self> which will be made available to other resources that depend on this one.

The AssemblyRuntime argument may be used to install a task (a unit of work) that will run in connection with this resource.

Provided Methods§

Source

fn provide_as_trait<'a>(_: &'a mut TraitInstaller<'_, 'a, '_, Self>)

Make this resource available to other resources that declare a dependency on a trait object. This is used for one resource to collect dependencies on all of the other resources in the assembly that share some property (by implementins a trait). By default, a resource is available to be declared as a dependency under its own name only, not as any dyn Trait.

To make this resource available as one or more trait objects, see the resource attribute macro (which will supply an appropriate definition for this method).

To declare a dependency on a resource that makes itself available in this way, see the ResourceDependencies derive macro.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§