@@ -2165,33 +2165,50 @@ pub fn as_bytes_slice<'a>(s: &'a str) -> &'a [u8] {
2165
2165
}
2166
2166
2167
2167
/**
2168
- * Work with the byte buffer of a string as a null-terminated C string.
2169
- *
2170
- * Allows for unsafe manipulation of strings, which is useful for foreign
2171
- * interop. This is similar to `str::as_buf`, but guarantees null-termination.
2172
- * If the given slice is not already null-terminated, this function will
2173
- * allocate a temporary, copy the slice, null terminate it, and pass
2174
- * that instead.
2175
- *
2176
- * # Example
2177
- *
2178
- * ~~~ {.rust}
2179
- * let s = str::as_c_str("PATH", { |path| libc::getenv(path) });
2180
- * ~~~
2168
+ * A dummy trait to hold all the utility methods that we implement on strings.
2181
2169
*/
2182
- #[ inline]
2183
- pub fn as_c_str < T > ( s : & str , f : & fn ( * libc:: c_char ) -> T ) -> T {
2184
- do as_buf ( s) |buf, len| {
2185
- // NB: len includes the trailing null.
2186
- assert ! ( len > 0 ) ;
2187
- if unsafe { * ( ptr:: offset ( buf, len-1 ) ) != 0 } {
2188
- as_c_str ( to_owned ( s) , f)
2189
- } else {
2190
- f ( buf as * libc:: c_char )
2170
+ pub trait StrUtil {
2171
+ /**
2172
+ * Work with the byte buffer of a string as a null-terminated C string.
2173
+ *
2174
+ * Allows for unsafe manipulation of strings, which is useful for foreign
2175
+ * interop. This is similar to `str::as_buf`, but guarantees null-termination.
2176
+ * If the given slice is not already null-terminated, this function will
2177
+ * allocate a temporary, copy the slice, null terminate it, and pass
2178
+ * that instead.
2179
+ *
2180
+ * # Example
2181
+ *
2182
+ * ~~~ {.rust}
2183
+ * let s = "PATH".as_c_str(|path| libc::getenv(path));
2184
+ * ~~~
2185
+ */
2186
+ fn as_c_str < T > ( self , f : & fn ( * libc:: c_char ) -> T ) -> T ;
2187
+ }
2188
+
2189
+ impl < ' self > StrUtil for & ' self str {
2190
+ #[ inline]
2191
+ fn as_c_str < T > ( self , f : & fn ( * libc:: c_char ) -> T ) -> T {
2192
+ do as_buf ( self ) |buf, len| {
2193
+ // NB: len includes the trailing null.
2194
+ assert ! ( len > 0 ) ;
2195
+ if unsafe { * ( ptr:: offset ( buf, len-1 ) ) != 0 } {
2196
+ to_owned ( self ) . as_c_str ( f)
2197
+ } else {
2198
+ f ( buf as * libc:: c_char )
2199
+ }
2191
2200
}
2192
2201
}
2193
2202
}
2194
2203
2204
+ /**
2205
+ * Deprecated. Use the `as_c_str` method on strings instead.
2206
+ */
2207
+ #[ inline( always) ]
2208
+ pub fn as_c_str < T > ( s : & str , f : & fn ( * libc:: c_char ) -> T ) -> T {
2209
+ s. as_c_str ( f)
2210
+ }
2211
+
2195
2212
/**
2196
2213
* Work with the byte buffer and length of a slice.
2197
2214
*
0 commit comments