diff --git a/perlmod/src/scalar.rs b/perlmod/src/scalar.rs index 84d8e7d..bbd7f93 100644 --- a/perlmod/src/scalar.rs +++ b/perlmod/src/scalar.rs @@ -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(&self) -> Result<*mut T, Error> { + pub fn pv_raw(&self) -> Result<*mut T, Error> { let bytes = self.pv_bytes(); let bytes: [u8; mem::size_of::()] = 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(&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(&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()) }