2014-11-25 13:28:35 -08:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-07-11 22:55:52 +02:00
|
|
|
use ascii;
|
2016-08-22 19:47:38 +00:00
|
|
|
use borrow::{Cow, Borrow};
|
|
|
|
use cmp::Ordering;
|
2015-03-30 17:56:48 -07:00
|
|
|
use error::Error;
|
2015-07-11 22:55:52 +02:00
|
|
|
use fmt::{self, Write};
|
2015-02-17 22:47:40 -08:00
|
|
|
use io;
|
2014-11-25 13:28:35 -08:00
|
|
|
use libc;
|
|
|
|
use mem;
|
2015-12-15 00:03:42 +01:00
|
|
|
use memchr;
|
2015-12-29 13:02:08 -05:00
|
|
|
use ops;
|
2015-11-11 12:02:55 -05:00
|
|
|
use os::raw::c_char;
|
2015-03-10 23:13:29 -05:00
|
|
|
use slice;
|
2015-10-11 19:49:57 -04:00
|
|
|
use str::{self, Utf8Error};
|
2014-11-25 13:28:35 -08:00
|
|
|
|
2015-02-17 22:47:40 -08:00
|
|
|
/// A type representing an owned C-compatible string
|
2014-11-25 13:28:35 -08:00
|
|
|
///
|
2015-02-17 22:47:40 -08:00
|
|
|
/// This type serves the primary purpose of being able to safely generate a
|
2014-11-25 13:28:35 -08:00
|
|
|
/// C-compatible string from a Rust byte slice or vector. An instance of this
|
|
|
|
/// type is a static guarantee that the underlying bytes contain no interior 0
|
|
|
|
/// bytes and the final byte is 0.
|
|
|
|
///
|
|
|
|
/// A `CString` is created from either a byte slice or a byte vector. After
|
|
|
|
/// being created, a `CString` predominately inherits all of its methods from
|
2015-11-11 14:25:18 -05:00
|
|
|
/// the `Deref` implementation to `[c_char]`. Note that the underlying array
|
|
|
|
/// is represented as an array of `c_char` as opposed to `u8`. A `u8` slice
|
|
|
|
/// can be obtained with the `as_bytes` method. Slices produced from a `CString`
|
|
|
|
/// do *not* contain the trailing nul terminator unless otherwise specified.
|
2014-11-25 13:28:35 -08:00
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2014-11-25 13:28:35 -08:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # fn main() {
|
|
|
|
/// use std::ffi::CString;
|
2015-11-11 12:02:55 -05:00
|
|
|
/// use std::os::raw::c_char;
|
2014-11-25 13:28:35 -08:00
|
|
|
///
|
|
|
|
/// extern {
|
2015-11-11 12:02:55 -05:00
|
|
|
/// fn my_printer(s: *const c_char);
|
2014-11-25 13:28:35 -08:00
|
|
|
/// }
|
|
|
|
///
|
2015-05-16 22:30:25 -04:00
|
|
|
/// let c_to_print = CString::new("Hello, world!").unwrap();
|
2014-11-25 13:28:35 -08:00
|
|
|
/// unsafe {
|
|
|
|
/// my_printer(c_to_print.as_ptr());
|
|
|
|
/// }
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2016-01-24 17:41:44 -05:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// `CString` is intended for working with traditional C-style strings
|
|
|
|
/// (a sequence of non-null bytes terminated by a single null byte); the
|
|
|
|
/// primary use case for these kinds of strings is interoperating with C-like
|
|
|
|
/// code. Often you will need to transfer ownership to/from that external
|
|
|
|
/// code. It is strongly recommended that you thoroughly read through the
|
|
|
|
/// documentation of `CString` before use, as improper ownership management
|
|
|
|
/// of `CString` instances can lead to invalid memory accesses, memory leaks,
|
|
|
|
/// and other memory errors.
|
|
|
|
|
2015-07-26 22:12:00 -07:00
|
|
|
#[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Clone)]
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-25 13:28:35 -08:00
|
|
|
pub struct CString {
|
2015-06-03 17:35:50 -04:00
|
|
|
inner: Box<[u8]>,
|
2015-02-17 22:47:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Representation of a borrowed C string.
|
|
|
|
///
|
|
|
|
/// This dynamically sized type is only safely constructed via a borrowed
|
|
|
|
/// version of an instance of `CString`. This type can be constructed from a raw
|
|
|
|
/// C string as well and represents a C string borrowed from another location.
|
|
|
|
///
|
|
|
|
/// Note that this structure is **not** `repr(C)` and is not recommended to be
|
|
|
|
/// placed in the signatures of FFI functions. Instead safe wrappers of FFI
|
|
|
|
/// functions may leverage the unsafe `from_ptr` constructor to provide a safe
|
|
|
|
/// interface to other consumers.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Inspecting a foreign C string
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::ffi::CStr;
|
2015-11-11 12:02:55 -05:00
|
|
|
/// use std::os::raw::c_char;
|
2015-02-17 22:47:40 -08:00
|
|
|
///
|
2015-11-11 12:02:55 -05:00
|
|
|
/// extern { fn my_string() -> *const c_char; }
|
2015-02-17 22:47:40 -08:00
|
|
|
///
|
2016-08-08 00:07:36 -04:00
|
|
|
/// unsafe {
|
|
|
|
/// let slice = CStr::from_ptr(my_string());
|
|
|
|
/// println!("string length: {}", slice.to_bytes().len());
|
2015-02-17 22:47:40 -08:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Passing a Rust-originating C string
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::ffi::{CString, CStr};
|
2015-11-11 12:02:55 -05:00
|
|
|
/// use std::os::raw::c_char;
|
2015-02-17 22:47:40 -08:00
|
|
|
///
|
|
|
|
/// fn work(data: &CStr) {
|
2015-11-11 12:02:55 -05:00
|
|
|
/// extern { fn work_with(data: *const c_char); }
|
2015-02-17 22:47:40 -08:00
|
|
|
///
|
|
|
|
/// unsafe { work_with(data.as_ptr()) }
|
|
|
|
/// }
|
|
|
|
///
|
2016-08-08 00:07:36 -04:00
|
|
|
/// let s = CString::new("data data data data").unwrap();
|
|
|
|
/// work(&s);
|
2015-02-17 22:47:40 -08:00
|
|
|
/// ```
|
2015-05-14 14:49:32 -07:00
|
|
|
///
|
|
|
|
/// Converting a foreign C string into a Rust `String`
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::ffi::CStr;
|
2015-11-11 12:02:55 -05:00
|
|
|
/// use std::os::raw::c_char;
|
2015-05-14 14:49:32 -07:00
|
|
|
///
|
2015-11-11 12:02:55 -05:00
|
|
|
/// extern { fn my_string() -> *const c_char; }
|
2015-05-14 14:49:32 -07:00
|
|
|
///
|
|
|
|
/// fn my_string_safe() -> String {
|
|
|
|
/// unsafe {
|
|
|
|
/// CStr::from_ptr(my_string()).to_string_lossy().into_owned()
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
2016-08-08 00:07:36 -04:00
|
|
|
/// println!("string: {}", my_string_safe());
|
2015-05-14 14:49:32 -07:00
|
|
|
/// ```
|
2015-02-17 22:47:40 -08:00
|
|
|
#[derive(Hash)]
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-17 22:47:40 -08:00
|
|
|
pub struct CStr {
|
2015-03-02 10:46:05 -08:00
|
|
|
// FIXME: this should not be represented with a DST slice but rather with
|
2015-11-11 12:02:55 -05:00
|
|
|
// just a raw `c_char` along with some form of marker to make
|
2015-03-02 10:46:05 -08:00
|
|
|
// this an unsized type. Essentially `sizeof(&CStr)` should be the
|
|
|
|
// same as `sizeof(&c_char)` but `CStr` should be an unsized type.
|
2015-11-11 12:02:55 -05:00
|
|
|
inner: [c_char]
|
2015-02-17 22:47:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// An error returned from `CString::new` to indicate that a nul byte was found
|
|
|
|
/// in the vector provided.
|
|
|
|
#[derive(Clone, PartialEq, Debug)]
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-17 22:47:40 -08:00
|
|
|
pub struct NulError(usize, Vec<u8>);
|
|
|
|
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 11:57:07 -07:00
|
|
|
/// An error returned from `CStr::from_bytes_with_nul` to indicate that a nul
|
|
|
|
/// byte was found too early in the slice provided or one wasn't found at all.
|
|
|
|
#[derive(Clone, PartialEq, Debug)]
|
|
|
|
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
|
|
|
|
pub struct FromBytesWithNulError { _a: () }
|
|
|
|
|
2015-10-11 19:49:57 -04:00
|
|
|
/// An error returned from `CString::into_string` to indicate that a UTF-8 error
|
|
|
|
/// was encountered during the conversion.
|
|
|
|
#[derive(Clone, PartialEq, Debug)]
|
2016-01-15 10:07:52 -08:00
|
|
|
#[stable(feature = "cstring_into", since = "1.7.0")]
|
2015-10-11 19:49:57 -04:00
|
|
|
pub struct IntoStringError {
|
|
|
|
inner: CString,
|
|
|
|
error: Utf8Error,
|
|
|
|
}
|
|
|
|
|
2014-11-25 13:28:35 -08:00
|
|
|
impl CString {
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Creates a new C-compatible string from a container of bytes.
|
2015-02-17 22:47:40 -08:00
|
|
|
///
|
|
|
|
/// This method will consume the provided data and use the underlying bytes
|
|
|
|
/// to construct a new string, ensuring that there is a trailing 0 byte.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::ffi::CString;
|
2015-11-11 12:02:55 -05:00
|
|
|
/// use std::os::raw::c_char;
|
2015-02-17 22:47:40 -08:00
|
|
|
///
|
2015-11-11 12:02:55 -05:00
|
|
|
/// extern { fn puts(s: *const c_char); }
|
2015-02-17 22:47:40 -08:00
|
|
|
///
|
2016-08-08 00:07:36 -04:00
|
|
|
/// let to_print = CString::new("Hello!").unwrap();
|
|
|
|
/// unsafe {
|
|
|
|
/// puts(to_print.as_ptr());
|
2015-02-17 22:47:40 -08:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return an error if the bytes yielded contain an
|
|
|
|
/// internal 0 byte. The error returned will contain the bytes as well as
|
|
|
|
/// the position of the nul byte.
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-18 09:14:54 -07:00
|
|
|
pub fn new<T: Into<Vec<u8>>>(t: T) -> Result<CString, NulError> {
|
2015-09-09 22:37:59 +03:00
|
|
|
Self::_new(t.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _new(bytes: Vec<u8>) -> Result<CString, NulError> {
|
2015-12-15 00:03:42 +01:00
|
|
|
match memchr::memchr(0, &bytes) {
|
2015-02-17 22:47:40 -08:00
|
|
|
Some(i) => Err(NulError(i, bytes)),
|
|
|
|
None => Ok(unsafe { CString::from_vec_unchecked(bytes) }),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Creates a C-compatible string from a byte vector without checking for
|
2014-11-25 13:28:35 -08:00
|
|
|
/// interior 0 bytes.
|
|
|
|
///
|
2015-04-12 10:47:35 -04:00
|
|
|
/// This method is equivalent to `new` except that no runtime assertion
|
|
|
|
/// is made that `v` contains no 0 bytes, and it requires an actual
|
2015-03-25 18:02:11 +01:00
|
|
|
/// byte vector, not anything that can be converted to one with Into.
|
2016-08-12 09:08:37 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::ffi::CString;
|
|
|
|
///
|
|
|
|
/// let raw = b"foo".to_vec();
|
|
|
|
/// unsafe {
|
|
|
|
/// let c_string = CString::from_vec_unchecked(raw);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-25 13:28:35 -08:00
|
|
|
pub unsafe fn from_vec_unchecked(mut v: Vec<u8>) -> CString {
|
2016-08-21 13:33:28 +02:00
|
|
|
v.reserve_exact(1);
|
2014-11-25 13:28:35 -08:00
|
|
|
v.push(0);
|
2015-06-03 17:35:50 -04:00
|
|
|
CString { inner: v.into_boxed_slice() }
|
2014-11-25 13:28:35 -08:00
|
|
|
}
|
|
|
|
|
2016-01-27 15:07:10 -08:00
|
|
|
/// Retakes ownership of a `CString` that was transferred to C.
|
2015-08-14 10:29:47 -07:00
|
|
|
///
|
2016-01-24 17:41:44 -05:00
|
|
|
/// This should only ever be called with a pointer that was earlier
|
2016-01-27 15:07:10 -08:00
|
|
|
/// obtained by calling `into_raw` on a `CString`. Additionally, the length
|
2016-01-24 17:41:44 -05:00
|
|
|
/// of the string will be recalculated from the pointer.
|
2015-09-10 13:26:44 -07:00
|
|
|
#[stable(feature = "cstr_memory", since = "1.4.0")]
|
2015-11-11 12:02:55 -05:00
|
|
|
pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
|
2015-05-25 12:37:45 -04:00
|
|
|
let len = libc::strlen(ptr) + 1; // Including the NUL byte
|
|
|
|
let slice = slice::from_raw_parts(ptr, len as usize);
|
|
|
|
CString { inner: mem::transmute(slice) }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Transfers ownership of the string to a C caller.
|
|
|
|
///
|
|
|
|
/// The pointer must be returned to Rust and reconstituted using
|
2015-10-01 12:43:00 +02:00
|
|
|
/// `from_raw` to be properly deallocated. Specifically, one
|
2015-05-25 12:37:45 -04:00
|
|
|
/// should *not* use the standard C `free` function to deallocate
|
|
|
|
/// this string.
|
|
|
|
///
|
2015-10-01 12:43:00 +02:00
|
|
|
/// Failure to call `from_raw` will lead to a memory leak.
|
2015-09-10 13:26:44 -07:00
|
|
|
#[stable(feature = "cstr_memory", since = "1.4.0")]
|
2015-11-11 12:02:55 -05:00
|
|
|
pub fn into_raw(self) -> *mut c_char {
|
|
|
|
Box::into_raw(self.inner) as *mut c_char
|
2015-05-25 12:37:45 -04:00
|
|
|
}
|
|
|
|
|
2015-10-11 19:49:57 -04:00
|
|
|
/// Converts the `CString` into a `String` if it contains valid Unicode data.
|
|
|
|
///
|
|
|
|
/// On failure, ownership of the original `CString` is returned.
|
2016-01-15 10:07:52 -08:00
|
|
|
#[stable(feature = "cstring_into", since = "1.7.0")]
|
2015-10-11 19:49:57 -04:00
|
|
|
pub fn into_string(self) -> Result<String, IntoStringError> {
|
|
|
|
String::from_utf8(self.into_bytes())
|
|
|
|
.map_err(|e| IntoStringError {
|
|
|
|
error: e.utf8_error(),
|
|
|
|
inner: unsafe { CString::from_vec_unchecked(e.into_bytes()) },
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the underlying byte buffer.
|
|
|
|
///
|
|
|
|
/// The returned buffer does **not** contain the trailing nul separator and
|
|
|
|
/// it is guaranteed to not have any interior nul bytes.
|
2016-01-15 10:07:52 -08:00
|
|
|
#[stable(feature = "cstring_into", since = "1.7.0")]
|
2015-10-11 19:49:57 -04:00
|
|
|
pub fn into_bytes(self) -> Vec<u8> {
|
|
|
|
let mut vec = self.inner.into_vec();
|
|
|
|
let _nul = vec.pop();
|
|
|
|
debug_assert_eq!(_nul, Some(0u8));
|
|
|
|
vec
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Equivalent to the `into_bytes` function except that the returned vector
|
|
|
|
/// includes the trailing nul byte.
|
2016-01-15 10:07:52 -08:00
|
|
|
#[stable(feature = "cstring_into", since = "1.7.0")]
|
2015-10-11 19:49:57 -04:00
|
|
|
pub fn into_bytes_with_nul(self) -> Vec<u8> {
|
|
|
|
self.inner.into_vec()
|
|
|
|
}
|
|
|
|
|
2015-02-17 22:47:40 -08:00
|
|
|
/// Returns the contents of this `CString` as a slice of bytes.
|
|
|
|
///
|
|
|
|
/// The returned slice does **not** contain the trailing nul separator and
|
2015-02-22 08:52:07 -05:00
|
|
|
/// it is guaranteed to not have any interior nul bytes.
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-25 13:28:35 -08:00
|
|
|
pub fn as_bytes(&self) -> &[u8] {
|
2015-02-17 22:47:40 -08:00
|
|
|
&self.inner[..self.inner.len() - 1]
|
2014-11-25 13:28:35 -08:00
|
|
|
}
|
|
|
|
|
2015-02-17 22:47:40 -08:00
|
|
|
/// Equivalent to the `as_bytes` function except that the returned slice
|
|
|
|
/// includes the trailing nul byte.
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-25 13:28:35 -08:00
|
|
|
pub fn as_bytes_with_nul(&self) -> &[u8] {
|
2015-02-17 22:47:40 -08:00
|
|
|
&self.inner
|
2014-11-25 13:28:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-12-29 13:02:08 -05:00
|
|
|
impl ops::Deref for CString {
|
2015-02-17 22:47:40 -08:00
|
|
|
type Target = CStr;
|
2014-11-25 13:28:35 -08:00
|
|
|
|
2015-02-17 22:47:40 -08:00
|
|
|
fn deref(&self) -> &CStr {
|
|
|
|
unsafe { mem::transmute(self.as_bytes_with_nul()) }
|
2014-11-25 13:28:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-24 09:15:42 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-20 15:45:07 -08:00
|
|
|
impl fmt::Debug for CString {
|
2014-11-25 13:28:35 -08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-07-11 22:55:52 +02:00
|
|
|
fmt::Debug::fmt(&**self, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 10:07:52 -08:00
|
|
|
#[stable(feature = "cstring_into", since = "1.7.0")]
|
|
|
|
impl From<CString> for Vec<u8> {
|
|
|
|
fn from(s: CString) -> Vec<u8> {
|
|
|
|
s.into_bytes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-11 22:55:52 +02:00
|
|
|
#[stable(feature = "cstr_debug", since = "1.3.0")]
|
|
|
|
impl fmt::Debug for CStr {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2016-03-22 22:01:37 -05:00
|
|
|
write!(f, "\"")?;
|
2015-07-11 22:55:52 +02:00
|
|
|
for byte in self.to_bytes().iter().flat_map(|&b| ascii::escape_default(b)) {
|
2016-03-22 22:01:37 -05:00
|
|
|
f.write_char(byte as char)?;
|
2015-07-11 22:55:52 +02:00
|
|
|
}
|
|
|
|
write!(f, "\"")
|
2014-11-25 13:28:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-15 17:58:59 +02:00
|
|
|
#[stable(feature = "cstr_default", since = "1.10.0")]
|
|
|
|
impl<'a> Default for &'a CStr {
|
|
|
|
fn default() -> &'a CStr {
|
|
|
|
static SLICE: &'static [c_char] = &[0];
|
|
|
|
unsafe { CStr::from_ptr(SLICE.as_ptr()) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "cstr_default", since = "1.10.0")]
|
|
|
|
impl Default for CString {
|
2016-09-11 17:00:09 +05:30
|
|
|
/// Creates a new `CString`, by calling the `Default` of `CStr`, and then owns it.
|
2016-04-15 17:58:59 +02:00
|
|
|
fn default() -> CString {
|
|
|
|
let a: &CStr = Default::default();
|
|
|
|
a.to_owned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-09 16:38:55 -07:00
|
|
|
#[stable(feature = "cstr_borrow", since = "1.3.0")]
|
|
|
|
impl Borrow<CStr> for CString {
|
|
|
|
fn borrow(&self) -> &CStr { self }
|
|
|
|
}
|
|
|
|
|
2015-02-17 22:47:40 -08:00
|
|
|
impl NulError {
|
|
|
|
/// Returns the position of the nul byte in the slice that was provided to
|
2015-04-12 10:47:35 -04:00
|
|
|
/// `CString::new`.
|
2016-08-02 08:49:05 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::ffi::CString;
|
|
|
|
///
|
|
|
|
/// let nul_error = CString::new("foo\0bar").unwrap_err();
|
|
|
|
/// assert_eq!(nul_error.nul_position(), 3);
|
|
|
|
///
|
|
|
|
/// let nul_error = CString::new("foo bar\0").unwrap_err();
|
|
|
|
/// assert_eq!(nul_error.nul_position(), 7);
|
|
|
|
/// ```
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-17 22:47:40 -08:00
|
|
|
pub fn nul_position(&self) -> usize { self.0 }
|
|
|
|
|
|
|
|
/// Consumes this error, returning the underlying vector of bytes which
|
|
|
|
/// generated the error in the first place.
|
2016-08-06 17:50:37 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::ffi::CString;
|
|
|
|
///
|
|
|
|
/// let nul_error = CString::new("foo\0bar").unwrap_err();
|
|
|
|
/// assert_eq!(nul_error.into_vec(), b"foo\0bar");
|
|
|
|
/// ```
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-17 22:47:40 -08:00
|
|
|
pub fn into_vec(self) -> Vec<u8> { self.1 }
|
|
|
|
}
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-17 22:47:40 -08:00
|
|
|
impl Error for NulError {
|
|
|
|
fn description(&self) -> &str { "nul byte found in data" }
|
|
|
|
}
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-17 22:47:40 -08:00
|
|
|
impl fmt::Display for NulError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "nul byte found in provided data at position: {}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-03-30 17:56:48 -07:00
|
|
|
impl From<NulError> for io::Error {
|
|
|
|
fn from(_: NulError) -> io::Error {
|
2015-02-17 22:47:40 -08:00
|
|
|
io::Error::new(io::ErrorKind::InvalidInput,
|
2015-03-31 16:01:03 -07:00
|
|
|
"data provided contains a nul byte")
|
2015-02-17 22:47:40 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-11 19:49:57 -04:00
|
|
|
impl IntoStringError {
|
|
|
|
/// Consumes this error, returning original `CString` which generated the
|
|
|
|
/// error.
|
2016-01-15 10:07:52 -08:00
|
|
|
#[stable(feature = "cstring_into", since = "1.7.0")]
|
2015-10-11 19:49:57 -04:00
|
|
|
pub fn into_cstring(self) -> CString {
|
|
|
|
self.inner
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Access the underlying UTF-8 error that was the cause of this error.
|
2016-01-15 10:07:52 -08:00
|
|
|
#[stable(feature = "cstring_into", since = "1.7.0")]
|
2015-10-11 19:49:57 -04:00
|
|
|
pub fn utf8_error(&self) -> Utf8Error {
|
|
|
|
self.error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 10:07:52 -08:00
|
|
|
#[stable(feature = "cstring_into", since = "1.7.0")]
|
2015-10-11 19:49:57 -04:00
|
|
|
impl Error for IntoStringError {
|
|
|
|
fn description(&self) -> &str {
|
2016-01-15 10:07:52 -08:00
|
|
|
"C string contained non-utf8 bytes"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&Error> {
|
|
|
|
Some(&self.error)
|
2015-10-11 19:49:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 10:07:52 -08:00
|
|
|
#[stable(feature = "cstring_into", since = "1.7.0")]
|
2015-10-11 19:49:57 -04:00
|
|
|
impl fmt::Display for IntoStringError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2016-01-15 10:07:52 -08:00
|
|
|
self.description().fmt(f)
|
2015-10-11 19:49:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 22:47:40 -08:00
|
|
|
impl CStr {
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Casts a raw C string to a safe C string wrapper.
|
2015-02-17 22:47:40 -08:00
|
|
|
///
|
|
|
|
/// This function will cast the provided `ptr` to the `CStr` wrapper which
|
|
|
|
/// allows inspection and interoperation of non-owned C strings. This method
|
|
|
|
/// is unsafe for a number of reasons:
|
|
|
|
///
|
|
|
|
/// * There is no guarantee to the validity of `ptr`
|
|
|
|
/// * The returned lifetime is not guaranteed to be the actual lifetime of
|
|
|
|
/// `ptr`
|
|
|
|
/// * There is no guarantee that the memory pointed to by `ptr` contains a
|
|
|
|
/// valid nul terminator byte at the end of the string.
|
|
|
|
///
|
|
|
|
/// > **Note**: This operation is intended to be a 0-cost cast but it is
|
|
|
|
/// > currently implemented with an up-front calculation of the length of
|
|
|
|
/// > the string. This is not guaranteed to always be the case.
|
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2015-02-17 22:47:40 -08:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # fn main() {
|
|
|
|
/// use std::ffi::CStr;
|
2015-11-11 12:02:55 -05:00
|
|
|
/// use std::os::raw::c_char;
|
2015-02-17 22:47:40 -08:00
|
|
|
///
|
|
|
|
/// extern {
|
2015-11-11 12:02:55 -05:00
|
|
|
/// fn my_string() -> *const c_char;
|
2015-02-17 22:47:40 -08:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// unsafe {
|
|
|
|
/// let slice = CStr::from_ptr(my_string());
|
2016-01-19 16:29:53 +11:00
|
|
|
/// println!("string returned: {}", slice.to_str().unwrap());
|
2015-02-17 22:47:40 -08:00
|
|
|
/// }
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-11 12:02:55 -05:00
|
|
|
pub unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr {
|
2015-02-17 22:47:40 -08:00
|
|
|
let len = libc::strlen(ptr);
|
|
|
|
mem::transmute(slice::from_raw_parts(ptr, len as usize + 1))
|
|
|
|
}
|
|
|
|
|
2015-12-29 13:41:43 -05:00
|
|
|
/// Creates a C string wrapper from a byte slice.
|
|
|
|
///
|
|
|
|
/// This function will cast the provided `bytes` to a `CStr` wrapper after
|
2016-02-23 01:36:50 -05:00
|
|
|
/// ensuring that it is null terminated and does not contain any interior
|
2015-12-29 13:41:43 -05:00
|
|
|
/// nul bytes.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::ffi::CStr;
|
|
|
|
///
|
2016-01-24 19:51:19 -05:00
|
|
|
/// let cstr = CStr::from_bytes_with_nul(b"hello\0");
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 11:57:07 -07:00
|
|
|
/// assert!(cstr.is_ok());
|
2015-12-29 13:41:43 -05:00
|
|
|
/// ```
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 11:57:07 -07:00
|
|
|
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
|
|
|
|
pub fn from_bytes_with_nul(bytes: &[u8])
|
|
|
|
-> Result<&CStr, FromBytesWithNulError> {
|
2015-12-29 13:41:43 -05:00
|
|
|
if bytes.is_empty() || memchr::memchr(0, &bytes) != Some(bytes.len() - 1) {
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 11:57:07 -07:00
|
|
|
Err(FromBytesWithNulError { _a: () })
|
2015-12-29 13:41:43 -05:00
|
|
|
} else {
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 11:57:07 -07:00
|
|
|
Ok(unsafe { Self::from_bytes_with_nul_unchecked(bytes) })
|
2015-12-29 13:41:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Unsafely creates a C string wrapper from a byte slice.
|
|
|
|
///
|
|
|
|
/// This function will cast the provided `bytes` to a `CStr` wrapper without
|
|
|
|
/// performing any sanity checks. The provided slice must be null terminated
|
|
|
|
/// and not contain any interior nul bytes.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::ffi::{CStr, CString};
|
|
|
|
///
|
|
|
|
/// unsafe {
|
|
|
|
/// let cstring = CString::new("hello").unwrap();
|
2016-01-24 19:51:19 -05:00
|
|
|
/// let cstr = CStr::from_bytes_with_nul_unchecked(cstring.to_bytes_with_nul());
|
2015-12-29 13:41:43 -05:00
|
|
|
/// assert_eq!(cstr, &*cstring);
|
|
|
|
/// }
|
|
|
|
/// ```
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 11:57:07 -07:00
|
|
|
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
|
2016-02-23 01:36:50 -05:00
|
|
|
pub unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
|
2015-12-29 13:41:43 -05:00
|
|
|
mem::transmute(bytes)
|
|
|
|
}
|
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Returns the inner pointer to this C string.
|
2015-02-17 22:47:40 -08:00
|
|
|
///
|
|
|
|
/// The returned pointer will be valid for as long as `self` is and points
|
2015-02-22 08:52:07 -05:00
|
|
|
/// to a contiguous region of memory terminated with a 0 byte to represent
|
2015-02-17 22:47:40 -08:00
|
|
|
/// the end of the string.
|
2016-06-19 11:55:34 +03:00
|
|
|
///
|
|
|
|
/// **WARNING**
|
|
|
|
///
|
|
|
|
/// It is your responsibility to make sure that the underlying memory is not
|
|
|
|
/// freed too early. For example, the following code will cause undefined
|
|
|
|
/// behaviour when `ptr` is used inside the `unsafe` block:
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::ffi::{CString};
|
|
|
|
///
|
|
|
|
/// let ptr = CString::new("Hello").unwrap().as_ptr();
|
|
|
|
/// unsafe {
|
|
|
|
/// // `ptr` is dangling
|
|
|
|
/// *ptr;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// This happens because the pointer returned by `as_ptr` does not carry any
|
|
|
|
/// lifetime information and the string is deallocated immediately after
|
|
|
|
/// the `CString::new("Hello").unwrap().as_ptr()` expression is evaluated.
|
|
|
|
/// To fix the problem, bind the string to a local variable:
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::ffi::{CString};
|
|
|
|
///
|
|
|
|
/// let hello = CString::new("Hello").unwrap();
|
|
|
|
/// let ptr = hello.as_ptr();
|
|
|
|
/// unsafe {
|
|
|
|
/// // `ptr` is valid because `hello` is in scope
|
|
|
|
/// *ptr;
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-11 12:02:55 -05:00
|
|
|
pub fn as_ptr(&self) -> *const c_char {
|
2015-02-17 22:47:40 -08:00
|
|
|
self.inner.as_ptr()
|
|
|
|
}
|
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Converts this C string to a byte slice.
|
2015-02-17 22:47:40 -08:00
|
|
|
///
|
|
|
|
/// This function will calculate the length of this string (which normally
|
|
|
|
/// requires a linear amount of work to be done) and then return the
|
|
|
|
/// resulting slice of `u8` elements.
|
|
|
|
///
|
|
|
|
/// The returned slice will **not** contain the trailing nul that this C
|
|
|
|
/// string has.
|
|
|
|
///
|
|
|
|
/// > **Note**: This method is currently implemented as a 0-cost cast, but
|
|
|
|
/// > it is planned to alter its definition in the future to perform the
|
|
|
|
/// > length calculation whenever this method is called.
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-17 22:47:40 -08:00
|
|
|
pub fn to_bytes(&self) -> &[u8] {
|
|
|
|
let bytes = self.to_bytes_with_nul();
|
|
|
|
&bytes[..bytes.len() - 1]
|
|
|
|
}
|
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Converts this C string to a byte slice containing the trailing 0 byte.
|
2015-02-17 22:47:40 -08:00
|
|
|
///
|
|
|
|
/// This function is the equivalent of `to_bytes` except that it will retain
|
|
|
|
/// the trailing nul instead of chopping it off.
|
|
|
|
///
|
|
|
|
/// > **Note**: This method is currently implemented as a 0-cost cast, but
|
|
|
|
/// > it is planned to alter its definition in the future to perform the
|
|
|
|
/// > length calculation whenever this method is called.
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-17 22:47:40 -08:00
|
|
|
pub fn to_bytes_with_nul(&self) -> &[u8] {
|
2015-07-24 21:02:05 +02:00
|
|
|
unsafe { mem::transmute(&self.inner) }
|
2015-02-17 22:47:40 -08:00
|
|
|
}
|
2015-05-14 14:49:32 -07:00
|
|
|
|
|
|
|
/// Yields a `&str` slice if the `CStr` contains valid UTF-8.
|
|
|
|
///
|
|
|
|
/// This function will calculate the length of this string and check for
|
|
|
|
/// UTF-8 validity, and then return the `&str` if it's valid.
|
|
|
|
///
|
|
|
|
/// > **Note**: This method is currently implemented to check for validity
|
|
|
|
/// > after a 0-cost cast, but it is planned to alter its definition in the
|
|
|
|
/// > future to perform the length calculation in addition to the UTF-8
|
|
|
|
/// > check whenever this method is called.
|
2015-09-10 13:26:44 -07:00
|
|
|
#[stable(feature = "cstr_to_str", since = "1.4.0")]
|
2015-05-14 14:49:32 -07:00
|
|
|
pub fn to_str(&self) -> Result<&str, str::Utf8Error> {
|
2015-08-13 10:12:38 -07:00
|
|
|
// NB: When CStr is changed to perform the length check in .to_bytes()
|
|
|
|
// instead of in from_ptr(), it may be worth considering if this should
|
|
|
|
// be rewritten to do the UTF-8 check inline with the length calculation
|
|
|
|
// instead of doing it afterwards.
|
2015-05-14 14:49:32 -07:00
|
|
|
str::from_utf8(self.to_bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a `CStr` into a `Cow<str>`.
|
|
|
|
///
|
|
|
|
/// This function will calculate the length of this string (which normally
|
|
|
|
/// requires a linear amount of work to be done) and then return the
|
|
|
|
/// resulting slice as a `Cow<str>`, replacing any invalid UTF-8 sequences
|
|
|
|
/// with `U+FFFD REPLACEMENT CHARACTER`.
|
|
|
|
///
|
|
|
|
/// > **Note**: This method is currently implemented to check for validity
|
|
|
|
/// > after a 0-cost cast, but it is planned to alter its definition in the
|
|
|
|
/// > future to perform the length calculation in addition to the UTF-8
|
|
|
|
/// > check whenever this method is called.
|
2015-09-10 13:26:44 -07:00
|
|
|
#[stable(feature = "cstr_to_str", since = "1.4.0")]
|
2015-05-14 14:49:32 -07:00
|
|
|
pub fn to_string_lossy(&self) -> Cow<str> {
|
|
|
|
String::from_utf8_lossy(self.to_bytes())
|
|
|
|
}
|
2015-02-17 22:47:40 -08:00
|
|
|
}
|
|
|
|
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-17 22:47:40 -08:00
|
|
|
impl PartialEq for CStr {
|
|
|
|
fn eq(&self, other: &CStr) -> bool {
|
2015-02-20 20:32:55 +03:00
|
|
|
self.to_bytes().eq(other.to_bytes())
|
2015-02-17 22:47:40 -08:00
|
|
|
}
|
|
|
|
}
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-17 22:47:40 -08:00
|
|
|
impl Eq for CStr {}
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-17 22:47:40 -08:00
|
|
|
impl PartialOrd for CStr {
|
|
|
|
fn partial_cmp(&self, other: &CStr) -> Option<Ordering> {
|
|
|
|
self.to_bytes().partial_cmp(&other.to_bytes())
|
|
|
|
}
|
|
|
|
}
|
2015-03-02 10:46:05 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-17 22:47:40 -08:00
|
|
|
impl Ord for CStr {
|
|
|
|
fn cmp(&self, other: &CStr) -> Ordering {
|
|
|
|
self.to_bytes().cmp(&other.to_bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-09 16:38:55 -07:00
|
|
|
#[stable(feature = "cstr_borrow", since = "1.3.0")]
|
|
|
|
impl ToOwned for CStr {
|
|
|
|
type Owned = CString;
|
|
|
|
|
|
|
|
fn to_owned(&self) -> CString {
|
|
|
|
unsafe { CString::from_vec_unchecked(self.to_bytes().to_vec()) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-31 14:21:40 -05:00
|
|
|
#[stable(feature = "cstring_asref", since = "1.7.0")]
|
2015-12-31 13:56:21 -05:00
|
|
|
impl<'a> From<&'a CStr> for CString {
|
|
|
|
fn from(s: &'a CStr) -> CString {
|
|
|
|
s.to_owned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-31 14:21:40 -05:00
|
|
|
#[stable(feature = "cstring_asref", since = "1.7.0")]
|
2015-12-29 13:02:08 -05:00
|
|
|
impl ops::Index<ops::RangeFull> for CString {
|
|
|
|
type Output = CStr;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, _index: ops::RangeFull) -> &CStr {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-31 14:21:40 -05:00
|
|
|
#[stable(feature = "cstring_asref", since = "1.7.0")]
|
2015-12-29 13:02:08 -05:00
|
|
|
impl AsRef<CStr> for CStr {
|
|
|
|
fn as_ref(&self) -> &CStr {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-31 14:21:40 -05:00
|
|
|
#[stable(feature = "cstring_asref", since = "1.7.0")]
|
2015-12-29 13:02:08 -05:00
|
|
|
impl AsRef<CStr> for CString {
|
|
|
|
fn as_ref(&self) -> &CStr {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-25 13:28:35 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2015-11-11 12:02:55 -05:00
|
|
|
use os::raw::c_char;
|
2015-05-14 14:49:32 -07:00
|
|
|
use borrow::Cow::{Borrowed, Owned};
|
2015-08-11 17:27:05 -07:00
|
|
|
use hash::{SipHasher, Hash, Hasher};
|
2014-11-25 13:28:35 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn c_to_rust() {
|
|
|
|
let data = b"123\0";
|
2015-11-11 12:02:55 -05:00
|
|
|
let ptr = data.as_ptr() as *const c_char;
|
2014-11-25 13:28:35 -08:00
|
|
|
unsafe {
|
2015-02-28 20:07:05 +02:00
|
|
|
assert_eq!(CStr::from_ptr(ptr).to_bytes(), b"123");
|
|
|
|
assert_eq!(CStr::from_ptr(ptr).to_bytes_with_nul(), b"123\0");
|
2014-11-25 13:28:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple() {
|
2015-03-18 09:16:08 +03:00
|
|
|
let s = CString::new("1234").unwrap();
|
2014-11-25 13:28:35 -08:00
|
|
|
assert_eq!(s.as_bytes(), b"1234");
|
|
|
|
assert_eq!(s.as_bytes_with_nul(), b"1234\0");
|
|
|
|
}
|
|
|
|
|
2015-02-17 22:47:40 -08:00
|
|
|
#[test]
|
|
|
|
fn build_with_zero1() {
|
2015-03-18 09:16:08 +03:00
|
|
|
assert!(CString::new(&b"\0"[..]).is_err());
|
2015-02-17 22:47:40 -08:00
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn build_with_zero2() {
|
2015-02-18 14:39:37 -08:00
|
|
|
assert!(CString::new(vec![0]).is_err());
|
2015-02-17 22:47:40 -08:00
|
|
|
}
|
2014-11-25 13:28:35 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn build_with_zero3() {
|
|
|
|
unsafe {
|
|
|
|
let s = CString::from_vec_unchecked(vec![0]);
|
|
|
|
assert_eq!(s.as_bytes(), b"\0");
|
|
|
|
}
|
|
|
|
}
|
2015-01-20 15:45:07 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn formatted() {
|
2015-07-11 22:55:52 +02:00
|
|
|
let s = CString::new(&b"abc\x01\x02\n\xE2\x80\xA6\xFF"[..]).unwrap();
|
|
|
|
assert_eq!(format!("{:?}", s), r#""abc\x01\x02\n\xe2\x80\xa6\xff""#);
|
2015-01-20 15:45:07 -08:00
|
|
|
}
|
2015-02-17 22:47:40 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn borrowed() {
|
|
|
|
unsafe {
|
|
|
|
let s = CStr::from_ptr(b"12\0".as_ptr() as *const _);
|
|
|
|
assert_eq!(s.to_bytes(), b"12");
|
|
|
|
assert_eq!(s.to_bytes_with_nul(), b"12\0");
|
|
|
|
}
|
|
|
|
}
|
2015-05-14 14:49:32 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn to_str() {
|
|
|
|
let data = b"123\xE2\x80\xA6\0";
|
2015-11-11 12:02:55 -05:00
|
|
|
let ptr = data.as_ptr() as *const c_char;
|
2015-05-14 14:49:32 -07:00
|
|
|
unsafe {
|
|
|
|
assert_eq!(CStr::from_ptr(ptr).to_str(), Ok("123…"));
|
|
|
|
assert_eq!(CStr::from_ptr(ptr).to_string_lossy(), Borrowed("123…"));
|
|
|
|
}
|
|
|
|
let data = b"123\xE2\0";
|
2015-11-11 12:02:55 -05:00
|
|
|
let ptr = data.as_ptr() as *const c_char;
|
2015-05-14 14:49:32 -07:00
|
|
|
unsafe {
|
|
|
|
assert!(CStr::from_ptr(ptr).to_str().is_err());
|
|
|
|
assert_eq!(CStr::from_ptr(ptr).to_string_lossy(), Owned::<str>(format!("123\u{FFFD}")));
|
|
|
|
}
|
|
|
|
}
|
2015-07-09 16:38:55 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn to_owned() {
|
|
|
|
let data = b"123\0";
|
2015-11-11 12:02:55 -05:00
|
|
|
let ptr = data.as_ptr() as *const c_char;
|
2015-07-09 16:38:55 -07:00
|
|
|
|
|
|
|
let owned = unsafe { CStr::from_ptr(ptr).to_owned() };
|
|
|
|
assert_eq!(owned.as_bytes_with_nul(), data);
|
|
|
|
}
|
2015-07-09 17:33:00 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn equal_hash() {
|
|
|
|
let data = b"123\xE2\xFA\xA6\0";
|
2015-11-11 12:02:55 -05:00
|
|
|
let ptr = data.as_ptr() as *const c_char;
|
2015-07-09 17:33:00 -07:00
|
|
|
let cstr: &'static CStr = unsafe { CStr::from_ptr(ptr) };
|
|
|
|
|
2015-08-11 17:27:05 -07:00
|
|
|
let mut s = SipHasher::new_with_keys(0, 0);
|
|
|
|
cstr.hash(&mut s);
|
|
|
|
let cstr_hash = s.finish();
|
|
|
|
let mut s = SipHasher::new_with_keys(0, 0);
|
|
|
|
CString::new(&data[..data.len() - 1]).unwrap().hash(&mut s);
|
|
|
|
let cstring_hash = s.finish();
|
2015-07-09 17:33:00 -07:00
|
|
|
|
|
|
|
assert_eq!(cstr_hash, cstring_hash);
|
|
|
|
}
|
2016-02-23 01:37:21 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_bytes_with_nul() {
|
|
|
|
let data = b"123\0";
|
|
|
|
let cstr = CStr::from_bytes_with_nul(data);
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 11:57:07 -07:00
|
|
|
assert_eq!(cstr.map(CStr::to_bytes), Ok(&b"123"[..]));
|
|
|
|
let cstr = CStr::from_bytes_with_nul(data);
|
|
|
|
assert_eq!(cstr.map(CStr::to_bytes_with_nul), Ok(&b"123\0"[..]));
|
2016-02-23 01:37:21 -05:00
|
|
|
|
|
|
|
unsafe {
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 11:57:07 -07:00
|
|
|
let cstr = CStr::from_bytes_with_nul(data);
|
2016-02-23 01:37:21 -05:00
|
|
|
let cstr_unchecked = CStr::from_bytes_with_nul_unchecked(data);
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 11:57:07 -07:00
|
|
|
assert_eq!(cstr, Ok(cstr_unchecked));
|
2016-02-23 01:37:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_bytes_with_nul_unterminated() {
|
|
|
|
let data = b"123";
|
|
|
|
let cstr = CStr::from_bytes_with_nul(data);
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 11:57:07 -07:00
|
|
|
assert!(cstr.is_err());
|
2016-02-23 01:37:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_bytes_with_nul_interior() {
|
|
|
|
let data = b"1\023\0";
|
|
|
|
let cstr = CStr::from_bytes_with_nul(data);
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 11:57:07 -07:00
|
|
|
assert!(cstr.is_err());
|
2016-02-23 01:37:21 -05:00
|
|
|
}
|
2014-11-25 13:28:35 -08:00
|
|
|
}
|