Skip to content

Commit

Permalink
clean up safety docs
Browse files Browse the repository at this point in the history
  • Loading branch information
russelltg committed Feb 18, 2023
1 parent 22e6d16 commit f03d505
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/shared_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ where
}

/// Create a shared pointer from an already-allocated object
/// Corresponds to constructor (3) of [std::shared\_ptr](https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr)
/// Corresponds to constructor (3) of [`std::shared_ptr`](https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr)
///
/// The SharedPtr gains ownership of the pointer and will call std::default_delete on it when the refcount goes to zero.
/// The SharedPtr gains ownership of the pointer and will call `std::default_delete` on it when the refcount goes to zero.
/// The data will not be moved, so any pointers to this data elsewhere in the program continue to be valid
///
/// # Safety
///
/// Value must either be null or point to a valid instance of T
/// * Value must either be null or point to a valid instance of T
/// * Value must not be deleted (as the `std::shared_ptr` now manages its lifetime)
/// * Value must not be accessed after the last `std::shared_ptr` is dropped
pub unsafe fn from_unmanaged(value: *mut T) -> Self {
let mut shared_ptr = MaybeUninit::<SharedPtr<T>>::uninit();
let new = shared_ptr.as_mut_ptr().cast();
Expand Down
2 changes: 1 addition & 1 deletion src/unique_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl<T> UniquePtr<T>
where
T: UniquePtrTarget + SharedPtrTarget,
{
/// Convert this UniquePtr to a SharedPtr, analogous to constructor (13) for [std::shared\_ptr](https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr)
/// Convert this UniquePtr to a SharedPtr, analogous to constructor (13) for [`std::shared_ptr`](https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr)
pub fn to_shared(self) -> SharedPtr<T> {
unsafe { SharedPtr::from_unmanaged(self.into_raw()) }
}
Expand Down

0 comments on commit f03d505

Please sign in to comment.