Fix static_mut_ref warning

This commit is contained in:
Gary Guo 2024-01-10 21:37:59 +00:00
parent e9eefeed51
commit 6720a34c66

View File

@ -32,7 +32,7 @@ unsafe fn lock_global_state() -> impl ops::DerefMut<Target = GlobalState> {
#[cfg(feature = "libc")] #[cfg(feature = "libc")]
{ {
static mut MUTEX: libc::pthread_mutex_t = libc::PTHREAD_MUTEX_INITIALIZER; static mut MUTEX: libc::pthread_mutex_t = libc::PTHREAD_MUTEX_INITIALIZER;
unsafe { libc::pthread_mutex_lock(&mut MUTEX) }; unsafe { libc::pthread_mutex_lock(core::ptr::addr_of_mut!(MUTEX)) };
static mut STATE: GlobalState = GlobalState { static mut STATE: GlobalState = GlobalState {
object: ptr::null_mut(), object: ptr::null_mut(),
@ -41,20 +41,22 @@ unsafe fn lock_global_state() -> impl ops::DerefMut<Target = GlobalState> {
struct LockGuard; struct LockGuard;
impl Drop for LockGuard { impl Drop for LockGuard {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { libc::pthread_mutex_unlock(&mut MUTEX) }; unsafe { libc::pthread_mutex_unlock(core::ptr::addr_of_mut!(MUTEX)) };
} }
} }
impl ops::Deref for LockGuard { impl ops::Deref for LockGuard {
type Target = GlobalState; type Target = GlobalState;
#[allow(static_mut_ref)]
fn deref(&self) -> &GlobalState { fn deref(&self) -> &GlobalState {
unsafe { &STATE } unsafe { &*core::ptr::addr_of!(STATE) }
} }
} }
impl ops::DerefMut for LockGuard { impl ops::DerefMut for LockGuard {
fn deref_mut(&mut self) -> &mut GlobalState { fn deref_mut(&mut self) -> &mut GlobalState {
unsafe { &mut STATE } unsafe { &mut *core::ptr::addr_of_mut!(STATE) }
} }
} }