1
1
//! # FFI opaque pointers.
2
- //!
2
+ //!
3
3
//! FFI to use Rust objects from C as opaque pointer.
4
4
5
5
#![ allow( unsafe_code) ]
8
8
#![ deny( clippy:: complexity) ]
9
9
#![ deny( clippy:: cognitive_complexity) ]
10
10
#![ allow( clippy:: needless_return) ] // To avoid surprise in devs more familiar where return is always explicit
11
-
12
11
#![ doc( html_no_source) ]
13
-
14
12
#![ no_std]
15
13
16
14
#[ cfg( all( feature = "alloc" , not( feature = "std" ) ) ) ]
@@ -36,18 +34,15 @@ fn panic_if_null<T>(pointer: *const T) {
36
34
}
37
35
38
36
/// Get a heap-allocated raw pointer without ownership.
39
- ///
37
+ ///
40
38
/// To back to manage the memory with ownership use [`own_back<T>()`].
41
39
#[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
42
40
#[ inline]
43
41
pub fn raw < T > ( data : T ) -> * mut T {
44
42
return Box :: into_raw ( Box :: new ( data) ) ;
45
43
}
46
44
47
- #[ deprecated(
48
- since = "0.7.2" ,
49
- note = "Use `own_back<T>()` instead"
50
- ) ]
45
+ #[ deprecated( since = "0.7.2" , note = "Use `own_back<T>()` instead" ) ]
51
46
#[ allow( missing_docs) ]
52
47
#[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
53
48
#[ inline]
@@ -56,11 +51,11 @@ pub unsafe fn free<T>(pointer: *mut T) {
56
51
}
57
52
58
53
/// Opposite of [`raw<T>()`], to use Rust's ownership as usually.
59
- ///
54
+ ///
60
55
/// # Safety
61
- ///
56
+ ///
62
57
/// The pointer must be a valid reference and never call it twice or behavior is undefined.
63
- ///
58
+ ///
64
59
/// That could produce a HEAP error that produce a crash.
65
60
#[ doc( alias = "free" ) ]
66
61
#[ cfg( any( feature = "alloc" , feature = "std" ) ) ]
@@ -74,12 +69,12 @@ pub unsafe fn own_back<T>(pointer: *mut T) -> T {
74
69
}
75
70
76
71
/// Reference to a object but without back to own it.
77
- ///
72
+ ///
78
73
/// That's the difference with [`own_back<T>()`], you must
79
74
/// use [`own_back<T>()`] to own it again and it will be dropped.
80
- ///
75
+ ///
81
76
/// # Safety
82
- ///
77
+ ///
83
78
/// Invalid pointer or call it twice could cause an undefined behavior or heap error and a crash.
84
79
#[ inline]
85
80
pub unsafe fn object < ' a , T > ( pointer : * const T ) -> & ' a T {
@@ -90,12 +85,12 @@ pub unsafe fn object<'a, T>(pointer: *const T) -> &'a T {
90
85
}
91
86
92
87
/// Mutable reference to a object but without back to own it.
93
- ///
88
+ ///
94
89
/// That's the difference with [`own_back<T>()`], you must
95
90
/// use [`own_back<T>()`] to own it again and it will be dropped.
96
- ///
91
+ ///
97
92
/// # Safety
98
- ///
93
+ ///
99
94
/// Invalid pointer or call it twice could cause an undefined behavior or heap error and a crash.
100
95
#[ inline]
101
96
pub unsafe fn mut_object < ' a , T > ( pointer : * mut T ) -> & ' a mut T {
0 commit comments