Skip to content

Commit

Permalink
Merge pull request #1417 from dtolnay/uptrasptr
Browse files Browse the repository at this point in the history
Update more UniquePtr methods to use as_ptr and as_mut_ptr
  • Loading branch information
dtolnay authored Dec 25, 2024
2 parents 9737c10 + 8ea95e8 commit dc4b034
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/unique_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,22 @@ where
///
/// This is the opposite of [std::unique_ptr\<T\>::operator bool](https://en.cppreference.com/w/cpp/memory/unique_ptr/operator_bool).
pub fn is_null(&self) -> bool {
let ptr = unsafe { T::__get(self.repr) };
ptr.is_null()
self.as_ptr().is_null()
}

/// Returns a reference to the object owned by this UniquePtr if any,
/// otherwise None.
pub fn as_ref(&self) -> Option<&T> {
unsafe { T::__get(self.repr).as_ref() }
let ptr = self.as_ptr();
unsafe { ptr.as_ref() }
}

/// Returns a mutable pinned reference to the object owned by this UniquePtr
/// if any, otherwise None.
pub fn as_mut(&mut self) -> Option<Pin<&mut T>> {
let ptr = self.as_mut_ptr();
unsafe {
let mut_reference = (T::__get(self.repr) as *mut T).as_mut()?;
let mut_reference = ptr.as_mut()?;
Some(Pin::new_unchecked(mut_reference))
}
}
Expand All @@ -93,10 +94,7 @@ where
/// Returns a raw const pointer to the object owned by this UniquePtr if
/// any, otherwise the null pointer.
pub fn as_ptr(&self) -> *const T {
match self.as_ref() {
Some(target) => target as *const T,
None => std::ptr::null(),
}
unsafe { T::__get(self.repr) }
}

/// Returns a raw mutable pointer to the object owned by this UniquePtr if
Expand Down

0 comments on commit dc4b034

Please sign in to comment.