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§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementors§
impl ProvideRef<AppConfig> for AppContext
impl ProvideRef<AppMetadata> for AppContext
impl ProvideRef<DatabaseConnection> for AppContext
db-sea-orm only.impl ProvideRef<PGMQueue> for AppContext
worker-pg only.impl ProvideRef<Pool<AsyncDieselConnectionManager<AsyncMysqlConnection>>> for AppContext
db-diesel-mysql-pool-async only.impl ProvideRef<Pool<AsyncDieselConnectionManager<AsyncPgConnection>>> for AppContext
db-diesel-postgres-pool-async only.impl ProvideRef<Pool<ConnectionManager<MysqlConnection>>> for AppContext
db-diesel-mysql-pool only.impl ProvideRef<Pool<ConnectionManager<PgConnection>>> for AppContext
db-diesel-postgres-pool only.impl ProvideRef<Pool<ConnectionManager<SqliteConnection>>> for AppContext
db-diesel-sqlite-pool only.impl ProvideRef<RedisEnqueue> for AppContext
worker-sidekiq only.impl ProvideRef<Sender> for AppContext
email-sendgrid only.impl ProvideRef<SmtpTransport> for AppContext
email-smtp only.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: