Skip to content

Commit

Permalink
Touch up PR 892
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Dec 25, 2024
1 parent e580f4c commit 9737c10
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/unique_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,22 @@ where
}
}

/// Returns a pointer to the object owned by this UniquePtr
/// if any, otherwise the null pointer.
/// 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(),
}
}

/// Returns a mutable pointer to the object owned by this UniquePtr
/// if any, otherwise the null pointer.
/// Returns a raw mutable pointer to the object owned by this UniquePtr if
/// any, otherwise the null pointer.
///
/// As with [std::unique_ptr\<T\>::get](https://en.cppreference.com/w/cpp/memory/unique_ptr/get),
/// this doesn't require that you hold a mutable reference to the `UniquePtr`.
/// This differs from Rust norms, so extra care should be taken in
/// the way the pointer is used.
/// this doesn't require that you hold an exclusive reference to the
/// UniquePtr. This differs from Rust norms, so extra care should be taken
/// in the way the pointer is used.
pub fn as_mut_ptr(&self) -> *mut T {
self.as_ptr() as *mut T
}
Expand Down
4 changes: 2 additions & 2 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ fn test_c_method_calls() {
assert_eq!(2021, unique_ptr.get());
assert_eq!(2021, unique_ptr.get2());
assert_eq!(2021, *unique_ptr.getRef());
assert_eq!(2021, unsafe { unique_ptr.as_mut_ptr().as_ref() }.unwrap().get());
assert_eq!(2021, unsafe { unique_ptr.as_ptr().as_ref() }.unwrap().get());
assert_eq!(2021, unsafe { &mut *unique_ptr.as_mut_ptr() }.get());
assert_eq!(2021, unsafe { &*unique_ptr.as_ptr() }.get());
assert_eq!(2021, *unique_ptr.pin_mut().getMut());
assert_eq!(2022, unique_ptr.pin_mut().set_succeed(2022).unwrap());
assert!(unique_ptr.pin_mut().get_fail().is_err());
Expand Down

0 comments on commit 9737c10

Please sign in to comment.