add pv_ref, drop unsafe from pv_raw

pv_raw itself doesn't actually dereference the pointer and
performs the necessary checks to not be considered unsafe
per se, the `pv_ref()` and `pv_mut_ref()` method now returns
a mutable reference.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2020-11-25 12:08:51 +01:00
parent 0eebb7e1d9
commit c837c7cc3b

View File

@ -256,11 +256,7 @@ impl ScalarRef {
}
/// Interpret the byte string as a raw pointer.
///
/// # Safety
///
/// The user is responsible for making sure the underlying pointer is correct.
pub unsafe fn pv_raw<T>(&self) -> Result<*mut T, Error> {
pub fn pv_raw<T>(&self) -> Result<*mut T, Error> {
let bytes = self.pv_bytes();
let bytes: [u8; mem::size_of::<usize>()] = bytes
@ -270,6 +266,25 @@ impl ScalarRef {
Ok(usize::from_ne_bytes(bytes) as *mut T)
}
/// Interpret the byte string as a pointer and return it as a reference for convenience.
///
/// # Safety
///
/// The user is responsible for making sure the underlying pointer is correct.
pub unsafe fn pv_ref<T>(&self) -> Result<&T, Error> {
self.pv_raw().map(|p| unsafe { &*p })
}
/// Interpret the byte string as a pointer and return it as a mutable reference for
/// convenience.
///
/// # Safety
///
/// The user is responsible for making sure the underlying pointer is correct.
pub unsafe fn pv_mut_ref<T>(&self) -> Result<&mut T, Error> {
self.pv_raw().map(|p| unsafe { &mut *p })
}
/// Create another owned reference to this value.
pub fn clone_ref(&self) -> Scalar {
unsafe { Scalar::from_raw_ref(self.sv()) }