c_str.rs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // http://rust-lang.org/COPYRIGHT.
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. use alloc::borrow::{Borrow, Cow, ToOwned};
  11. use alloc::boxed::Box;
  12. use alloc::rc::Rc;
  13. use alloc::string::String;
  14. use alloc::sync::Arc;
  15. use alloc::vec::Vec;
  16. use core::ascii;
  17. use core::cmp::Ordering;
  18. use core::fmt::{self, Write};
  19. use core::mem;
  20. use core::ops;
  21. use core::ptr;
  22. use core::slice;
  23. use core::str::{self, Utf8Error};
  24. use header::string::strlen;
  25. use platform::types::*;
  26. pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
  27. use header::string;
  28. let p = unsafe {
  29. string::memchr(
  30. haystack.as_ptr() as *const c_void,
  31. needle as c_int,
  32. haystack.len(),
  33. )
  34. };
  35. if p.is_null() {
  36. None
  37. } else {
  38. Some(p as usize - (haystack.as_ptr() as usize))
  39. }
  40. }
  41. /// A type representing an owned, C-compatible, nul-terminated string with no nul bytes in the
  42. /// middle.
  43. ///
  44. /// This type serves the purpose of being able to safely generate a
  45. /// C-compatible string from a Rust byte slice or vector. An instance of this
  46. /// type is a static guarantee that the underlying bytes contain no interior 0
  47. /// bytes ("nul characters") and that the final byte is 0 ("nul terminator").
  48. ///
  49. /// `CString` is to [`CStr`] as [`String`] is to [`&str`]: the former
  50. /// in each pair are owned strings; the latter are borrowed
  51. /// references.
  52. ///
  53. /// # Creating a `CString`
  54. ///
  55. /// A `CString` is created from either a byte slice or a byte vector,
  56. /// or anything that implements [`Into`]`<`[`Vec`]`<`[`u8`]`>>` (for
  57. /// example, you can build a `CString` straight out of a [`String`] or
  58. /// a [`&str`], since both implement that trait).
  59. ///
  60. /// The [`new`] method will actually check that the provided `&[u8]`
  61. /// does not have 0 bytes in the middle, and return an error if it
  62. /// finds one.
  63. ///
  64. /// # Extracting a raw pointer to the whole C string
  65. ///
  66. /// `CString` implements a [`as_ptr`] method through the [`Deref`]
  67. /// trait. This method will give you a `*const c_char` which you can
  68. /// feed directly to extern functions that expect a nul-terminated
  69. /// string, like C's `strdup()`.
  70. ///
  71. /// # Extracting a slice of the whole C string
  72. ///
  73. /// Alternatively, you can obtain a `&[`[`u8`]`]` slice from a
  74. /// `CString` with the [`as_bytes`] method. Slices produced in this
  75. /// way do *not* contain the trailing nul terminator. This is useful
  76. /// when you will be calling an extern function that takes a `*const
  77. /// u8` argument which is not necessarily nul-terminated, plus another
  78. /// argument with the length of the string — like C's `strndup()`.
  79. /// You can of course get the slice's length with its
  80. /// [`len`][slice.len] method.
  81. ///
  82. /// If you need a `&[`[`u8`]`]` slice *with* the nul terminator, you
  83. /// can use [`as_bytes_with_nul`] instead.
  84. ///
  85. /// Once you have the kind of slice you need (with or without a nul
  86. /// terminator), you can call the slice's own
  87. /// [`as_ptr`][slice.as_ptr] method to get a raw pointer to pass to
  88. /// extern functions. See the documentation for that function for a
  89. /// discussion on ensuring the lifetime of the raw pointer.
  90. ///
  91. /// [`Into`]: ../convert/trait.Into.html
  92. /// [`Vec`]: ../vec/struct.Vec.html
  93. /// [`String`]: ../string/struct.String.html
  94. /// [`&str`]: ../primitive.str.html
  95. /// [`u8`]: ../primitive.u8.html
  96. /// [`new`]: #method.new
  97. /// [`as_bytes`]: #method.as_bytes
  98. /// [`as_bytes_with_nul`]: #method.as_bytes_with_nul
  99. /// [`as_ptr`]: #method.as_ptr
  100. /// [slice.as_ptr]: ../primitive.slice.html#method.as_ptr
  101. /// [slice.len]: ../primitive.slice.html#method.len
  102. /// [`Deref`]: ../ops/trait.Deref.html
  103. /// [`CStr`]: struct.CStr.html
  104. ///
  105. /// # Examples
  106. ///
  107. /// ```ignore (extern-declaration)
  108. /// # fn main() {
  109. /// use std::ffi::CString;
  110. /// use std::os::raw::c_char;
  111. ///
  112. /// extern {
  113. /// fn my_printer(s: *const c_char);
  114. /// }
  115. ///
  116. /// // We are certain that our string doesn't have 0 bytes in the middle,
  117. /// // so we can .unwrap()
  118. /// let c_to_print = CString::new("Hello, world!").unwrap();
  119. /// unsafe {
  120. /// my_printer(c_to_print.as_ptr());
  121. /// }
  122. /// # }
  123. /// ```
  124. ///
  125. /// # Safety
  126. ///
  127. /// `CString` is intended for working with traditional C-style strings
  128. /// (a sequence of non-nul bytes terminated by a single nul byte); the
  129. /// primary use case for these kinds of strings is interoperating with C-like
  130. /// code. Often you will need to transfer ownership to/from that external
  131. /// code. It is strongly recommended that you thoroughly read through the
  132. /// documentation of `CString` before use, as improper ownership management
  133. /// of `CString` instances can lead to invalid memory accesses, memory leaks,
  134. /// and other memory errors.
  135. #[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Clone)]
  136. pub struct CString {
  137. // Invariant 1: the slice ends with a zero byte and has a length of at least one.
  138. // Invariant 2: the slice contains only one zero byte.
  139. // Improper usage of unsafe function can break Invariant 2, but not Invariant 1.
  140. inner: Box<[u8]>,
  141. }
  142. /// Representation of a borrowed C string.
  143. ///
  144. /// This type represents a borrowed reference to a nul-terminated
  145. /// array of bytes. It can be constructed safely from a `&[`[`u8`]`]`
  146. /// slice, or unsafely from a raw `*const c_char`. It can then be
  147. /// converted to a Rust [`&str`] by performing UTF-8 validation, or
  148. /// into an owned [`CString`].
  149. ///
  150. /// `CStr` is to [`CString`] as [`&str`] is to [`String`]: the former
  151. /// in each pair are borrowed references; the latter are owned
  152. /// strings.
  153. ///
  154. /// Note that this structure is **not** `repr(C)` and is not recommended to be
  155. /// placed in the signatures of FFI functions. Instead, safe wrappers of FFI
  156. /// functions may leverage the unsafe [`from_ptr`] constructor to provide a safe
  157. /// interface to other consumers.
  158. ///
  159. /// # Examples
  160. ///
  161. /// Inspecting a foreign C string:
  162. ///
  163. /// ```ignore (extern-declaration)
  164. /// use std::ffi::CStr;
  165. /// use std::os::raw::c_char;
  166. ///
  167. /// extern { fn my_string() -> *const c_char; }
  168. ///
  169. /// unsafe {
  170. /// let slice = CStr::from_ptr(my_string());
  171. /// println!("string buffer size without nul terminator: {}", slice.to_bytes().len());
  172. /// }
  173. /// ```
  174. ///
  175. /// Passing a Rust-originating C string:
  176. ///
  177. /// ```ignore (extern-declaration)
  178. /// use std::ffi::{CString, CStr};
  179. /// use std::os::raw::c_char;
  180. ///
  181. /// fn work(data: &CStr) {
  182. /// extern { fn work_with(data: *const c_char); }
  183. ///
  184. /// unsafe { work_with(data.as_ptr()) }
  185. /// }
  186. ///
  187. /// let s = CString::new("data data data data").unwrap();
  188. /// work(&s);
  189. /// ```
  190. ///
  191. /// Converting a foreign C string into a Rust [`String`]:
  192. ///
  193. /// ```ignore (extern-declaration)
  194. /// use std::ffi::CStr;
  195. /// use std::os::raw::c_char;
  196. ///
  197. /// extern { fn my_string() -> *const c_char; }
  198. ///
  199. /// fn my_string_safe() -> String {
  200. /// unsafe {
  201. /// CStr::from_ptr(my_string()).to_string_lossy().into_owned()
  202. /// }
  203. /// }
  204. ///
  205. /// println!("string: {}", my_string_safe());
  206. /// ```
  207. ///
  208. /// [`u8`]: ../primitive.u8.html
  209. /// [`&str`]: ../primitive.str.html
  210. /// [`String`]: ../string/struct.String.html
  211. /// [`CString`]: struct.CString.html
  212. /// [`from_ptr`]: #method.from_ptr
  213. #[derive(Hash)]
  214. pub struct CStr {
  215. // FIXME: this should not be represented with a DST slice but rather with
  216. // just a raw `c_char` along with some form of marker to make
  217. // this an unsized type. Essentially `sizeof(&CStr)` should be the
  218. // same as `sizeof(&c_char)` but `CStr` should be an unsized type.
  219. inner: [c_char],
  220. }
  221. /// An error indicating that an interior nul byte was found.
  222. ///
  223. /// While Rust strings may contain nul bytes in the middle, C strings
  224. /// can't, as that byte would effectively truncate the string.
  225. ///
  226. /// This error is created by the [`new`][`CString::new`] method on
  227. /// [`CString`]. See its documentation for more.
  228. ///
  229. /// [`CString`]: struct.CString.html
  230. /// [`CString::new`]: struct.CString.html#method.new
  231. ///
  232. /// # Examples
  233. ///
  234. /// ```
  235. /// use std::ffi::{CString, NulError};
  236. ///
  237. /// let _: NulError = CString::new(b"f\0oo".to_vec()).unwrap_err();
  238. /// ```
  239. #[derive(Clone, PartialEq, Eq, Debug)]
  240. pub struct NulError(usize, Vec<u8>);
  241. /// An error indicating that a nul byte was not in the expected position.
  242. ///
  243. /// The slice used to create a [`CStr`] must have one and only one nul
  244. /// byte at the end of the slice.
  245. ///
  246. /// This error is created by the
  247. /// [`from_bytes_with_nul`][`CStr::from_bytes_with_nul`] method on
  248. /// [`CStr`]. See its documentation for more.
  249. ///
  250. /// [`CStr`]: struct.CStr.html
  251. /// [`CStr::from_bytes_with_nul`]: struct.CStr.html#method.from_bytes_with_nul
  252. ///
  253. /// # Examples
  254. ///
  255. /// ```
  256. /// use std::ffi::{CStr, FromBytesWithNulError};
  257. ///
  258. /// let _: FromBytesWithNulError = CStr::from_bytes_with_nul(b"f\0oo").unwrap_err();
  259. /// ```
  260. #[derive(Clone, PartialEq, Eq, Debug)]
  261. pub struct FromBytesWithNulError {
  262. kind: FromBytesWithNulErrorKind,
  263. }
  264. #[derive(Clone, PartialEq, Eq, Debug)]
  265. enum FromBytesWithNulErrorKind {
  266. InteriorNul(usize),
  267. NotNulTerminated,
  268. }
  269. impl FromBytesWithNulError {
  270. fn interior_nul(pos: usize) -> FromBytesWithNulError {
  271. FromBytesWithNulError {
  272. kind: FromBytesWithNulErrorKind::InteriorNul(pos),
  273. }
  274. }
  275. fn not_nul_terminated() -> FromBytesWithNulError {
  276. FromBytesWithNulError {
  277. kind: FromBytesWithNulErrorKind::NotNulTerminated,
  278. }
  279. }
  280. fn description(&self) -> &str {
  281. match self.kind {
  282. FromBytesWithNulErrorKind::InteriorNul(..) => {
  283. "data provided contains an interior nul byte"
  284. }
  285. FromBytesWithNulErrorKind::NotNulTerminated => "data provided is not nul terminated",
  286. }
  287. }
  288. }
  289. /// An error indicating invalid UTF-8 when converting a [`CString`] into a [`String`].
  290. ///
  291. /// `CString` is just a wrapper over a buffer of bytes with a nul
  292. /// terminator; [`into_string`][`CString::into_string`] performs UTF-8
  293. /// validation on those bytes and may return this error.
  294. ///
  295. /// This `struct` is created by the
  296. /// [`into_string`][`CString::into_string`] method on [`CString`]. See
  297. /// its documentation for more.
  298. ///
  299. /// [`String`]: ../string/struct.String.html
  300. /// [`CString`]: struct.CString.html
  301. /// [`CString::into_string`]: struct.CString.html#method.into_string
  302. #[derive(Clone, PartialEq, Eq, Debug)]
  303. pub struct IntoStringError {
  304. inner: CString,
  305. error: Utf8Error,
  306. }
  307. impl CString {
  308. /// Creates a new C-compatible string from a container of bytes.
  309. ///
  310. /// This function will consume the provided data and use the
  311. /// underlying bytes to construct a new string, ensuring that
  312. /// there is a trailing 0 byte. This trailing 0 byte will be
  313. /// appended by this function; the provided data should *not*
  314. /// contain any 0 bytes in it.
  315. ///
  316. /// # Examples
  317. ///
  318. /// ```ignore (extern-declaration)
  319. /// use std::ffi::CString;
  320. /// use std::os::raw::c_char;
  321. ///
  322. /// extern { fn puts(s: *const c_char); }
  323. ///
  324. /// let to_print = CString::new("Hello!").unwrap();
  325. /// unsafe {
  326. /// puts(to_print.as_ptr());
  327. /// }
  328. /// ```
  329. ///
  330. /// # Errors
  331. ///
  332. /// This function will return an error if the supplied bytes contain an
  333. /// internal 0 byte. The [`NulError`] returned will contain the bytes as well as
  334. /// the position of the nul byte.
  335. ///
  336. /// [`NulError`]: struct.NulError.html
  337. pub fn new<T: Into<Vec<u8>>>(t: T) -> Result<CString, NulError> {
  338. Self::_new(t.into())
  339. }
  340. fn _new(bytes: Vec<u8>) -> Result<CString, NulError> {
  341. match memchr(0, &bytes) {
  342. Some(i) => Err(NulError(i, bytes)),
  343. None => Ok(unsafe { CString::from_vec_unchecked(bytes) }),
  344. }
  345. }
  346. /// Creates a C-compatible string by consuming a byte vector,
  347. /// without checking for interior 0 bytes.
  348. ///
  349. /// This method is equivalent to [`new`] except that no runtime assertion
  350. /// is made that `v` contains no 0 bytes, and it requires an actual
  351. /// byte vector, not anything that can be converted to one with Into.
  352. ///
  353. /// [`new`]: #method.new
  354. ///
  355. /// # Examples
  356. ///
  357. /// ```
  358. /// use std::ffi::CString;
  359. ///
  360. /// let raw = b"foo".to_vec();
  361. /// unsafe {
  362. /// let c_string = CString::from_vec_unchecked(raw);
  363. /// }
  364. /// ```
  365. pub unsafe fn from_vec_unchecked(mut v: Vec<u8>) -> CString {
  366. v.reserve_exact(1);
  367. v.push(0);
  368. CString {
  369. inner: v.into_boxed_slice(),
  370. }
  371. }
  372. /// Retakes ownership of a `CString` that was transferred to C via [`into_raw`].
  373. ///
  374. /// Additionally, the length of the string will be recalculated from the pointer.
  375. ///
  376. /// # Safety
  377. ///
  378. /// This should only ever be called with a pointer that was earlier
  379. /// obtained by calling [`into_raw`] on a `CString`. Other usage (e.g. trying to take
  380. /// ownership of a string that was allocated by foreign code) is likely to lead
  381. /// to undefined behavior or allocator corruption.
  382. ///
  383. /// > **Note:** If you need to borrow a string that was allocated by
  384. /// > foreign code, use [`CStr`]. If you need to take ownership of
  385. /// > a string that was allocated by foreign code, you will need to
  386. /// > make your own provisions for freeing it appropriately, likely
  387. /// > with the foreign code's API to do that.
  388. ///
  389. /// [`into_raw`]: #method.into_raw
  390. /// [`CStr`]: struct.CStr.html
  391. ///
  392. /// # Examples
  393. ///
  394. /// Create a `CString`, pass ownership to an `extern` function (via raw pointer), then retake
  395. /// ownership with `from_raw`:
  396. ///
  397. /// ```ignore (extern-declaration)
  398. /// use std::ffi::CString;
  399. /// use std::os::raw::c_char;
  400. ///
  401. /// extern {
  402. /// fn some_extern_function(s: *mut c_char);
  403. /// }
  404. ///
  405. /// let c_string = CString::new("Hello!").unwrap();
  406. /// let raw = c_string.into_raw();
  407. /// unsafe {
  408. /// some_extern_function(raw);
  409. /// let c_string = CString::from_raw(raw);
  410. /// }
  411. /// ```
  412. pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
  413. let len = strlen(ptr) + 1; // Including the NUL byte
  414. let slice = slice::from_raw_parts_mut(ptr, len as usize);
  415. CString {
  416. inner: Box::from_raw(slice as *mut [c_char] as *mut [u8]),
  417. }
  418. }
  419. /// Consumes the `CString` and transfers ownership of the string to a C caller.
  420. ///
  421. /// The pointer which this function returns must be returned to Rust and reconstituted using
  422. /// [`from_raw`] to be properly deallocated. Specifically, one
  423. /// should *not* use the standard C `free()` function to deallocate
  424. /// this string.
  425. ///
  426. /// Failure to call [`from_raw`] will lead to a memory leak.
  427. ///
  428. /// [`from_raw`]: #method.from_raw
  429. ///
  430. /// # Examples
  431. ///
  432. /// ```
  433. /// use std::ffi::CString;
  434. ///
  435. /// let c_string = CString::new("foo").unwrap();
  436. ///
  437. /// let ptr = c_string.into_raw();
  438. ///
  439. /// unsafe {
  440. /// assert_eq!(b'f', *ptr as u8);
  441. /// assert_eq!(b'o', *ptr.offset(1) as u8);
  442. /// assert_eq!(b'o', *ptr.offset(2) as u8);
  443. /// assert_eq!(b'\0', *ptr.offset(3) as u8);
  444. ///
  445. /// // retake pointer to free memory
  446. /// let _ = CString::from_raw(ptr);
  447. /// }
  448. /// ```
  449. #[inline]
  450. pub fn into_raw(self) -> *mut c_char {
  451. Box::into_raw(self.into_inner()) as *mut c_char
  452. }
  453. /// Converts the `CString` into a [`String`] if it contains valid UTF-8 data.
  454. ///
  455. /// On failure, ownership of the original `CString` is returned.
  456. ///
  457. /// [`String`]: ../string/struct.String.html
  458. ///
  459. /// # Examples
  460. ///
  461. /// ```
  462. /// use std::ffi::CString;
  463. ///
  464. /// let valid_utf8 = vec![b'f', b'o', b'o'];
  465. /// let cstring = CString::new(valid_utf8).unwrap();
  466. /// assert_eq!(cstring.into_string().unwrap(), "foo");
  467. ///
  468. /// let invalid_utf8 = vec![b'f', 0xff, b'o', b'o'];
  469. /// let cstring = CString::new(invalid_utf8).unwrap();
  470. /// let err = cstring.into_string().err().unwrap();
  471. /// assert_eq!(err.utf8_error().valid_up_to(), 1);
  472. /// ```
  473. pub fn into_string(self) -> Result<String, IntoStringError> {
  474. String::from_utf8(self.into_bytes()).map_err(|e| IntoStringError {
  475. error: e.utf8_error(),
  476. inner: unsafe { CString::from_vec_unchecked(e.into_bytes()) },
  477. })
  478. }
  479. /// Consumes the `CString` and returns the underlying byte buffer.
  480. ///
  481. /// The returned buffer does **not** contain the trailing nul
  482. /// terminator, and it is guaranteed to not have any interior nul
  483. /// bytes.
  484. ///
  485. /// # Examples
  486. ///
  487. /// ```
  488. /// use std::ffi::CString;
  489. ///
  490. /// let c_string = CString::new("foo").unwrap();
  491. /// let bytes = c_string.into_bytes();
  492. /// assert_eq!(bytes, vec![b'f', b'o', b'o']);
  493. /// ```
  494. pub fn into_bytes(self) -> Vec<u8> {
  495. let mut vec = self.into_inner().into_vec();
  496. let _nul = vec.pop();
  497. debug_assert_eq!(_nul, Some(0u8));
  498. vec
  499. }
  500. /// Equivalent to the [`into_bytes`] function except that the returned vector
  501. /// includes the trailing nul terminator.
  502. ///
  503. /// [`into_bytes`]: #method.into_bytes
  504. ///
  505. /// # Examples
  506. ///
  507. /// ```
  508. /// use std::ffi::CString;
  509. ///
  510. /// let c_string = CString::new("foo").unwrap();
  511. /// let bytes = c_string.into_bytes_with_nul();
  512. /// assert_eq!(bytes, vec![b'f', b'o', b'o', b'\0']);
  513. /// ```
  514. pub fn into_bytes_with_nul(self) -> Vec<u8> {
  515. self.into_inner().into_vec()
  516. }
  517. /// Returns the contents of this `CString` as a slice of bytes.
  518. ///
  519. /// The returned slice does **not** contain the trailing nul
  520. /// terminator, and it is guaranteed to not have any interior nul
  521. /// bytes. If you need the nul terminator, use
  522. /// [`as_bytes_with_nul`] instead.
  523. ///
  524. /// [`as_bytes_with_nul`]: #method.as_bytes_with_nul
  525. ///
  526. /// # Examples
  527. ///
  528. /// ```
  529. /// use std::ffi::CString;
  530. ///
  531. /// let c_string = CString::new("foo").unwrap();
  532. /// let bytes = c_string.as_bytes();
  533. /// assert_eq!(bytes, &[b'f', b'o', b'o']);
  534. /// ```
  535. #[inline]
  536. pub fn as_bytes(&self) -> &[u8] {
  537. &self.inner[..self.inner.len() - 1]
  538. }
  539. /// Equivalent to the [`as_bytes`] function except that the returned slice
  540. /// includes the trailing nul terminator.
  541. ///
  542. /// [`as_bytes`]: #method.as_bytes
  543. ///
  544. /// # Examples
  545. ///
  546. /// ```
  547. /// use std::ffi::CString;
  548. ///
  549. /// let c_string = CString::new("foo").unwrap();
  550. /// let bytes = c_string.as_bytes_with_nul();
  551. /// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']);
  552. /// ```
  553. #[inline]
  554. pub fn as_bytes_with_nul(&self) -> &[u8] {
  555. &self.inner
  556. }
  557. /// Extracts a [`CStr`] slice containing the entire string.
  558. ///
  559. /// [`CStr`]: struct.CStr.html
  560. ///
  561. /// # Examples
  562. ///
  563. /// ```
  564. /// use std::ffi::{CString, CStr};
  565. ///
  566. /// let c_string = CString::new(b"foo".to_vec()).unwrap();
  567. /// let c_str = c_string.as_c_str();
  568. /// assert_eq!(c_str, CStr::from_bytes_with_nul(b"foo\0").unwrap());
  569. /// ```
  570. #[inline]
  571. pub fn as_c_str(&self) -> &CStr {
  572. &*self
  573. }
  574. /// Converts this `CString` into a boxed [`CStr`].
  575. ///
  576. /// [`CStr`]: struct.CStr.html
  577. ///
  578. /// # Examples
  579. ///
  580. /// ```
  581. /// use std::ffi::{CString, CStr};
  582. ///
  583. /// let c_string = CString::new(b"foo".to_vec()).unwrap();
  584. /// let boxed = c_string.into_boxed_c_str();
  585. /// assert_eq!(&*boxed, CStr::from_bytes_with_nul(b"foo\0").unwrap());
  586. /// ```
  587. pub fn into_boxed_c_str(self) -> Box<CStr> {
  588. unsafe { Box::from_raw(Box::into_raw(self.into_inner()) as *mut CStr) }
  589. }
  590. // Bypass "move out of struct which implements [`Drop`] trait" restriction.
  591. ///
  592. /// [`Drop`]: ../ops/trait.Drop.html
  593. fn into_inner(self) -> Box<[u8]> {
  594. unsafe {
  595. let result = ptr::read(&self.inner);
  596. mem::forget(self);
  597. result
  598. }
  599. }
  600. }
  601. // Turns this `CString` into an empty string to prevent
  602. // memory unsafe code from working by accident. Inline
  603. // to prevent LLVM from optimizing it away in debug builds.
  604. impl Drop for CString {
  605. #[inline]
  606. fn drop(&mut self) {
  607. unsafe {
  608. *self.inner.get_unchecked_mut(0) = 0;
  609. }
  610. }
  611. }
  612. impl ops::Deref for CString {
  613. type Target = CStr;
  614. #[inline]
  615. fn deref(&self) -> &CStr {
  616. unsafe { CStr::from_bytes_with_nul_unchecked(self.as_bytes_with_nul()) }
  617. }
  618. }
  619. impl fmt::Debug for CString {
  620. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  621. fmt::Debug::fmt(&**self, f)
  622. }
  623. }
  624. impl From<CString> for Vec<u8> {
  625. /// Converts a [`CString`] into a [`Vec`]`<u8>`.
  626. ///
  627. /// The conversion consumes the [`CString`], and removes the terminating NUL byte.
  628. ///
  629. /// [`Vec`]: ../vec/struct.Vec.html
  630. /// [`CString`]: ../ffi/struct.CString.html
  631. #[inline]
  632. fn from(s: CString) -> Vec<u8> {
  633. s.into_bytes()
  634. }
  635. }
  636. impl fmt::Debug for CStr {
  637. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  638. write!(f, "\"")?;
  639. for byte in self
  640. .to_bytes()
  641. .iter()
  642. .flat_map(|&b| ascii::escape_default(b))
  643. {
  644. f.write_char(byte as char)?;
  645. }
  646. write!(f, "\"")
  647. }
  648. }
  649. impl<'a> Default for &'a CStr {
  650. fn default() -> &'a CStr {
  651. const SLICE: &[c_char] = &[0];
  652. unsafe { CStr::from_ptr(SLICE.as_ptr()) }
  653. }
  654. }
  655. impl Default for CString {
  656. /// Creates an empty `CString`.
  657. fn default() -> CString {
  658. let a: &CStr = Default::default();
  659. a.to_owned()
  660. }
  661. }
  662. impl Borrow<CStr> for CString {
  663. #[inline]
  664. fn borrow(&self) -> &CStr {
  665. self
  666. }
  667. }
  668. impl<'a> From<Cow<'a, CStr>> for CString {
  669. #[inline]
  670. fn from(s: Cow<'a, CStr>) -> Self {
  671. s.into_owned()
  672. }
  673. }
  674. impl<'a> From<&'a CStr> for Box<CStr> {
  675. fn from(s: &'a CStr) -> Box<CStr> {
  676. let boxed: Box<[u8]> = Box::from(s.to_bytes_with_nul());
  677. unsafe { Box::from_raw(Box::into_raw(boxed) as *mut CStr) }
  678. }
  679. }
  680. impl From<Box<CStr>> for CString {
  681. /// Converts a [`Box`]`<CStr>` into a [`CString`] without copying or allocating.
  682. ///
  683. /// [`Box`]: ../boxed/struct.Box.html
  684. /// [`CString`]: ../ffi/struct.CString.html
  685. #[inline]
  686. fn from(s: Box<CStr>) -> CString {
  687. s.into_c_string()
  688. }
  689. }
  690. impl Clone for Box<CStr> {
  691. #[inline]
  692. fn clone(&self) -> Self {
  693. (**self).into()
  694. }
  695. }
  696. impl From<CString> for Box<CStr> {
  697. /// Converts a [`CString`] into a [`Box`]`<CStr>` without copying or allocating.
  698. ///
  699. /// [`CString`]: ../ffi/struct.CString.html
  700. /// [`Box`]: ../boxed/struct.Box.html
  701. #[inline]
  702. fn from(s: CString) -> Box<CStr> {
  703. s.into_boxed_c_str()
  704. }
  705. }
  706. impl<'a> From<CString> for Cow<'a, CStr> {
  707. #[inline]
  708. fn from(s: CString) -> Cow<'a, CStr> {
  709. Cow::Owned(s)
  710. }
  711. }
  712. impl<'a> From<&'a CStr> for Cow<'a, CStr> {
  713. #[inline]
  714. fn from(s: &'a CStr) -> Cow<'a, CStr> {
  715. Cow::Borrowed(s)
  716. }
  717. }
  718. impl<'a> From<&'a CString> for Cow<'a, CStr> {
  719. #[inline]
  720. fn from(s: &'a CString) -> Cow<'a, CStr> {
  721. Cow::Borrowed(s.as_c_str())
  722. }
  723. }
  724. impl From<CString> for Arc<CStr> {
  725. /// Converts a [`CString`] into a [`Arc`]`<CStr>` without copying or allocating.
  726. ///
  727. /// [`CString`]: ../ffi/struct.CString.html
  728. /// [`Arc`]: ../sync/struct.Arc.html
  729. #[inline]
  730. fn from(s: CString) -> Arc<CStr> {
  731. let arc: Arc<[u8]> = Arc::from(s.into_inner());
  732. unsafe { Arc::from_raw(Arc::into_raw(arc) as *const CStr) }
  733. }
  734. }
  735. impl<'a> From<&'a CStr> for Arc<CStr> {
  736. #[inline]
  737. fn from(s: &CStr) -> Arc<CStr> {
  738. let arc: Arc<[u8]> = Arc::from(s.to_bytes_with_nul());
  739. unsafe { Arc::from_raw(Arc::into_raw(arc) as *const CStr) }
  740. }
  741. }
  742. impl From<CString> for Rc<CStr> {
  743. /// Converts a [`CString`] into a [`Rc`]`<CStr>` without copying or allocating.
  744. ///
  745. /// [`CString`]: ../ffi/struct.CString.html
  746. /// [`Rc`]: ../rc/struct.Rc.html
  747. #[inline]
  748. fn from(s: CString) -> Rc<CStr> {
  749. let rc: Rc<[u8]> = Rc::from(s.into_inner());
  750. unsafe { Rc::from_raw(Rc::into_raw(rc) as *const CStr) }
  751. }
  752. }
  753. impl<'a> From<&'a CStr> for Rc<CStr> {
  754. #[inline]
  755. fn from(s: &CStr) -> Rc<CStr> {
  756. let rc: Rc<[u8]> = Rc::from(s.to_bytes_with_nul());
  757. unsafe { Rc::from_raw(Rc::into_raw(rc) as *const CStr) }
  758. }
  759. }
  760. impl Default for Box<CStr> {
  761. fn default() -> Box<CStr> {
  762. let boxed: Box<[u8]> = Box::from([0]);
  763. unsafe { Box::from_raw(Box::into_raw(boxed) as *mut CStr) }
  764. }
  765. }
  766. impl NulError {
  767. /// Returns the position of the nul byte in the slice that caused
  768. /// [`CString::new`] to fail.
  769. ///
  770. /// [`CString::new`]: struct.CString.html#method.new
  771. ///
  772. /// # Examples
  773. ///
  774. /// ```
  775. /// use std::ffi::CString;
  776. ///
  777. /// let nul_error = CString::new("foo\0bar").unwrap_err();
  778. /// assert_eq!(nul_error.nul_position(), 3);
  779. ///
  780. /// let nul_error = CString::new("foo bar\0").unwrap_err();
  781. /// assert_eq!(nul_error.nul_position(), 7);
  782. /// ```
  783. pub fn nul_position(&self) -> usize {
  784. self.0
  785. }
  786. /// Consumes this error, returning the underlying vector of bytes which
  787. /// generated the error in the first place.
  788. ///
  789. /// # Examples
  790. ///
  791. /// ```
  792. /// use std::ffi::CString;
  793. ///
  794. /// let nul_error = CString::new("foo\0bar").unwrap_err();
  795. /// assert_eq!(nul_error.into_vec(), b"foo\0bar");
  796. /// ```
  797. pub fn into_vec(self) -> Vec<u8> {
  798. self.1
  799. }
  800. }
  801. impl fmt::Display for NulError {
  802. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  803. write!(f, "nul byte found in provided data at position: {}", self.0)
  804. }
  805. }
  806. impl fmt::Display for FromBytesWithNulError {
  807. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  808. f.write_str(self.description())?;
  809. if let FromBytesWithNulErrorKind::InteriorNul(pos) = self.kind {
  810. write!(f, " at byte pos {}", pos)?;
  811. }
  812. Ok(())
  813. }
  814. }
  815. impl IntoStringError {
  816. /// Consumes this error, returning original [`CString`] which generated the
  817. /// error.
  818. ///
  819. /// [`CString`]: struct.CString.html
  820. pub fn into_cstring(self) -> CString {
  821. self.inner
  822. }
  823. /// Access the underlying UTF-8 error that was the cause of this error.
  824. pub fn utf8_error(&self) -> Utf8Error {
  825. self.error
  826. }
  827. fn description(&self) -> &str {
  828. "C string contained non-utf8 bytes"
  829. }
  830. }
  831. impl fmt::Display for IntoStringError {
  832. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  833. self.description().fmt(f)
  834. }
  835. }
  836. impl CStr {
  837. /// Wraps a raw C string with a safe C string wrapper.
  838. ///
  839. /// This function will wrap the provided `ptr` with a `CStr` wrapper, which
  840. /// allows inspection and interoperation of non-owned C strings. This method
  841. /// is unsafe for a number of reasons:
  842. ///
  843. /// * There is no guarantee to the validity of `ptr`.
  844. /// * The returned lifetime is not guaranteed to be the actual lifetime of
  845. /// `ptr`.
  846. /// * There is no guarantee that the memory pointed to by `ptr` contains a
  847. /// valid nul terminator byte at the end of the string.
  848. /// * It is not guaranteed that the memory pointed by `ptr` won't change
  849. /// before the `CStr` has been destroyed.
  850. ///
  851. /// > **Note**: This operation is intended to be a 0-cost cast but it is
  852. /// > currently implemented with an up-front calculation of the length of
  853. /// > the string. This is not guaranteed to always be the case.
  854. ///
  855. /// # Examples
  856. ///
  857. /// ```ignore (extern-declaration)
  858. /// # fn main() {
  859. /// use std::ffi::CStr;
  860. /// use std::os::raw::c_char;
  861. ///
  862. /// extern {
  863. /// fn my_string() -> *const c_char;
  864. /// }
  865. ///
  866. /// unsafe {
  867. /// let slice = CStr::from_ptr(my_string());
  868. /// println!("string returned: {}", slice.to_str().unwrap());
  869. /// }
  870. /// # }
  871. /// ```
  872. pub unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr {
  873. let len = strlen(ptr);
  874. let ptr = ptr as *const u8;
  875. CStr::from_bytes_with_nul_unchecked(slice::from_raw_parts(ptr, len as usize + 1))
  876. }
  877. /// Creates a C string wrapper from a byte slice.
  878. ///
  879. /// This function will cast the provided `bytes` to a `CStr`
  880. /// wrapper after ensuring that the byte slice is nul-terminated
  881. /// and does not contain any interior nul bytes.
  882. ///
  883. /// # Examples
  884. ///
  885. /// ```
  886. /// use std::ffi::CStr;
  887. ///
  888. /// let cstr = CStr::from_bytes_with_nul(b"hello\0");
  889. /// assert!(cstr.is_ok());
  890. /// ```
  891. ///
  892. /// Creating a `CStr` without a trailing nul terminator is an error:
  893. ///
  894. /// ```
  895. /// use std::ffi::CStr;
  896. ///
  897. /// let c_str = CStr::from_bytes_with_nul(b"hello");
  898. /// assert!(c_str.is_err());
  899. /// ```
  900. ///
  901. /// Creating a `CStr` with an interior nul byte is an error:
  902. ///
  903. /// ```
  904. /// use std::ffi::CStr;
  905. ///
  906. /// let c_str = CStr::from_bytes_with_nul(b"he\0llo\0");
  907. /// assert!(c_str.is_err());
  908. /// ```
  909. pub fn from_bytes_with_nul(bytes: &[u8]) -> Result<&CStr, FromBytesWithNulError> {
  910. let nul_pos = memchr(0, bytes);
  911. if let Some(nul_pos) = nul_pos {
  912. if nul_pos + 1 != bytes.len() {
  913. return Err(FromBytesWithNulError::interior_nul(nul_pos));
  914. }
  915. Ok(unsafe { CStr::from_bytes_with_nul_unchecked(bytes) })
  916. } else {
  917. Err(FromBytesWithNulError::not_nul_terminated())
  918. }
  919. }
  920. /// Unsafely creates a C string wrapper from a byte slice.
  921. ///
  922. /// This function will cast the provided `bytes` to a `CStr` wrapper without
  923. /// performing any sanity checks. The provided slice **must** be nul-terminated
  924. /// and not contain any interior nul bytes.
  925. ///
  926. /// # Examples
  927. ///
  928. /// ```
  929. /// use std::ffi::{CStr, CString};
  930. ///
  931. /// unsafe {
  932. /// let cstring = CString::new("hello").unwrap();
  933. /// let cstr = CStr::from_bytes_with_nul_unchecked(cstring.to_bytes_with_nul());
  934. /// assert_eq!(cstr, &*cstring);
  935. /// }
  936. /// ```
  937. #[inline]
  938. pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
  939. &*(bytes as *const [u8] as *const CStr)
  940. }
  941. /// Returns the inner pointer to this C string.
  942. ///
  943. /// The returned pointer will be valid for as long as `self` is, and points
  944. /// to a contiguous region of memory terminated with a 0 byte to represent
  945. /// the end of the string.
  946. ///
  947. /// **WARNING**
  948. ///
  949. /// It is your responsibility to make sure that the underlying memory is not
  950. /// freed too early. For example, the following code will cause undefined
  951. /// behavior when `ptr` is used inside the `unsafe` block:
  952. ///
  953. /// ```no_run
  954. /// # #![allow(unused_must_use)]
  955. /// use std::ffi::{CString};
  956. ///
  957. /// let ptr = CString::new("Hello").unwrap().as_ptr();
  958. /// unsafe {
  959. /// // `ptr` is dangling
  960. /// *ptr;
  961. /// }
  962. /// ```
  963. ///
  964. /// This happens because the pointer returned by `as_ptr` does not carry any
  965. /// lifetime information and the [`CString`] is deallocated immediately after
  966. /// the `CString::new("Hello").unwrap().as_ptr()` expression is evaluated.
  967. /// To fix the problem, bind the `CString` to a local variable:
  968. ///
  969. /// ```no_run
  970. /// # #![allow(unused_must_use)]
  971. /// use std::ffi::{CString};
  972. ///
  973. /// let hello = CString::new("Hello").unwrap();
  974. /// let ptr = hello.as_ptr();
  975. /// unsafe {
  976. /// // `ptr` is valid because `hello` is in scope
  977. /// *ptr;
  978. /// }
  979. /// ```
  980. ///
  981. /// This way, the lifetime of the `CString` in `hello` encompasses
  982. /// the lifetime of `ptr` and the `unsafe` block.
  983. ///
  984. /// [`CString`]: struct.CString.html
  985. #[inline]
  986. pub fn as_ptr(&self) -> *const c_char {
  987. self.inner.as_ptr()
  988. }
  989. /// Converts this C string to a byte slice.
  990. ///
  991. /// The returned slice will **not** contain the trailing nul terminator that this C
  992. /// string has.
  993. ///
  994. /// > **Note**: This method is currently implemented as a constant-time
  995. /// > cast, but it is planned to alter its definition in the future to
  996. /// > perform the length calculation whenever this method is called.
  997. ///
  998. /// # Examples
  999. ///
  1000. /// ```
  1001. /// use std::ffi::CStr;
  1002. ///
  1003. /// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap();
  1004. /// assert_eq!(c_str.to_bytes(), b"foo");
  1005. /// ```
  1006. #[inline]
  1007. pub fn to_bytes(&self) -> &[u8] {
  1008. let bytes = self.to_bytes_with_nul();
  1009. &bytes[..bytes.len() - 1]
  1010. }
  1011. /// Converts this C string to a byte slice containing the trailing 0 byte.
  1012. ///
  1013. /// This function is the equivalent of [`to_bytes`] except that it will retain
  1014. /// the trailing nul terminator instead of chopping it off.
  1015. ///
  1016. /// > **Note**: This method is currently implemented as a 0-cost cast, but
  1017. /// > it is planned to alter its definition in the future to perform the
  1018. /// > length calculation whenever this method is called.
  1019. ///
  1020. /// [`to_bytes`]: #method.to_bytes
  1021. ///
  1022. /// # Examples
  1023. ///
  1024. /// ```
  1025. /// use std::ffi::CStr;
  1026. ///
  1027. /// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap();
  1028. /// assert_eq!(c_str.to_bytes_with_nul(), b"foo\0");
  1029. /// ```
  1030. #[inline]
  1031. pub fn to_bytes_with_nul(&self) -> &[u8] {
  1032. unsafe { &*(&self.inner as *const [c_char] as *const [u8]) }
  1033. }
  1034. /// Yields a [`&str`] slice if the `CStr` contains valid UTF-8.
  1035. ///
  1036. /// If the contents of the `CStr` are valid UTF-8 data, this
  1037. /// function will return the corresponding [`&str`] slice. Otherwise,
  1038. /// it will return an error with details of where UTF-8 validation failed.
  1039. ///
  1040. /// > **Note**: This method is currently implemented to check for validity
  1041. /// > after a constant-time cast, but it is planned to alter its definition
  1042. /// > in the future to perform the length calculation in addition to the
  1043. /// > UTF-8 check whenever this method is called.
  1044. ///
  1045. /// [`&str`]: ../primitive.str.html
  1046. ///
  1047. /// # Examples
  1048. ///
  1049. /// ```
  1050. /// use std::ffi::CStr;
  1051. ///
  1052. /// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap();
  1053. /// assert_eq!(c_str.to_str(), Ok("foo"));
  1054. /// ```
  1055. pub fn to_str(&self) -> Result<&str, str::Utf8Error> {
  1056. // NB: When CStr is changed to perform the length check in .to_bytes()
  1057. // instead of in from_ptr(), it may be worth considering if this should
  1058. // be rewritten to do the UTF-8 check inline with the length calculation
  1059. // instead of doing it afterwards.
  1060. str::from_utf8(self.to_bytes())
  1061. }
  1062. /// Converts a `CStr` into a [`Cow`]`<`[`str`]`>`.
  1063. ///
  1064. /// If the contents of the `CStr` are valid UTF-8 data, this
  1065. /// function will return a [`Cow`]`::`[`Borrowed`]`(`[`&str`]`)`
  1066. /// with the the corresponding [`&str`] slice. Otherwise, it will
  1067. /// replace any invalid UTF-8 sequences with
  1068. /// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD] and return a
  1069. /// [`Cow`]`::`[`Owned`]`(`[`String`]`)` with the result.
  1070. ///
  1071. /// > **Note**: This method is currently implemented to check for validity
  1072. /// > after a constant-time cast, but it is planned to alter its definition
  1073. /// > in the future to perform the length calculation in addition to the
  1074. /// > UTF-8 check whenever this method is called.
  1075. ///
  1076. /// [`Cow`]: ../borrow/enum.Cow.html
  1077. /// [`Borrowed`]: ../borrow/enum.Cow.html#variant.Borrowed
  1078. /// [`Owned`]: ../borrow/enum.Cow.html#variant.Owned
  1079. /// [`str`]: ../primitive.str.html
  1080. /// [`String`]: ../string/struct.String.html
  1081. /// [U+FFFD]: ../char/constant.REPLACEMENT_CHARACTER.html
  1082. ///
  1083. /// # Examples
  1084. ///
  1085. /// Calling `to_string_lossy` on a `CStr` containing valid UTF-8:
  1086. ///
  1087. /// ```
  1088. /// use std::borrow::Cow;
  1089. /// use std::ffi::CStr;
  1090. ///
  1091. /// let c_str = CStr::from_bytes_with_nul(b"Hello World\0").unwrap();
  1092. /// assert_eq!(c_str.to_string_lossy(), Cow::Borrowed("Hello World"));
  1093. /// ```
  1094. ///
  1095. /// Calling `to_string_lossy` on a `CStr` containing invalid UTF-8:
  1096. ///
  1097. /// ```
  1098. /// use std::borrow::Cow;
  1099. /// use std::ffi::CStr;
  1100. ///
  1101. /// let c_str = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0").unwrap();
  1102. /// assert_eq!(
  1103. /// c_str.to_string_lossy(),
  1104. /// Cow::Owned(String::from("Hello �World")) as Cow<str>
  1105. /// );
  1106. /// ```
  1107. pub fn to_string_lossy(&self) -> Cow<str> {
  1108. String::from_utf8_lossy(self.to_bytes())
  1109. }
  1110. /// Converts a [`Box`]`<CStr>` into a [`CString`] without copying or allocating.
  1111. ///
  1112. /// [`Box`]: ../boxed/struct.Box.html
  1113. /// [`CString`]: struct.CString.html
  1114. ///
  1115. /// # Examples
  1116. ///
  1117. /// ```
  1118. /// use std::ffi::CString;
  1119. ///
  1120. /// let c_string = CString::new(b"foo".to_vec()).unwrap();
  1121. /// let boxed = c_string.into_boxed_c_str();
  1122. /// assert_eq!(boxed.into_c_string(), CString::new("foo").unwrap());
  1123. /// ```
  1124. pub fn into_c_string(self: Box<CStr>) -> CString {
  1125. let raw = Box::into_raw(self) as *mut [u8];
  1126. CString {
  1127. inner: unsafe { Box::from_raw(raw) },
  1128. }
  1129. }
  1130. }
  1131. impl PartialEq for CStr {
  1132. fn eq(&self, other: &CStr) -> bool {
  1133. self.to_bytes().eq(other.to_bytes())
  1134. }
  1135. }
  1136. impl Eq for CStr {}
  1137. impl PartialOrd for CStr {
  1138. fn partial_cmp(&self, other: &CStr) -> Option<Ordering> {
  1139. self.to_bytes().partial_cmp(&other.to_bytes())
  1140. }
  1141. }
  1142. impl Ord for CStr {
  1143. fn cmp(&self, other: &CStr) -> Ordering {
  1144. self.to_bytes().cmp(&other.to_bytes())
  1145. }
  1146. }
  1147. impl ToOwned for CStr {
  1148. type Owned = CString;
  1149. fn to_owned(&self) -> CString {
  1150. CString {
  1151. inner: self.to_bytes_with_nul().into(),
  1152. }
  1153. }
  1154. }
  1155. impl<'a> From<&'a CStr> for CString {
  1156. fn from(s: &'a CStr) -> CString {
  1157. s.to_owned()
  1158. }
  1159. }
  1160. impl ops::Index<ops::RangeFull> for CString {
  1161. type Output = CStr;
  1162. #[inline]
  1163. fn index(&self, _index: ops::RangeFull) -> &CStr {
  1164. self
  1165. }
  1166. }
  1167. impl AsRef<CStr> for CStr {
  1168. #[inline]
  1169. fn as_ref(&self) -> &CStr {
  1170. self
  1171. }
  1172. }
  1173. impl AsRef<CStr> for CString {
  1174. #[inline]
  1175. fn as_ref(&self) -> &CStr {
  1176. self
  1177. }
  1178. }