Skip to content

Commit 2105750

Browse files
committed
refactor: Simplify check of invalid pointers
1 parent ae5ac0c commit 2105750

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

src/lib.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ lazy_static! {
4141

4242
#[cfg(all(feature = "std", feature = "lender"))]
4343
#[inline]
44-
fn is_lent<T>(pointer: *const T) -> bool {
45-
return LENT.read().unwrap().contains(&(pointer as usize));
44+
fn invalid_error_check<T>(pointer: *const T) -> Result<(), crate::error::PointerError> {
45+
if !LENT.read().unwrap().contains(&(pointer as usize)) {
46+
return Err(crate::error::PointerError::Invalid);
47+
}
48+
return Ok(());
4649
}
4750

4851
#[inline]
@@ -101,9 +104,7 @@ pub unsafe fn free<T>(pointer: *mut T) {
101104
pub unsafe fn own_back<T>(pointer: *mut T) -> Result<T, crate::error::PointerError> {
102105
null_error_check(pointer)?;
103106
#[cfg(all(feature = "std", feature = "lender"))]
104-
if !is_lent(pointer) {
105-
return Err(crate::error::PointerError::Invalid);
106-
}
107+
invalid_error_check(pointer)?;
107108
let boxed = { Box::from_raw(pointer) };
108109
#[cfg(all(feature = "std", feature = "lender"))]
109110
LENT.write().unwrap().remove(&(pointer as usize));
@@ -126,9 +127,7 @@ pub unsafe fn own_back<T>(pointer: *mut T) -> Result<T, crate::error::PointerErr
126127
pub unsafe fn object<'a, T>(pointer: *const T) -> Result<&'a T, crate::error::PointerError> {
127128
null_error_check(pointer)?;
128129
#[cfg(all(feature = "std", feature = "lender"))]
129-
if !is_lent(pointer) {
130-
return Err(crate::error::PointerError::Invalid);
131-
}
130+
invalid_error_check(pointer)?;
132131
return Ok(&*pointer);
133132
}
134133

@@ -148,8 +147,6 @@ pub unsafe fn object<'a, T>(pointer: *const T) -> Result<&'a T, crate::error::Po
148147
pub unsafe fn mut_object<'a, T>(pointer: *mut T) -> Result<&'a mut T, crate::error::PointerError> {
149148
null_error_check(pointer)?;
150149
#[cfg(all(feature = "std", feature = "lender"))]
151-
if !is_lent(pointer) {
152-
return Err(crate::error::PointerError::Invalid);
153-
}
150+
invalid_error_check(pointer)?;
154151
return Ok(&mut *pointer);
155152
}

0 commit comments

Comments
 (0)