@@ -24,26 +24,27 @@ use std::boxed::Box;
24
24
#[ cfg( all( feature = "std" , feature = "c-types" ) ) ]
25
25
pub mod c;
26
26
27
- /// Panic if a pointer is null.
28
27
#[ inline]
29
28
fn panic_if_null < T > ( pointer : * const T ) {
30
29
if pointer. is_null ( ) {
31
30
unreachable ! ( "A null pointer was passed to the library, something is wrong in the C or C++ code" ) ;
32
31
}
33
32
}
34
33
35
- /// Convert type to raw pointer.
34
+ /// Convert type to raw pointer ready to be used as opaque pointer .
36
35
#[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
37
36
#[ inline]
38
37
pub fn raw < T > ( data : T ) -> * mut T {
39
38
return Box :: into_raw ( Box :: new ( data) ) ;
40
39
}
41
40
42
- /// Free pointer to type.
41
+ /// Free memory of a previous type converted to raw pointer .
43
42
///
44
43
/// # Safety
45
44
///
46
- /// Never call it twice. That could produce a HEAP error that produce a crash.
45
+ /// The pointer must be a valid reference and never call it twice or behavior is undefined.
46
+ ///
47
+ /// That could produce a HEAP error that produce a crash.
47
48
#[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
48
49
#[ inline]
49
50
pub unsafe fn free < T > ( pointer : * mut T ) {
@@ -58,11 +59,13 @@ pub unsafe fn free<T>(pointer: *mut T) {
58
59
// We let drop the boxed data.
59
60
}
60
61
61
- /// Own back from a raw pointer.
62
+ /// Own back from a raw pointer to use Rust ownership as usually .
62
63
///
63
64
/// # Safety
64
65
///
65
- /// Never call it twice. That could produce a HEAP error that produce a crash.
66
+ /// The pointer must be a valid reference and never call it twice or behavior is undefined.
67
+ ///
68
+ /// That could produce a HEAP error that produce a crash.
66
69
#[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
67
70
#[ inline]
68
71
pub unsafe fn own_back < T > ( pointer : * mut T ) -> T {
@@ -72,23 +75,33 @@ pub unsafe fn own_back<T>(pointer: *mut T) -> T {
72
75
return * boxed;
73
76
}
74
77
75
- /// Convert raw pointer to type to type reference.
78
+ /// Convert raw pointer to type to type reference but without back to own it.
79
+ ///
80
+ /// That's the main difference with `own_back<T>`, it does not back to use ownership
81
+ /// and values will not be dropped.
76
82
///
77
83
/// # Safety
78
84
///
79
- /// The pointer must be a valid reference to that value with that type.
85
+ /// The pointer must be a valid reference and never call it twice or behavior is undefined.
86
+ ///
87
+ /// That could produce a HEAP error that produce a crash.
80
88
#[ inline]
81
89
pub unsafe fn object < ' a , T > ( pointer : * const T ) -> & ' a T {
82
90
panic_if_null ( pointer) ;
83
91
// CAUTION: this is unsafe
84
92
return & * pointer;
85
93
}
86
94
87
- /// Convert raw pointer to type into type mutable reference.
95
+ /// Convert raw pointer to type into type mutable reference but without back to own it.
96
+ ///
97
+ /// That's the main difference with `own_back<T>`, it does not back to use ownership
98
+ /// and values will not be dropped.
88
99
///
89
100
/// # Safety
90
101
///
91
- /// The pointer must be a valid reference to that value with that type.
102
+ /// The pointer must be a valid reference and never call it twice or behavior is undefined.
103
+ ///
104
+ /// That could produce a HEAP error that produce a crash.
92
105
#[ inline]
93
106
pub unsafe fn mut_object < ' a , T > ( pointer : * mut T ) -> & ' a mut T {
94
107
panic_if_null ( pointer) ;
0 commit comments