Trait Resource

Source
pub trait Resource:
    'static
    + Send
    + Sync
    + Any {
    // Required method
    fn name() -> String;

    // Provided method
    fn on_resource_wired(&mut self) { ... }
}
Expand description

A trait for anything that can be stored (and retrieved) as a resource.

Typically, the type that implements this trait also should implement Clone since the same resource may be requested by several tasks and thus it would be an additional bound on most methods that work with Resource.

§Example


/// An abstract interface you want to share.
/// Normally you want the interface to be thread-safe.
trait MyInterface: 'static + Send + Sync {
    fn do_something(&self);
}

/// Resource wrapper.
#[derive(Clone)]
struct MyResource(Arc<dyn MyInterface>);

impl Resource for MyResource {
    fn name() -> String {
        // It is a helpful practice to follow a structured naming pattern for resource names.
        // For example, you can use a certain prefix for all resources related to a some component, e.g. `api`.
        "common/my_resource".to_string()
    }
}

Required Methods§

Source

fn name() -> String

Returns the name of the resource. Used for logging purposes.

Provided Methods§

Source

fn on_resource_wired(&mut self)

Invoked after the wiring phase of the service is done. Can be used to perform additional resource preparation, knowing that the resource is guaranteed to be requested by all the tasks that need it.

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§