Skip to content

Commit

Permalink
Avoid clippy warning on reference to static mut
Browse files Browse the repository at this point in the history
It will be a hard error in 2024 edition so implement it "properly"

warning: creating a mutable reference to mutable static is discouraged
   --> diesel/src/sqlite/connection/mod.rs:229:18
    |
229 |         unsafe { &mut SQLITE_METADATA_LOOKUP }
    |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable reference to mutable static
    |
    = note: for more information, see issue #114447 <rust-lang/rust#114447>
    = note: this will be a hard error in the 2024 edition
    = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior
    = note: `#[warn(static_mut_refs)]` on by default
  • Loading branch information
momobel committed Apr 12, 2024
1 parent 0b3699d commit e2c2c11
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions diesel/src/sqlite/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ pub struct SqliteConnection {
statement_cache: StatementCache<Sqlite, Statement>,
raw_connection: RawConnection,
transaction_state: AnsiTransactionManager,
// this exists for the sole purpose of implementing `WithMetadataLookup` trait
// and avoiding static mut which will be deprecated in 2024 edition
metadata_lookup: (),
instrumentation: Option<Box<dyn Instrumentation>>,
}

Expand Down Expand Up @@ -221,12 +224,9 @@ impl LoadConnection<DefaultLoadingMode> for SqliteConnection {
}
}

static mut SQLITE_METADATA_LOOKUP: () = ();

impl WithMetadataLookup for SqliteConnection {
fn metadata_lookup(&mut self) -> &mut <Sqlite as TypeMetadata>::MetadataLookup {
// safe since it's a unit type
unsafe { &mut SQLITE_METADATA_LOOKUP }
&mut self.metadata_lookup
}
}

Expand Down Expand Up @@ -539,6 +539,7 @@ impl SqliteConnection {
statement_cache: StatementCache::new(),
raw_connection,
transaction_state: AnsiTransactionManager::default(),
metadata_lookup: (),
instrumentation: None,
};
conn.register_diesel_sql_functions()
Expand Down

0 comments on commit e2c2c11

Please sign in to comment.