Skip to content

Implement Borrow<CStr> for CString and ToOwned for CStr #26928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use borrow::{Cow, ToOwned};
use borrow::{Cow, ToOwned, Borrow};
use boxed::Box;
use clone::Clone;
use convert::{Into, From};
Expand Down Expand Up @@ -272,6 +272,11 @@ impl fmt::Debug for CString {
}
}

#[stable(feature = "cstr_borrow", since = "1.3.0")]
impl Borrow<CStr> for CString {
fn borrow(&self) -> &CStr { self }
}

impl NulError {
/// Returns the position of the nul byte in the slice that was provided to
/// `CString::new`.
Expand Down Expand Up @@ -444,6 +449,15 @@ impl Ord for CStr {
}
}

#[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()) }
}
}

#[cfg(test)]
mod tests {
use prelude::v1::*;
Expand Down Expand Up @@ -515,4 +529,28 @@ mod tests {
assert_eq!(CStr::from_ptr(ptr).to_string_lossy(), Owned::<str>(format!("123\u{FFFD}")));
}
}

#[test]
fn to_owned() {
let data = b"123\0";
let ptr = data.as_ptr() as *const libc::c_char;

let owned = unsafe { CStr::from_ptr(ptr).to_owned() };
assert_eq!(owned.as_bytes_with_nul(), data);
}

#[test]
fn equal_hash() {
use hash;

let data = b"123\xE2\xFA\xA6\0";
let ptr = data.as_ptr() as *const libc::c_char;
let cstr: &'static CStr = unsafe { CStr::from_ptr(ptr) };

let cstr_hash = hash::hash::<_, hash::SipHasher>(&cstr);
let cstring_hash =
hash::hash::<_, hash::SipHasher>(&CString::new(&data[..data.len() - 1]).unwrap());

assert_eq!(cstr_hash, cstring_hash);
}
}
2 changes: 1 addition & 1 deletion src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
#![feature(wrapping)]
#![feature(zero_one)]
#![cfg_attr(windows, feature(str_utf16))]
#![cfg_attr(test, feature(float_from_str_radix, range_inclusive, float_extras))]
#![cfg_attr(test, feature(float_from_str_radix, range_inclusive, float_extras, hash_default))]
#![cfg_attr(test, feature(test, rustc_private, float_consts))]
#![cfg_attr(target_env = "msvc", feature(link_args))]

Expand Down