1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use super::{ProbeArgType, ProbeArgWrapper};
use std::ffi::CString;
#[cfg(unix)]
use std::ffi::{OsStr, OsString};
use std::os::raw::c_char;
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
impl ProbeArgType<&str> for &str {
type WrapperType = Option<CString>;
fn wrap(arg: &str) -> Self::WrapperType {
CString::new(arg).ok()
}
}
impl ProbeArgType<&String> for &String {
type WrapperType = Option<CString>;
fn wrap(arg: &String) -> Self::WrapperType {
CString::new(arg.as_str()).ok()
}
}
#[cfg(unix)]
impl ProbeArgType<&OsStr> for &OsStr {
type WrapperType = Option<CString>;
fn wrap(arg: &OsStr) -> Self::WrapperType {
CString::new(arg.as_bytes()).ok()
}
}
#[cfg(unix)]
impl ProbeArgType<&OsString> for &OsString {
type WrapperType = Option<CString>;
fn wrap(arg: &OsString) -> Self::WrapperType {
CString::new(arg.as_bytes()).ok()
}
}
impl<'a> ProbeArgWrapper for Option<CString> {
type CType = *const c_char;
fn as_c_type(&self) -> Self::CType {
self.as_ref()
.map(|x| x.as_ptr())
.unwrap_or_else(Self::default_c_value)
}
}
#[cfg(test)]
mod tests {
use crate::{wrap, ProbeArgType, ProbeArgWrapper};
use std::ffi::{CStr, CString};
#[cfg(unix)]
use std::ffi::{OsStr, OsString};
use std::fmt::Debug;
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
#[quickcheck]
fn string_as_c_type(x: String) -> bool {
let wrapper = wrap(&x);
let pointer: *const i8 = wrapper.as_c_type();
test_with_string(&x, pointer);
true
}
#[quickcheck]
fn str_as_c_type(x: String) -> bool {
let wrapper = wrap(x.as_str());
let pointer: *const i8 = wrapper.as_c_type();
test_with_string(x.as_str(), pointer);
true
}
#[quickcheck]
#[cfg(unix)]
fn osstring_as_c_type(x: OsString) -> bool {
let wrapper = wrap(&x);
let pointer: *const i8 = wrapper.as_c_type();
test_with_string(&x, pointer);
true
}
#[quickcheck]
#[cfg(unix)]
fn osstr_as_c_type(x: OsString) -> bool {
let wrapper = wrap(x.as_os_str());
let pointer: *const i8 = wrapper.as_c_type();
test_with_string(x.as_os_str(), pointer);
true
}
trait StringHelpers {
fn to_cstring(&self) -> Option<CString>;
fn assert_equals(&self, x: &str) -> ();
}
impl StringHelpers for &str {
fn to_cstring(&self) -> Option<CString> {
CString::new(*self).ok()
}
fn assert_equals(&self, x: &str) -> () {
assert_eq!(*self, x)
}
}
impl StringHelpers for &String {
fn to_cstring(&self) -> Option<CString> {
CString::new(self.as_str()).ok()
}
fn assert_equals(&self, x: &str) -> () {
assert_eq!(self.as_str(), x)
}
}
#[cfg(unix)]
impl StringHelpers for &OsStr {
fn to_cstring(&self) -> Option<CString> {
CString::new(Vec::from(self.as_bytes())).ok()
}
fn assert_equals(&self, x: &str) -> () {
assert_eq!(self.to_str().expect("should always be valid UTF-8"), x)
}
}
#[cfg(unix)]
impl StringHelpers for &OsString {
fn to_cstring(&self) -> Option<CString> {
CString::new(Vec::from(self.as_bytes())).ok()
}
fn assert_equals(&self, x: &str) -> () {
assert_eq!(self.to_str().expect("should always be valid UTF-8"), x)
}
}
fn test_with_string<'a, T: StringHelpers + ProbeArgType<T> + Debug>(x: T, pointer: *const i8) {
if pointer.is_null() {
assert!(x.to_cstring().is_none());
} else {
let cstr = unsafe { CStr::from_ptr(pointer) };
let as_string = cstr
.to_str()
.expect("The string should always be valid unicode");
x.assert_equals(as_string);
}
}
}