From b7f9b25e4d0e0801b9bb98a902289e09e01292da Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Mon, 25 Jan 2021 14:54:33 +0100 Subject: [PATCH] tools::sgutils2: use NonNull Signed-off-by: Wolfgang Bumiller --- src/tools/sgutils2.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/tools/sgutils2.rs b/src/tools/sgutils2.rs index 12c91918..274f81b1 100644 --- a/src/tools/sgutils2.rs +++ b/src/tools/sgutils2.rs @@ -5,6 +5,7 @@ //! See: `/usr/include/scsi/sg_pt.h` use std::os::unix::io::AsRawFd; +use std::ptr::NonNull; use anyhow::{bail, format_err, Error}; use endian_trait::Endian; @@ -18,32 +19,29 @@ struct SgPtBase { _private: [u8; 0] } #[repr(transparent)] struct SgPt { - raw: *mut SgPtBase, + raw: NonNull, } -impl Drop for SgPt { +impl Drop for SgPt { fn drop(&mut self) { - unsafe { destruct_scsi_pt_obj(self.raw) }; + unsafe { destruct_scsi_pt_obj(self.as_mut_ptr()) }; } } impl SgPt { fn new() -> Result { - let raw = unsafe { construct_scsi_pt_obj() }; - - if raw.is_null() { - bail!("construct_scsi_pt_ob failed"); - } - - Ok(Self { raw }) + Ok(Self { + raw: NonNull::new(unsafe { construct_scsi_pt_obj() }) + .ok_or_else(|| format_err!("construct_scsi_pt_ob failed"))?, + }) } fn as_ptr(&self) -> *const SgPtBase { - self.raw as *const SgPtBase + self.raw.as_ptr() } fn as_mut_ptr(&mut self) -> *mut SgPtBase { - self.raw + self.raw.as_ptr() } }