tools::sgutils2: use NonNull

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2021-01-25 14:54:33 +01:00
parent fe61280b6b
commit b7f9b25e4d

View File

@ -5,6 +5,7 @@
//! See: `/usr/include/scsi/sg_pt.h` //! See: `/usr/include/scsi/sg_pt.h`
use std::os::unix::io::AsRawFd; use std::os::unix::io::AsRawFd;
use std::ptr::NonNull;
use anyhow::{bail, format_err, Error}; use anyhow::{bail, format_err, Error};
use endian_trait::Endian; use endian_trait::Endian;
@ -18,32 +19,29 @@ struct SgPtBase { _private: [u8; 0] }
#[repr(transparent)] #[repr(transparent)]
struct SgPt { struct SgPt {
raw: *mut SgPtBase, raw: NonNull<SgPtBase>,
} }
impl Drop for SgPt { impl Drop for SgPt {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { destruct_scsi_pt_obj(self.raw) }; unsafe { destruct_scsi_pt_obj(self.as_mut_ptr()) };
} }
} }
impl SgPt { impl SgPt {
fn new() -> Result<Self, Error> { fn new() -> Result<Self, Error> {
let raw = unsafe { construct_scsi_pt_obj() }; Ok(Self {
raw: NonNull::new(unsafe { construct_scsi_pt_obj() })
if raw.is_null() { .ok_or_else(|| format_err!("construct_scsi_pt_ob failed"))?,
bail!("construct_scsi_pt_ob failed"); })
}
Ok(Self { raw })
} }
fn as_ptr(&self) -> *const SgPtBase { fn as_ptr(&self) -> *const SgPtBase {
self.raw as *const SgPtBase self.raw.as_ptr()
} }
fn as_mut_ptr(&mut self) -> *mut SgPtBase { fn as_mut_ptr(&mut self) -> *mut SgPtBase {
self.raw self.raw.as_ptr()
} }
} }