Impl AsMut for advisory lock types (#2520) (#2554)

The documentation for `PgAdvisoryLockGuard` lists a set of types
that should be able to be passed to it, but when actually trying
to do so, compilation would fail due to missing `AsMut` trait
implementations for those types. This commit adds the missing
`AsMut` impls so that `Transaction` and `PgConnection` can be used
as type parameters to `PgAdvisoryLockGuard`, as expected.

For reference: https://github.com/launchbadge/sqlx/issues/2520
This commit is contained in:
Andrew Lilley Brinker 2023-06-30 16:53:14 -07:00 committed by GitHub
parent e2ce463af8
commit 1bdbedabdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View File

@ -197,6 +197,16 @@ where
}
}
// Implement `AsMut<DB::Connection>` so `Transaction` can be given to a
// `PgAdvisoryLockGuard`.
//
// See: https://github.com/launchbadge/sqlx/issues/2520
impl<'c, DB: Database> AsMut<DB::Connection> for Transaction<'c, DB> {
fn as_mut(&mut self) -> &mut DB::Connection {
&mut self.connection
}
}
impl<'c, 't, DB: Database> crate::acquire::Acquire<'t> for &'t mut Transaction<'c, DB> {
type Database = DB;

View File

@ -213,3 +213,13 @@ impl Connection for PgConnection {
!self.stream.write_buffer().is_empty()
}
}
// Implement `AsMut<Self>` so that `PgConnection` can be wrapped in
// a `PgAdvisoryLockGuard`.
//
// See: https://github.com/launchbadge/sqlx/issues/2520
impl AsMut<PgConnection> for PgConnection {
fn as_mut(&mut self) -> &mut PgConnection {
self
}
}