Trait Resource

Source
pub trait Resource<Kind: ResourceKind = Plain>:
    'static
    + Send
    + Sync {
    // Required method
    fn name() -> String;
}
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);
}

impl Resource<Shared> for dyn MyInterface {
    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()
    }
}

// The resource can now be injected / requested as `Arc<dyn MyInterface>`

Required Methods§

Source

fn name() -> String

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

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.

Implementations on Foreign Types§

Source§

impl<T: Resource<Boxed> + ?Sized> Resource for Box<T>

Source§

impl<T: Resource<Shared> + ?Sized> Resource for Arc<T>

Implementors§