mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-07-27 20:33:54 -06:00
rust: prefer NonNull::new to assertions
Do not use new_unchecked; the effect is the same, but the code is easier to read and unsafe regions become smaller. Likewise, NonNull::new can be used instead of assertion and followed by as_ref() or as_mut() instead of dereferencing the pointer. Suggested-by: Zhao Liu <zhao1.liu@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
24f0e8d818
commit
7d0520398f
5 changed files with 35 additions and 47 deletions
|
@ -4,7 +4,7 @@
|
|||
|
||||
//! Bindings to create devices and access device functionality from Rust.
|
||||
|
||||
use std::ffi::CStr;
|
||||
use std::{ffi::CStr, ptr::NonNull};
|
||||
|
||||
pub use bindings::{DeviceClass, DeviceState, Property};
|
||||
|
||||
|
@ -55,9 +55,8 @@ pub trait DeviceImpl {
|
|||
/// can be downcasted to type `T`. We also expect the device is
|
||||
/// readable/writeable from one thread at any time.
|
||||
unsafe extern "C" fn rust_realize_fn<T: DeviceImpl>(dev: *mut DeviceState, _errp: *mut *mut Error) {
|
||||
assert!(!dev.is_null());
|
||||
let state = dev.cast::<T>();
|
||||
T::REALIZE.unwrap()(unsafe { &mut *state });
|
||||
let state = NonNull::new(dev).unwrap().cast::<T>();
|
||||
T::REALIZE.unwrap()(unsafe { state.as_ref() });
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
|
@ -66,9 +65,8 @@ unsafe extern "C" fn rust_realize_fn<T: DeviceImpl>(dev: *mut DeviceState, _errp
|
|||
/// can be downcasted to type `T`. We also expect the device is
|
||||
/// readable/writeable from one thread at any time.
|
||||
unsafe extern "C" fn rust_reset_fn<T: DeviceImpl>(dev: *mut DeviceState) {
|
||||
assert!(!dev.is_null());
|
||||
let state = dev.cast::<T>();
|
||||
T::RESET.unwrap()(unsafe { &mut *state });
|
||||
let mut state = NonNull::new(dev).unwrap().cast::<T>();
|
||||
T::RESET.unwrap()(unsafe { state.as_mut() });
|
||||
}
|
||||
|
||||
impl<T> ClassInitImpl<DeviceClass> for T
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue