Skip to main content

ProvideRef

Trait ProvideRef 

Source
pub trait ProvideRef<T> {
    // Required method
    fn provide(&self) -> &T;
}
Expand description

Trait to allow getting a reference to the T from the implementing type. AppContext implements this for various types it contains. This allows a method to specify the type it requires, then the caller of the method can determine how to provide the type. This is a similar concept to dependency injection (DI) in frameworks like Java Spring, though this is far from a full DI system.

This is useful, for example, to allow mocking the DB connection in tests. Your DB operation method would declare a parameter of type ProvideRef<DataBaseConnection>, then your application code would provide the AppContext to the method, and your tests could provide a mocked ProvideRef instance that returns a mock DB connection. Note that mocking the DB comes with its own set of trade-offs, for example, it may not exactly match the behavior of an actual DB that’s used in production. Consider testing against an actual DB instead of mocking, e.g., by using test containers.

A mocked implementation of the trait is provided if the testing-mocks feature is enabled.

See also:

Required Methods§

Source

fn provide(&self) -> &T

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl ProvideRef<AppConfig> for AppContext

Source§

impl ProvideRef<AppMetadata> for AppContext

Source§

impl ProvideRef<DatabaseConnection> for AppContext

Available on crate feature db-sea-orm only.
Source§

impl ProvideRef<PGMQueue> for AppContext

Available on crate feature worker-pg only.
Source§

impl ProvideRef<Pool<AsyncDieselConnectionManager<AsyncMysqlConnection>>> for AppContext

Available on crate feature db-diesel-mysql-pool-async only.
Source§

impl ProvideRef<Pool<AsyncDieselConnectionManager<AsyncPgConnection>>> for AppContext

Available on crate feature db-diesel-postgres-pool-async only.
Source§

impl ProvideRef<Pool<ConnectionManager<MysqlConnection>>> for AppContext

Available on crate feature db-diesel-mysql-pool only.
Source§

impl ProvideRef<Pool<ConnectionManager<PgConnection>>> for AppContext

Available on crate feature db-diesel-postgres-pool only.
Source§

impl ProvideRef<Pool<ConnectionManager<SqliteConnection>>> for AppContext

Available on crate feature db-diesel-sqlite-pool only.
Source§

impl ProvideRef<RedisEnqueue> for AppContext

Available on crate feature worker-sidekiq only.
Source§

impl ProvideRef<Sender> for AppContext

Available on crate feature email-sendgrid only.
Source§

impl ProvideRef<SmtpTransport> for AppContext

Available on crate feature email-smtp only.
Source§

impl<T> ProvideRef<T> for MockProvideRef<T>

Trait to allow getting a reference to the T from the implementing type. AppContext implements this for various types it contains. This allows a method to specify the type it requires, then the caller of the method can determine how to provide the type. This is a similar concept to dependency injection (DI) in frameworks like Java Spring, though this is far from a full DI system.

This is useful, for example, to allow mocking the DB connection in tests. Your DB operation method would declare a parameter of type ProvideRef<DataBaseConnection>, then your application code would provide the AppContext to the method, and your tests could provide a mocked ProvideRef instance that returns a mock DB connection. Note that mocking the DB comes with its own set of trade-offs, for example, it may not exactly match the behavior of an actual DB that’s used in production. Consider testing against an actual DB instead of mocking, e.g., by using test containers.

A mocked implementation of the trait is provided if the testing-mocks feature is enabled.

See also: