File tree Expand file tree Collapse file tree 3 files changed +31
-23
lines changed Expand file tree Collapse file tree 3 files changed +31
-23
lines changed Original file line number Diff line number Diff line change @@ -13,3 +13,4 @@ repository = "https://github.com/jhg/opaque-pointer-rs/"
13
13
[features ]
14
14
default = [" std" ]
15
15
std = []
16
+ c-types = []
Original file line number Diff line number Diff line change
1
+ //! # C types FFI opaque pointers.
2
+ //!
3
+ //! Specific C types like C-like string pointers.
4
+
5
+ #![ cfg( all( feature = "std" , feature = "c-types" ) ) ]
6
+
7
+ use std:: os:: raw:: c_char;
8
+ use std:: ffi:: CStr ;
9
+
10
+ use super :: panic_if_null;
11
+
12
+ /// Reference to a C string.
13
+ ///
14
+ /// # Safety
15
+ ///
16
+ /// The pointer must be a valid reference to that value with that type.
17
+ ///
18
+ /// # Panics
19
+ ///
20
+ /// This could panic if the C string is not a valid UTF-8 string.
21
+ #[ must_use]
22
+ #[ inline]
23
+ pub unsafe fn ref_str < ' a > ( string : * const c_char ) -> & ' a str {
24
+ panic_if_null ( string) ;
25
+ // CAUTION: this is unsafe
26
+ let string = CStr :: from_ptr ( string) ;
27
+ return string. to_str ( ) . expect ( "Invalid UTF-8 string from C or C++ code" ) ;
28
+ }
Original file line number Diff line number Diff line change @@ -20,10 +20,8 @@ extern crate std;
20
20
#[ cfg( feature = "std" ) ]
21
21
use std:: boxed:: Box ;
22
22
23
- #[ cfg( feature = "std" ) ]
24
- use std:: os:: raw:: c_char;
25
- #[ cfg( feature = "std" ) ]
26
- use std:: ffi:: CStr ;
23
+ #[ cfg( all( feature = "std" , feature = "c-types" ) ) ]
24
+ pub mod c;
27
25
28
26
/// Panic if a pointer is null.
29
27
#[ inline]
@@ -93,22 +91,3 @@ pub unsafe fn mut_object<'a, T>(pointer: *mut T) -> &'a mut T {
93
91
// CAUTION: this is unsafe
94
92
& mut * pointer
95
93
}
96
-
97
- /// Reference to a C string.
98
- ///
99
- /// # Safety
100
- ///
101
- /// The pointer must be a valid reference to that value with that type.
102
- ///
103
- /// # Panics
104
- ///
105
- /// This could panic if the C string is not a valid UTF-8 string.
106
- #[ cfg( feature = "std" ) ]
107
- #[ must_use]
108
- #[ inline]
109
- pub unsafe fn ref_str < ' a > ( string : * const c_char ) -> & ' a str {
110
- panic_if_null ( string) ;
111
- // CAUTION: this is unsafe
112
- let string = CStr :: from_ptr ( string) ;
113
- string. to_str ( ) . expect ( "Invalid UTF-8 string from C or C++ code" )
114
- }
You can’t perform that action at this time.
0 commit comments