Skip to content

Commit

Permalink
Invert the dependency between UniquePtr as_ptr and as_ref
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Dec 25, 2024
1 parent 9737c10 commit 37a533a
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/unique_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ where
/// 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
Expand Down Expand Up @@ -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 37a533a

Please sign in to comment.