diff options
| author | Paolo Bonzini <pbonzini@redhat.com> | 2025-01-23 11:25:22 +0100 |
|---|---|---|
| committer | Paolo Bonzini <pbonzini@redhat.com> | 2025-01-23 18:47:46 +0100 |
| commit | 7d0520398f7f58214cf5242b34c1b46efa2fcf4f (patch) | |
| tree | 1aa706baadc819ff3f569af00c91ba99fe4696fd /rust/hw/char/pl011/src/memory_ops.rs | |
| parent | 24f0e8d818b931758b6dc47f973a6b1b80ecee1f (diff) | |
| download | focaccia-qemu-7d0520398f7f58214cf5242b34c1b46efa2fcf4f.tar.gz focaccia-qemu-7d0520398f7f58214cf5242b34c1b46efa2fcf4f.zip | |
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>
Diffstat (limited to 'rust/hw/char/pl011/src/memory_ops.rs')
| -rw-r--r-- | rust/hw/char/pl011/src/memory_ops.rs | 9 |
1 files changed, 3 insertions, 6 deletions
diff --git a/rust/hw/char/pl011/src/memory_ops.rs b/rust/hw/char/pl011/src/memory_ops.rs index c4e8599ba4..a286003d13 100644 --- a/rust/hw/char/pl011/src/memory_ops.rs +++ b/rust/hw/char/pl011/src/memory_ops.rs @@ -25,7 +25,7 @@ pub static PL011_OPS: MemoryRegionOps = MemoryRegionOps { unsafe extern "C" fn pl011_read(opaque: *mut c_void, addr: hwaddr, size: c_uint) -> u64 { assert!(!opaque.is_null()); - let mut state = unsafe { NonNull::new_unchecked(opaque.cast::<PL011State>()) }; + let mut state = NonNull::new(opaque).unwrap().cast::<PL011State>(); let val = unsafe { state.as_mut().read(addr, size) }; match val { std::ops::ControlFlow::Break(val) => val, @@ -43,9 +43,6 @@ unsafe extern "C" fn pl011_read(opaque: *mut c_void, addr: hwaddr, size: c_uint) } unsafe extern "C" fn pl011_write(opaque: *mut c_void, addr: hwaddr, data: u64, _size: c_uint) { - unsafe { - assert!(!opaque.is_null()); - let mut state = NonNull::new_unchecked(opaque.cast::<PL011State>()); - state.as_mut().write(addr, data) - } + let mut state = NonNull::new(opaque).unwrap().cast::<PL011State>(); + unsafe { state.as_mut().write(addr, data) } } |