4 use crate::borrow
::{Cow, ToOwned}
;
7 use crate::slice
::hack
::into_vec
;
8 use crate::string
::String
;
10 use core
::borrow
::Borrow
;
11 use core
::ffi
::{c_char, CStr}
;
14 use core
::num
::NonZeroU8
;
18 use core
::slice
::memchr
;
19 use core
::str::{self, Utf8Error}
;
21 #[cfg(target_has_atomic = "ptr")]
24 /// A type representing an owned, C-compatible, nul-terminated string with no nul bytes in the
27 /// This type serves the purpose of being able to safely generate a
28 /// C-compatible string from a Rust byte slice or vector. An instance of this
29 /// type is a static guarantee that the underlying bytes contain no interior 0
30 /// bytes ("nul characters") and that the final byte is 0 ("nul terminator").
32 /// `CString` is to <code>&[CStr]</code> as [`String`] is to <code>&[str]</code>: the former
33 /// in each pair are owned strings; the latter are borrowed
36 /// # Creating a `CString`
38 /// A `CString` is created from either a byte slice or a byte vector,
39 /// or anything that implements <code>[Into]<[Vec]<[u8]>></code> (for
40 /// example, you can build a `CString` straight out of a [`String`] or
41 /// a <code>&[str]</code>, since both implement that trait).
43 /// The [`CString::new`] method will actually check that the provided <code>&[[u8]]</code>
44 /// does not have 0 bytes in the middle, and return an error if it
47 /// # Extracting a raw pointer to the whole C string
49 /// `CString` implements an [`as_ptr`][`CStr::as_ptr`] method through the [`Deref`]
50 /// trait. This method will give you a `*const c_char` which you can
51 /// feed directly to extern functions that expect a nul-terminated
52 /// string, like C's `strdup()`. Notice that [`as_ptr`][`CStr::as_ptr`] returns a
53 /// read-only pointer; if the C code writes to it, that causes
54 /// undefined behavior.
56 /// # Extracting a slice of the whole C string
58 /// Alternatively, you can obtain a <code>&[[u8]]</code> slice from a
59 /// `CString` with the [`CString::as_bytes`] method. Slices produced in this
60 /// way do *not* contain the trailing nul terminator. This is useful
61 /// when you will be calling an extern function that takes a `*const
62 /// u8` argument which is not necessarily nul-terminated, plus another
63 /// argument with the length of the string — like C's `strndup()`.
64 /// You can of course get the slice's length with its
65 /// [`len`][slice::len] method.
67 /// If you need a <code>&[[u8]]</code> slice *with* the nul terminator, you
68 /// can use [`CString::as_bytes_with_nul`] instead.
70 /// Once you have the kind of slice you need (with or without a nul
71 /// terminator), you can call the slice's own
72 /// [`as_ptr`][slice::as_ptr] method to get a read-only raw pointer to pass to
73 /// extern functions. See the documentation for that function for a
74 /// discussion on ensuring the lifetime of the raw pointer.
76 /// [str]: prim@str "str"
77 /// [`Deref`]: ops::Deref
81 /// ```ignore (extern-declaration)
83 /// use std::ffi::CString;
84 /// use std::os::raw::c_char;
87 /// fn my_printer(s: *const c_char);
90 /// // We are certain that our string doesn't have 0 bytes in the middle,
91 /// // so we can .expect()
92 /// let c_to_print = CString::new("Hello, world!").expect("CString::new failed");
94 /// my_printer(c_to_print.as_ptr());
101 /// `CString` is intended for working with traditional C-style strings
102 /// (a sequence of non-nul bytes terminated by a single nul byte); the
103 /// primary use case for these kinds of strings is interoperating with C-like
104 /// code. Often you will need to transfer ownership to/from that external
105 /// code. It is strongly recommended that you thoroughly read through the
106 /// documentation of `CString` before use, as improper ownership management
107 /// of `CString` instances can lead to invalid memory accesses, memory leaks,
108 /// and other memory errors.
109 #[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Clone)]
110 #[cfg_attr(not(test), rustc_diagnostic_item = "cstring_type")]
111 #[unstable(feature = "alloc_c_string", issue = "94079")]
113 // Invariant 1: the slice ends with a zero byte and has a length of at least one.
114 // Invariant 2: the slice contains only one zero byte.
115 // Improper usage of unsafe function can break Invariant 2, but not Invariant 1.
119 /// An error indicating that an interior nul byte was found.
121 /// While Rust strings may contain nul bytes in the middle, C strings
122 /// can't, as that byte would effectively truncate the string.
124 /// This error is created by the [`new`][`CString::new`] method on
125 /// [`CString`]. See its documentation for more.
130 /// use std::ffi::{CString, NulError};
132 /// let _: NulError = CString::new(b"f\0oo".to_vec()).unwrap_err();
134 #[derive(Clone, PartialEq, Eq, Debug)]
135 #[unstable(feature = "alloc_c_string", issue = "94079")]
136 pub struct NulError(usize, Vec
<u8>);
138 #[derive(Clone, PartialEq, Eq, Debug)]
139 enum FromBytesWithNulErrorKind
{
144 /// An error indicating that a nul byte was not in the expected position.
146 /// The vector used to create a [`CString`] must have one and only one nul byte,
147 /// positioned at the end.
149 /// This error is created by the [`CString::from_vec_with_nul`] method.
150 /// See its documentation for more.
155 /// use std::ffi::{CString, FromVecWithNulError};
157 /// let _: FromVecWithNulError = CString::from_vec_with_nul(b"f\0oo".to_vec()).unwrap_err();
159 #[derive(Clone, PartialEq, Eq, Debug)]
160 #[unstable(feature = "alloc_c_string", issue = "94079")]
161 pub struct FromVecWithNulError
{
162 error_kind
: FromBytesWithNulErrorKind
,
166 #[stable(feature = "cstring_from_vec_with_nul", since = "1.58.0")]
167 impl FromVecWithNulError
{
168 /// Returns a slice of [`u8`]s bytes that were attempted to convert to a [`CString`].
175 /// use std::ffi::CString;
177 /// // Some invalid bytes in a vector
178 /// let bytes = b"f\0oo".to_vec();
180 /// let value = CString::from_vec_with_nul(bytes.clone());
182 /// assert_eq!(&bytes[..], value.unwrap_err().as_bytes());
185 #[stable(feature = "cstring_from_vec_with_nul", since = "1.58.0")]
186 pub fn as_bytes(&self) -> &[u8] {
190 /// Returns the bytes that were attempted to convert to a [`CString`].
192 /// This method is carefully constructed to avoid allocation. It will
193 /// consume the error, moving out the bytes, so that a copy of the bytes
194 /// does not need to be made.
201 /// use std::ffi::CString;
203 /// // Some invalid bytes in a vector
204 /// let bytes = b"f\0oo".to_vec();
206 /// let value = CString::from_vec_with_nul(bytes.clone());
208 /// assert_eq!(bytes, value.unwrap_err().into_bytes());
210 #[must_use = "`self` will be dropped if the result is not used"]
211 #[stable(feature = "cstring_from_vec_with_nul", since = "1.58.0")]
212 pub fn into_bytes(self) -> Vec
<u8> {
217 /// An error indicating invalid UTF-8 when converting a [`CString`] into a [`String`].
219 /// `CString` is just a wrapper over a buffer of bytes with a nul terminator;
220 /// [`CString::into_string`] performs UTF-8 validation on those bytes and may
221 /// return this error.
223 /// This `struct` is created by [`CString::into_string()`]. See
224 /// its documentation for more.
225 #[derive(Clone, PartialEq, Eq, Debug)]
226 #[unstable(feature = "alloc_c_string", issue = "94079")]
227 pub struct IntoStringError
{
233 /// Creates a new C-compatible string from a container of bytes.
235 /// This function will consume the provided data and use the
236 /// underlying bytes to construct a new string, ensuring that
237 /// there is a trailing 0 byte. This trailing 0 byte will be
238 /// appended by this function; the provided data should *not*
239 /// contain any 0 bytes in it.
243 /// ```ignore (extern-declaration)
244 /// use std::ffi::CString;
245 /// use std::os::raw::c_char;
247 /// extern "C" { fn puts(s: *const c_char); }
249 /// let to_print = CString::new("Hello!").expect("CString::new failed");
251 /// puts(to_print.as_ptr());
257 /// This function will return an error if the supplied bytes contain an
258 /// internal 0 byte. The [`NulError`] returned will contain the bytes as well as
259 /// the position of the nul byte.
260 #[stable(feature = "rust1", since = "1.0.0")]
261 pub fn new
<T
: Into
<Vec
<u8>>>(t
: T
) -> Result
<CString
, NulError
> {
263 fn spec_new_impl(self) -> Result
<CString
, NulError
>;
266 impl<T
: Into
<Vec
<u8>>> SpecNewImpl
for T
{
267 default fn spec_new_impl(self) -> Result
<CString
, NulError
> {
268 let bytes
: Vec
<u8> = self.into();
269 match memchr
::memchr(0, &bytes
) {
270 Some(i
) => Err(NulError(i
, bytes
)),
271 None
=> Ok(unsafe { CString::_from_vec_unchecked(bytes) }
),
276 // Specialization for avoiding reallocation
277 #[inline(always)] // Without that it is not inlined into specializations
278 fn spec_new_impl_bytes(bytes
: &[u8]) -> Result
<CString
, NulError
> {
279 // We cannot have such large slice that we would overflow here
280 // but using `checked_add` allows LLVM to assume that capacity never overflows
281 // and generate twice shorter code.
282 // `saturating_add` doesn't help for some reason.
283 let capacity
= bytes
.len().checked_add(1).unwrap();
285 // Allocate before validation to avoid duplication of allocation code.
286 // We still need to allocate and copy memory even if we get an error.
287 let mut buffer
= Vec
::with_capacity(capacity
);
288 buffer
.extend(bytes
);
290 // Check memory of self instead of new buffer.
291 // This allows better optimizations if lto enabled.
292 match memchr
::memchr(0, bytes
) {
293 Some(i
) => Err(NulError(i
, buffer
)),
294 None
=> Ok(unsafe { CString::_from_vec_unchecked(buffer) }
),
298 impl SpecNewImpl
for &'_
[u8] {
299 fn spec_new_impl(self) -> Result
<CString
, NulError
> {
300 spec_new_impl_bytes(self)
304 impl SpecNewImpl
for &'_
str {
305 fn spec_new_impl(self) -> Result
<CString
, NulError
> {
306 spec_new_impl_bytes(self.as_bytes())
310 impl SpecNewImpl
for &'_
mut [u8] {
311 fn spec_new_impl(self) -> Result
<CString
, NulError
> {
312 spec_new_impl_bytes(self)
319 /// Creates a C-compatible string by consuming a byte vector,
320 /// without checking for interior 0 bytes.
322 /// Trailing 0 byte will be appended by this function.
324 /// This method is equivalent to [`CString::new`] except that no runtime
325 /// assertion is made that `v` contains no 0 bytes, and it requires an
326 /// actual byte vector, not anything that can be converted to one with Into.
331 /// use std::ffi::CString;
333 /// let raw = b"foo".to_vec();
335 /// let c_string = CString::from_vec_unchecked(raw);
339 #[stable(feature = "rust1", since = "1.0.0")]
340 pub unsafe fn from_vec_unchecked(v
: Vec
<u8>) -> Self {
341 debug_assert
!(memchr
::memchr(0, &v
).is_none());
342 unsafe { Self::_from_vec_unchecked(v) }
345 unsafe fn _from_vec_unchecked(mut v
: Vec
<u8>) -> Self {
348 Self { inner: v.into_boxed_slice() }
351 /// Retakes ownership of a `CString` that was transferred to C via
352 /// [`CString::into_raw`].
354 /// Additionally, the length of the string will be recalculated from the pointer.
358 /// This should only ever be called with a pointer that was earlier
359 /// obtained by calling [`CString::into_raw`]. Other usage (e.g., trying to take
360 /// ownership of a string that was allocated by foreign code) is likely to lead
361 /// to undefined behavior or allocator corruption.
363 /// It should be noted that the length isn't just "recomputed," but that
364 /// the recomputed length must match the original length from the
365 /// [`CString::into_raw`] call. This means the [`CString::into_raw`]/`from_raw`
366 /// methods should not be used when passing the string to C functions that can
367 /// modify the string's length.
369 /// > **Note:** If you need to borrow a string that was allocated by
370 /// > foreign code, use [`CStr`]. If you need to take ownership of
371 /// > a string that was allocated by foreign code, you will need to
372 /// > make your own provisions for freeing it appropriately, likely
373 /// > with the foreign code's API to do that.
377 /// Creates a `CString`, pass ownership to an `extern` function (via raw pointer), then retake
378 /// ownership with `from_raw`:
380 /// ```ignore (extern-declaration)
381 /// use std::ffi::CString;
382 /// use std::os::raw::c_char;
385 /// fn some_extern_function(s: *mut c_char);
388 /// let c_string = CString::new("Hello!").expect("CString::new failed");
389 /// let raw = c_string.into_raw();
391 /// some_extern_function(raw);
392 /// let c_string = CString::from_raw(raw);
395 #[must_use = "call `drop(from_raw(ptr))` if you intend to drop the `CString`"]
396 #[stable(feature = "cstr_memory", since = "1.4.0")]
397 pub unsafe fn from_raw(ptr
: *mut c_char
) -> CString
{
398 // SAFETY: This is called with a pointer that was obtained from a call
399 // to `CString::into_raw` and the length has not been modified. As such,
400 // we know there is a NUL byte (and only one) at the end and that the
401 // information about the size of the allocation is correct on Rust's
405 /// Provided by libc or compiler_builtins.
406 fn strlen(s
: *const c_char
) -> usize;
408 let len
= strlen(ptr
) + 1; // Including the NUL byte
409 let slice
= slice
::from_raw_parts_mut(ptr
, len
as usize);
410 CString { inner: Box::from_raw(slice as *mut [c_char] as *mut [u8]) }
414 /// Consumes the `CString` and transfers ownership of the string to a C caller.
416 /// The pointer which this function returns must be returned to Rust and reconstituted using
417 /// [`CString::from_raw`] to be properly deallocated. Specifically, one
418 /// should *not* use the standard C `free()` function to deallocate
421 /// Failure to call [`CString::from_raw`] will lead to a memory leak.
423 /// The C side must **not** modify the length of the string (by writing a
424 /// `null` somewhere inside the string or removing the final one) before
425 /// it makes it back into Rust using [`CString::from_raw`]. See the safety section
426 /// in [`CString::from_raw`].
431 /// use std::ffi::CString;
433 /// let c_string = CString::new("foo").expect("CString::new failed");
435 /// let ptr = c_string.into_raw();
438 /// assert_eq!(b'f', *ptr as u8);
439 /// assert_eq!(b'o', *ptr.offset(1) as u8);
440 /// assert_eq!(b'o', *ptr.offset(2) as u8);
441 /// assert_eq!(b'\0', *ptr.offset(3) as u8);
443 /// // retake pointer to free memory
444 /// let _ = CString::from_raw(ptr);
448 #[must_use = "`self` will be dropped if the result is not used"]
449 #[stable(feature = "cstr_memory", since = "1.4.0")]
450 pub fn into_raw(self) -> *mut c_char
{
451 Box
::into_raw(self.into_inner()) as *mut c_char
454 /// Converts the `CString` into a [`String`] if it contains valid UTF-8 data.
456 /// On failure, ownership of the original `CString` is returned.
461 /// use std::ffi::CString;
463 /// let valid_utf8 = vec![b'f', b'o', b'o'];
464 /// let cstring = CString::new(valid_utf8).expect("CString::new failed");
465 /// assert_eq!(cstring.into_string().expect("into_string() call failed"), "foo");
467 /// let invalid_utf8 = vec![b'f', 0xff, b'o', b'o'];
468 /// let cstring = CString::new(invalid_utf8).expect("CString::new failed");
469 /// let err = cstring.into_string().err().expect("into_string().err() failed");
470 /// assert_eq!(err.utf8_error().valid_up_to(), 1);
472 #[stable(feature = "cstring_into", since = "1.7.0")]
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 { Self::_from_vec_unchecked(e.into_bytes()) }
,
480 /// Consumes the `CString` and returns the underlying byte buffer.
482 /// The returned buffer does **not** contain the trailing nul
483 /// terminator, and it is guaranteed to not have any interior nul
489 /// use std::ffi::CString;
491 /// let c_string = CString::new("foo").expect("CString::new failed");
492 /// let bytes = c_string.into_bytes();
493 /// assert_eq!(bytes, vec![b'f', b'o', b'o']);
495 #[must_use = "`self` will be dropped if the result is not used"]
496 #[stable(feature = "cstring_into", since = "1.7.0")]
497 pub fn into_bytes(self) -> Vec
<u8> {
498 let mut vec
= into_vec(self.into_inner());
499 let _nul
= vec
.pop();
500 debug_assert_eq
!(_nul
, Some(0u8));
504 /// Equivalent to [`CString::into_bytes()`] except that the
505 /// returned vector includes the trailing nul terminator.
510 /// use std::ffi::CString;
512 /// let c_string = CString::new("foo").expect("CString::new failed");
513 /// let bytes = c_string.into_bytes_with_nul();
514 /// assert_eq!(bytes, vec![b'f', b'o', b'o', b'\0']);
516 #[must_use = "`self` will be dropped if the result is not used"]
517 #[stable(feature = "cstring_into", since = "1.7.0")]
518 pub fn into_bytes_with_nul(self) -> Vec
<u8> {
519 into_vec(self.into_inner())
522 /// Returns the contents of this `CString` as a slice of bytes.
524 /// The returned slice does **not** contain the trailing nul
525 /// terminator, and it is guaranteed to not have any interior nul
526 /// bytes. If you need the nul terminator, use
527 /// [`CString::as_bytes_with_nul`] instead.
532 /// use std::ffi::CString;
534 /// let c_string = CString::new("foo").expect("CString::new failed");
535 /// let bytes = c_string.as_bytes();
536 /// assert_eq!(bytes, &[b'f', b'o', b'o']);
540 #[stable(feature = "rust1", since = "1.0.0")]
541 pub fn as_bytes(&self) -> &[u8] {
542 // SAFETY: CString has a length at least 1
543 unsafe { self.inner.get_unchecked(..self.inner.len() - 1) }
546 /// Equivalent to [`CString::as_bytes()`] except that the
547 /// returned slice includes the trailing nul terminator.
552 /// use std::ffi::CString;
554 /// let c_string = CString::new("foo").expect("CString::new failed");
555 /// let bytes = c_string.as_bytes_with_nul();
556 /// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']);
560 #[stable(feature = "rust1", since = "1.0.0")]
561 pub fn as_bytes_with_nul(&self) -> &[u8] {
565 /// Extracts a [`CStr`] slice containing the entire string.
570 /// use std::ffi::{CString, CStr};
572 /// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
573 /// let cstr = c_string.as_c_str();
575 /// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
579 #[stable(feature = "as_c_str", since = "1.20.0")]
580 pub fn as_c_str(&self) -> &CStr
{
584 /// Converts this `CString` into a boxed [`CStr`].
589 /// use std::ffi::{CString, CStr};
591 /// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
592 /// let boxed = c_string.into_boxed_c_str();
593 /// assert_eq!(&*boxed,
594 /// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
596 #[must_use = "`self` will be dropped if the result is not used"]
597 #[stable(feature = "into_boxed_c_str", since = "1.20.0")]
598 pub fn into_boxed_c_str(self) -> Box
<CStr
> {
599 unsafe { Box::from_raw(Box::into_raw(self.into_inner()) as *mut CStr) }
602 /// Bypass "move out of struct which implements [`Drop`] trait" restriction.
604 fn into_inner(self) -> Box
<[u8]> {
605 // Rationale: `mem::forget(self)` invalidates the previous call to `ptr::read(&self.inner)`
606 // so we use `ManuallyDrop` to ensure `self` is not dropped.
607 // Then we can return the box directly without invalidating it.
608 // See https://github.com/rust-lang/rust/issues/62553.
609 let this
= mem
::ManuallyDrop
::new(self);
610 unsafe { ptr::read(&this.inner) }
613 /// Converts a <code>[Vec]<[u8]></code> to a [`CString`] without checking the
614 /// invariants on the given [`Vec`].
618 /// The given [`Vec`] **must** have one nul byte as its last element.
619 /// This means it cannot be empty nor have any other nul byte anywhere else.
624 /// use std::ffi::CString;
626 /// unsafe { CString::from_vec_with_nul_unchecked(b"abc\0".to_vec()) },
627 /// unsafe { CString::from_vec_unchecked(b"abc".to_vec()) }
631 #[stable(feature = "cstring_from_vec_with_nul", since = "1.58.0")]
632 pub unsafe fn from_vec_with_nul_unchecked(v
: Vec
<u8>) -> Self {
633 debug_assert
!(memchr
::memchr(0, &v
).unwrap() + 1 == v
.len());
634 unsafe { Self::_from_vec_with_nul_unchecked(v) }
637 unsafe fn _from_vec_with_nul_unchecked(v
: Vec
<u8>) -> Self {
638 Self { inner: v.into_boxed_slice() }
641 /// Attempts to converts a <code>[Vec]<[u8]></code> to a [`CString`].
643 /// Runtime checks are present to ensure there is only one nul byte in the
644 /// [`Vec`], its last element.
648 /// If a nul byte is present and not the last element or no nul bytes
649 /// is present, an error will be returned.
653 /// A successful conversion will produce the same result as [`CString::new`]
654 /// when called without the ending nul byte.
657 /// use std::ffi::CString;
659 /// CString::from_vec_with_nul(b"abc\0".to_vec())
660 /// .expect("CString::from_vec_with_nul failed"),
661 /// CString::new(b"abc".to_vec()).expect("CString::new failed")
665 /// An incorrectly formatted [`Vec`] will produce an error.
668 /// use std::ffi::{CString, FromVecWithNulError};
669 /// // Interior nul byte
670 /// let _: FromVecWithNulError = CString::from_vec_with_nul(b"a\0bc".to_vec()).unwrap_err();
672 /// let _: FromVecWithNulError = CString::from_vec_with_nul(b"abc".to_vec()).unwrap_err();
674 #[stable(feature = "cstring_from_vec_with_nul", since = "1.58.0")]
675 pub fn from_vec_with_nul(v
: Vec
<u8>) -> Result
<Self, FromVecWithNulError
> {
676 let nul_pos
= memchr
::memchr(0, &v
);
678 Some(nul_pos
) if nul_pos
+ 1 == v
.len() => {
679 // SAFETY: We know there is only one nul byte, at the end
681 Ok(unsafe { Self::_from_vec_with_nul_unchecked(v) }
)
683 Some(nul_pos
) => Err(FromVecWithNulError
{
684 error_kind
: FromBytesWithNulErrorKind
::InteriorNul(nul_pos
),
687 None
=> Err(FromVecWithNulError
{
688 error_kind
: FromBytesWithNulErrorKind
::NotNulTerminated
,
695 // Turns this `CString` into an empty string to prevent
696 // memory-unsafe code from working by accident. Inline
697 // to prevent LLVM from optimizing it away in debug builds.
698 #[stable(feature = "cstring_drop", since = "1.13.0")]
699 impl Drop
for CString
{
703 *self.inner
.get_unchecked_mut(0) = 0;
708 #[stable(feature = "rust1", since = "1.0.0")]
709 impl ops
::Deref
for CString
{
713 fn deref(&self) -> &CStr
{
714 unsafe { CStr::from_bytes_with_nul_unchecked(self.as_bytes_with_nul()) }
718 #[stable(feature = "rust1", since = "1.0.0")]
719 impl fmt
::Debug
for CString
{
720 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
721 fmt
::Debug
::fmt(&**self, f
)
725 #[stable(feature = "cstring_into", since = "1.7.0")]
726 impl From
<CString
> for Vec
<u8> {
727 /// Converts a [`CString`] into a <code>[Vec]<[u8]></code>.
729 /// The conversion consumes the [`CString`], and removes the terminating NUL byte.
731 fn from(s
: CString
) -> Vec
<u8> {
736 #[stable(feature = "cstr_default", since = "1.10.0")]
737 impl Default
for CString
{
738 /// Creates an empty `CString`.
739 fn default() -> CString
{
740 let a
: &CStr
= Default
::default();
745 #[stable(feature = "cstr_borrow", since = "1.3.0")]
746 impl Borrow
<CStr
> for CString
{
748 fn borrow(&self) -> &CStr
{
753 #[stable(feature = "cstring_from_cow_cstr", since = "1.28.0")]
754 impl<'a
> From
<Cow
<'a
, CStr
>> for CString
{
755 /// Converts a `Cow<'a, CStr>` into a `CString`, by copying the contents if they are
758 fn from(s
: Cow
<'a
, CStr
>) -> Self {
764 #[stable(feature = "box_from_c_str", since = "1.17.0")]
765 impl From
<&CStr
> for Box
<CStr
> {
766 /// Converts a `&CStr` into a `Box<CStr>`,
767 /// by copying the contents into a newly allocated [`Box`].
768 fn from(s
: &CStr
) -> Box
<CStr
> {
769 let boxed
: Box
<[u8]> = Box
::from(s
.to_bytes_with_nul());
770 unsafe { Box::from_raw(Box::into_raw(boxed) as *mut CStr) }
774 #[stable(feature = "box_from_cow", since = "1.45.0")]
775 impl From
<Cow
<'_
, CStr
>> for Box
<CStr
> {
776 /// Converts a `Cow<'a, CStr>` into a `Box<CStr>`,
777 /// by copying the contents if they are borrowed.
779 fn from(cow
: Cow
<'_
, CStr
>) -> Box
<CStr
> {
781 Cow
::Borrowed(s
) => Box
::from(s
),
782 Cow
::Owned(s
) => Box
::from(s
),
787 #[stable(feature = "c_string_from_box", since = "1.18.0")]
788 impl From
<Box
<CStr
>> for CString
{
789 /// Converts a <code>[Box]<[CStr]></code> into a [`CString`] without copying or allocating.
791 fn from(s
: Box
<CStr
>) -> CString
{
792 let raw
= Box
::into_raw(s
) as *mut [u8];
793 CString { inner: unsafe { Box::from_raw(raw) }
}
797 #[stable(feature = "cstring_from_vec_of_nonzerou8", since = "1.43.0")]
798 impl From
<Vec
<NonZeroU8
>> for CString
{
799 /// Converts a <code>[Vec]<[NonZeroU8]></code> into a [`CString`] without
800 /// copying nor checking for inner null bytes.
802 fn from(v
: Vec
<NonZeroU8
>) -> CString
{
804 // Transmute `Vec<NonZeroU8>` to `Vec<u8>`.
807 // - transmuting between `NonZeroU8` and `u8` is sound;
808 // - `alloc::Layout<NonZeroU8> == alloc::Layout<u8>`.
809 let (ptr
, len
, cap
): (*mut NonZeroU8
, _
, _
) = Vec
::into_raw_parts(v
);
810 Vec
::from_raw_parts(ptr
.cast
::<u8>(), len
, cap
)
812 // SAFETY: `v` cannot contain null bytes, given the type-level
813 // invariant of `NonZeroU8`.
814 Self::_from_vec_unchecked(v
)
820 #[stable(feature = "more_box_slice_clone", since = "1.29.0")]
821 impl Clone
for Box
<CStr
> {
823 fn clone(&self) -> Self {
828 #[stable(feature = "box_from_c_string", since = "1.20.0")]
829 impl From
<CString
> for Box
<CStr
> {
830 /// Converts a [`CString`] into a <code>[Box]<[CStr]></code> without copying or allocating.
832 fn from(s
: CString
) -> Box
<CStr
> {
837 #[stable(feature = "cow_from_cstr", since = "1.28.0")]
838 impl<'a
> From
<CString
> for Cow
<'a
, CStr
> {
839 /// Converts a [`CString`] into an owned [`Cow`] without copying or allocating.
841 fn from(s
: CString
) -> Cow
<'a
, CStr
> {
846 #[stable(feature = "cow_from_cstr", since = "1.28.0")]
847 impl<'a
> From
<&'a CStr
> for Cow
<'a
, CStr
> {
848 /// Converts a [`CStr`] into a borrowed [`Cow`] without copying or allocating.
850 fn from(s
: &'a CStr
) -> Cow
<'a
, CStr
> {
855 #[stable(feature = "cow_from_cstr", since = "1.28.0")]
856 impl<'a
> From
<&'a CString
> for Cow
<'a
, CStr
> {
857 /// Converts a `&`[`CString`] into a borrowed [`Cow`] without copying or allocating.
859 fn from(s
: &'a CString
) -> Cow
<'a
, CStr
> {
860 Cow
::Borrowed(s
.as_c_str())
864 #[cfg(target_has_atomic = "ptr")]
865 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
866 impl From
<CString
> for Arc
<CStr
> {
867 /// Converts a [`CString`] into an <code>[Arc]<[CStr]></code> by moving the [`CString`]
868 /// data into a new [`Arc`] buffer.
870 fn from(s
: CString
) -> Arc
<CStr
> {
871 let arc
: Arc
<[u8]> = Arc
::from(s
.into_inner());
872 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const CStr) }
876 #[cfg(target_has_atomic = "ptr")]
877 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
878 impl From
<&CStr
> for Arc
<CStr
> {
879 /// Converts a `&CStr` into a `Arc<CStr>`,
880 /// by copying the contents into a newly allocated [`Arc`].
882 fn from(s
: &CStr
) -> Arc
<CStr
> {
883 let arc
: Arc
<[u8]> = Arc
::from(s
.to_bytes_with_nul());
884 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const CStr) }
888 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
889 impl From
<CString
> for Rc
<CStr
> {
890 /// Converts a [`CString`] into an <code>[Rc]<[CStr]></code> by moving the [`CString`]
891 /// data into a new [`Arc`] buffer.
893 fn from(s
: CString
) -> Rc
<CStr
> {
894 let rc
: Rc
<[u8]> = Rc
::from(s
.into_inner());
895 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const CStr) }
899 #[stable(feature = "shared_from_slice2", since = "1.24.0")]
900 impl From
<&CStr
> for Rc
<CStr
> {
901 /// Converts a `&CStr` into a `Rc<CStr>`,
902 /// by copying the contents into a newly allocated [`Rc`].
904 fn from(s
: &CStr
) -> Rc
<CStr
> {
905 let rc
: Rc
<[u8]> = Rc
::from(s
.to_bytes_with_nul());
906 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const CStr) }
911 #[stable(feature = "default_box_extra", since = "1.17.0")]
912 impl Default
for Box
<CStr
> {
913 fn default() -> Box
<CStr
> {
914 let boxed
: Box
<[u8]> = Box
::from([0]);
915 unsafe { Box::from_raw(Box::into_raw(boxed) as *mut CStr) }
920 /// Returns the position of the nul byte in the slice that caused
921 /// [`CString::new`] to fail.
926 /// use std::ffi::CString;
928 /// let nul_error = CString::new("foo\0bar").unwrap_err();
929 /// assert_eq!(nul_error.nul_position(), 3);
931 /// let nul_error = CString::new("foo bar\0").unwrap_err();
932 /// assert_eq!(nul_error.nul_position(), 7);
935 #[stable(feature = "rust1", since = "1.0.0")]
936 pub fn nul_position(&self) -> usize {
940 /// Consumes this error, returning the underlying vector of bytes which
941 /// generated the error in the first place.
946 /// use std::ffi::CString;
948 /// let nul_error = CString::new("foo\0bar").unwrap_err();
949 /// assert_eq!(nul_error.into_vec(), b"foo\0bar");
951 #[must_use = "`self` will be dropped if the result is not used"]
952 #[stable(feature = "rust1", since = "1.0.0")]
953 pub fn into_vec(self) -> Vec
<u8> {
958 #[stable(feature = "rust1", since = "1.0.0")]
959 impl fmt
::Display
for NulError
{
960 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
961 write
!(f
, "nul byte found in provided data at position: {}", self.0)
965 #[stable(feature = "cstring_from_vec_with_nul", since = "1.58.0")]
966 impl fmt
::Display
for FromVecWithNulError
{
967 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
968 match self.error_kind
{
969 FromBytesWithNulErrorKind
::InteriorNul(pos
) => {
970 write
!(f
, "data provided contains an interior nul byte at pos {pos}")
972 FromBytesWithNulErrorKind
::NotNulTerminated
=> {
973 write
!(f
, "data provided is not nul terminated")
979 impl IntoStringError
{
980 /// Consumes this error, returning original [`CString`] which generated the
982 #[must_use = "`self` will be dropped if the result is not used"]
983 #[stable(feature = "cstring_into", since = "1.7.0")]
984 pub fn into_cstring(self) -> CString
{
988 /// Access the underlying UTF-8 error that was the cause of this error.
990 #[stable(feature = "cstring_into", since = "1.7.0")]
991 pub fn utf8_error(&self) -> Utf8Error
{
996 #[unstable(feature = "cstr_internals", issue = "none")]
997 pub fn __source(&self) -> &Utf8Error
{
1002 impl IntoStringError
{
1003 fn description(&self) -> &str {
1004 "C string contained non-utf8 bytes"
1008 #[stable(feature = "cstring_into", since = "1.7.0")]
1009 impl fmt
::Display
for IntoStringError
{
1010 #[allow(deprecated, deprecated_in_future)]
1011 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
1012 self.description().fmt(f
)
1016 #[stable(feature = "cstr_borrow", since = "1.3.0")]
1017 impl ToOwned
for CStr
{
1018 type Owned
= CString
;
1020 fn to_owned(&self) -> CString
{
1021 CString { inner: self.to_bytes_with_nul().into() }
1024 fn clone_into(&self, target
: &mut CString
) {
1025 let mut b
= into_vec(mem
::take(&mut target
.inner
));
1026 self.to_bytes_with_nul().clone_into(&mut b
);
1027 target
.inner
= b
.into_boxed_slice();
1031 #[stable(feature = "cstring_asref", since = "1.7.0")]
1032 impl From
<&CStr
> for CString
{
1033 fn from(s
: &CStr
) -> CString
{
1038 #[stable(feature = "cstring_asref", since = "1.7.0")]
1039 impl ops
::Index
<ops
::RangeFull
> for CString
{
1043 fn index(&self, _index
: ops
::RangeFull
) -> &CStr
{
1048 #[stable(feature = "cstring_asref", since = "1.7.0")]
1049 impl AsRef
<CStr
> for CString
{
1051 fn as_ref(&self) -> &CStr
{
1058 #[unstable(feature = "cstr_internals", issue = "none")]
1060 /// Converts a `CStr` into a <code>[Cow]<[str]></code>.
1062 /// If the contents of the `CStr` are valid UTF-8 data, this
1063 /// function will return a <code>[Cow]::[Borrowed]\(&[str])</code>
1064 /// with the corresponding <code>&[str]</code> slice. Otherwise, it will
1065 /// replace any invalid UTF-8 sequences with
1066 /// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD] and return a
1067 /// <code>[Cow]::[Owned]\(&[str])</code> with the result.
1069 /// [str]: prim@str "str"
1070 /// [Borrowed]: Cow::Borrowed
1071 /// [Owned]: Cow::Owned
1072 /// [U+FFFD]: crate::char::REPLACEMENT_CHARACTER "std::char::REPLACEMENT_CHARACTER"
1076 /// Calling `to_string_lossy` on a `CStr` containing valid UTF-8:
1079 /// use std::borrow::Cow;
1080 /// use std::ffi::CStr;
1082 /// let cstr = CStr::from_bytes_with_nul(b"Hello World\0")
1083 /// .expect("CStr::from_bytes_with_nul failed");
1084 /// assert_eq!(cstr.to_string_lossy(), Cow::Borrowed("Hello World"));
1087 /// Calling `to_string_lossy` on a `CStr` containing invalid UTF-8:
1090 /// use std::borrow::Cow;
1091 /// use std::ffi::CStr;
1093 /// let cstr = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0")
1094 /// .expect("CStr::from_bytes_with_nul failed");
1096 /// cstr.to_string_lossy(),
1097 /// Cow::Owned(String::from("Hello �World")) as Cow<'_, str>
1100 #[must_use = "this returns the result of the operation, \
1101 without modifying the original"]
1102 #[stable(feature = "cstr_to_str", since = "1.4.0")]
1103 fn to_string_lossy(&self) -> Cow
<'_
, str>;
1105 /// Converts a <code>[Box]<[CStr]></code> into a [`CString`] without copying or allocating.
1110 /// use std::ffi::CString;
1112 /// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
1113 /// let boxed = c_string.into_boxed_c_str();
1114 /// assert_eq!(boxed.into_c_string(), CString::new("foo").expect("CString::new failed"));
1116 #[must_use = "`self` will be dropped if the result is not used"]
1117 #[stable(feature = "into_boxed_c_str", since = "1.20.0")]
1118 fn into_c_string(self: Box
<Self>) -> CString
;
1122 #[unstable(feature = "cstr_internals", issue = "none")]
1123 impl CStrExt
for CStr
{
1124 fn to_string_lossy(&self) -> Cow
<'_
, str> {
1125 String
::from_utf8_lossy(self.to_bytes())
1128 fn into_c_string(self: Box
<Self>) -> CString
{
1133 #[cfg(not(bootstrap))]
1136 /// Converts a `CStr` into a <code>[Cow]<[str]></code>.
1138 /// If the contents of the `CStr` are valid UTF-8 data, this
1139 /// function will return a <code>[Cow]::[Borrowed]\(&[str])</code>
1140 /// with the corresponding <code>&[str]</code> slice. Otherwise, it will
1141 /// replace any invalid UTF-8 sequences with
1142 /// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD] and return a
1143 /// <code>[Cow]::[Owned]\(&[str])</code> with the result.
1145 /// [str]: prim@str "str"
1146 /// [Borrowed]: Cow::Borrowed
1147 /// [Owned]: Cow::Owned
1148 /// [U+FFFD]: core::char::REPLACEMENT_CHARACTER "std::char::REPLACEMENT_CHARACTER"
1152 /// Calling `to_string_lossy` on a `CStr` containing valid UTF-8:
1155 /// use std::borrow::Cow;
1156 /// use std::ffi::CStr;
1158 /// let cstr = CStr::from_bytes_with_nul(b"Hello World\0")
1159 /// .expect("CStr::from_bytes_with_nul failed");
1160 /// assert_eq!(cstr.to_string_lossy(), Cow::Borrowed("Hello World"));
1163 /// Calling `to_string_lossy` on a `CStr` containing invalid UTF-8:
1166 /// use std::borrow::Cow;
1167 /// use std::ffi::CStr;
1169 /// let cstr = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0")
1170 /// .expect("CStr::from_bytes_with_nul failed");
1172 /// cstr.to_string_lossy(),
1173 /// Cow::Owned(String::from("Hello �World")) as Cow<'_, str>
1176 #[rustc_allow_incoherent_impl]
1177 #[must_use = "this returns the result of the operation, \
1178 without modifying the original"]
1179 #[stable(feature = "cstr_to_str", since = "1.4.0")]
1180 pub fn to_string_lossy(&self) -> Cow
<'_
, str> {
1181 String
::from_utf8_lossy(self.to_bytes())
1184 /// Converts a <code>[Box]<[CStr]></code> into a [`CString`] without copying or allocating.
1189 /// use std::ffi::CString;
1191 /// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
1192 /// let boxed = c_string.into_boxed_c_str();
1193 /// assert_eq!(boxed.into_c_string(), CString::new("foo").expect("CString::new failed"));
1195 #[rustc_allow_incoherent_impl]
1196 #[must_use = "`self` will be dropped if the result is not used"]
1197 #[stable(feature = "into_boxed_c_str", since = "1.20.0")]
1198 pub fn into_c_string(self: Box
<Self>) -> CString
{