Skip to content

Commit 9f437d8

Browse files
committed
doc: Improve doc comment to resume it and clarify
1 parent ac30452 commit 9f437d8

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

src/lib.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,16 @@ fn panic_if_null<T>(pointer: *const T) {
3333
}
3434
}
3535

36-
/// Convert type to raw pointer ready to be used as opaque pointer.
36+
/// Get a heap-allocated raw pointer without ownership.
37+
///
38+
/// To back to manage the memory with ownership use [`own_back<T>()`].
3739
#[cfg(any(feature = "alloc", feature = "std"))]
3840
#[inline]
3941
pub fn raw<T>(data: T) -> *mut T {
4042
return Box::into_raw(Box::new(data));
4143
}
4244

43-
/// Free memory of a previous type converted to raw pointer.
44-
///
45-
/// # Safety
46-
///
47-
/// The pointer must be a valid reference and never call it twice or behavior is undefined.
48-
///
49-
/// That could produce a HEAP error that produce a crash.
45+
/// Opposite of [`raw<T>()`], to use Rust's ownership as usually.
5046
#[cfg(any(feature = "alloc", feature = "std"))]
5147
#[inline]
5248
pub unsafe fn free<T>(pointer: *mut T) {
@@ -63,7 +59,6 @@ pub unsafe fn free<T>(pointer: *mut T) {
6359
// We let drop the boxed data.
6460
}
6561

66-
/// Own back from a raw pointer to use Rust ownership as usually.
6762
///
6863
/// # Safety
6964
///
@@ -75,21 +70,19 @@ pub unsafe fn free<T>(pointer: *mut T) {
7570
pub unsafe fn own_back<T>(pointer: *mut T) -> T {
7671
#[cfg(any(feature = "panic-if-null", debug_assertions))]
7772
panic_if_null(pointer);
78-
// CAUTION: this is unsafe
73+
// CAUTION: this is the unsafe part of the function.
7974
let boxed = Box::from_raw(pointer);
8075
return *boxed;
8176
}
8277

83-
/// Convert raw pointer to type to type reference but without back to own it.
78+
/// Reference to a object but without back to own it.
8479
///
85-
/// That's the main difference with `own_back<T>`, it does not back to use ownership
86-
/// and values will not be dropped.
80+
/// That's the difference with [`own_back<T>()`], you must
81+
/// use [`own_back<T>()`] to own it again and it will be dropped.
8782
///
8883
/// # Safety
8984
///
90-
/// The pointer must be a valid reference and never call it twice or behavior is undefined.
91-
///
92-
/// That could produce a HEAP error that produce a crash.
85+
/// Invalid pointer or call it twice could cause an undefined behavior or heap error and a crash.
9386
#[inline]
9487
pub unsafe fn object<'a, T>(pointer: *const T) -> &'a T {
9588
#[cfg(any(feature = "panic-if-null", debug_assertions))]
@@ -98,16 +91,14 @@ pub unsafe fn object<'a, T>(pointer: *const T) -> &'a T {
9891
return &*pointer;
9992
}
10093

101-
/// Convert raw pointer to type into type mutable reference but without back to own it.
94+
/// Mutable reference to a object but without back to own it.
10295
///
103-
/// That's the main difference with `own_back<T>`, it does not back to use ownership
104-
/// and values will not be dropped.
96+
/// That's the difference with [`own_back<T>()`], you must
97+
/// use [`own_back<T>()`] to own it again and it will be dropped.
10598
///
10699
/// # Safety
107100
///
108-
/// The pointer must be a valid reference and never call it twice or behavior is undefined.
109-
///
110-
/// That could produce a HEAP error that produce a crash.
101+
/// Invalid pointer or call it twice could cause an undefined behavior or heap error and a crash.
111102
#[inline]
112103
pub unsafe fn mut_object<'a, T>(pointer: *mut T) -> &'a mut T {
113104
#[cfg(any(feature = "panic-if-null", debug_assertions))]

0 commit comments

Comments
 (0)