Skip to content

Commit dd9dda7

Browse files
author
Jorge Aparicio
committed
DSTify ToCStr
1 parent 82045ca commit dd9dda7

File tree

3 files changed

+29
-39
lines changed

3 files changed

+29
-39
lines changed

src/librustrt/c_str.rs

+29-15
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn main() {
7474
use collections::string::String;
7575
use collections::hash;
7676
use core::fmt;
77-
use core::kinds::marker;
77+
use core::kinds::{Sized, marker};
7878
use core::mem;
7979
use core::prelude::{Clone, Collection, Drop, Eq, ImmutableSlice, Iterator};
8080
use core::prelude::{MutableSlice, None, Option, Ordering, PartialEq};
@@ -286,7 +286,7 @@ impl fmt::Show for CString {
286286
}
287287

288288
/// A generic trait for converting a value to a CString.
289-
pub trait ToCStr {
289+
pub trait ToCStr for Sized? {
290290
/// Copy the receiver into a CString.
291291
///
292292
/// # Failure
@@ -329,15 +329,7 @@ pub trait ToCStr {
329329
}
330330
}
331331

332-
// FIXME (#12938): Until DST lands, we cannot decompose &str into &
333-
// and str, so we cannot usefully take ToCStr arguments by reference
334-
// (without forcing an additional & around &str). So we are instead
335-
// temporarily adding an instance for ~str and String, so that we can
336-
// take ToCStr as owned. When DST lands, the string instances should
337-
// be revisited, and arguments bound by ToCStr should be passed by
338-
// reference.
339-
340-
impl<'a> ToCStr for &'a str {
332+
impl ToCStr for str {
341333
#[inline]
342334
fn to_c_str(&self) -> CString {
343335
self.as_bytes().to_c_str()
@@ -384,10 +376,10 @@ impl ToCStr for String {
384376
// The length of the stack allocated buffer for `vec.with_c_str()`
385377
const BUF_LEN: uint = 128;
386378

387-
impl<'a> ToCStr for &'a [u8] {
379+
impl ToCStr for [u8] {
388380
fn to_c_str(&self) -> CString {
389381
let mut cs = unsafe { self.to_c_str_unchecked() };
390-
check_for_null(*self, cs.as_mut_ptr());
382+
check_for_null(self, cs.as_mut_ptr());
391383
cs
392384
}
393385

@@ -403,11 +395,33 @@ impl<'a> ToCStr for &'a [u8] {
403395
}
404396

405397
fn with_c_str<T>(&self, f: |*const libc::c_char| -> T) -> T {
406-
unsafe { with_c_str(*self, true, f) }
398+
unsafe { with_c_str(self, true, f) }
399+
}
400+
401+
unsafe fn with_c_str_unchecked<T>(&self, f: |*const libc::c_char| -> T) -> T {
402+
with_c_str(self, false, f)
403+
}
404+
}
405+
406+
impl<'a, Sized? T: ToCStr> ToCStr for &'a T {
407+
#[inline]
408+
fn to_c_str(&self) -> CString {
409+
(**self).to_c_str()
410+
}
411+
412+
#[inline]
413+
unsafe fn to_c_str_unchecked(&self) -> CString {
414+
(**self).to_c_str_unchecked()
415+
}
416+
417+
#[inline]
418+
fn with_c_str<T>(&self, f: |*const libc::c_char| -> T) -> T {
419+
(**self).with_c_str(f)
407420
}
408421

422+
#[inline]
409423
unsafe fn with_c_str_unchecked<T>(&self, f: |*const libc::c_char| -> T) -> T {
410-
with_c_str(*self, false, f)
424+
(**self).with_c_str_unchecked(f)
411425
}
412426
}
413427

src/libstd/path/posix.rs

-12
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,6 @@ impl ToCStr for Path {
106106
}
107107
}
108108

109-
impl<'a> ToCStr for &'a Path {
110-
#[inline]
111-
fn to_c_str(&self) -> CString {
112-
(*self).to_c_str()
113-
}
114-
115-
#[inline]
116-
unsafe fn to_c_str_unchecked(&self) -> CString {
117-
(*self).to_c_str_unchecked()
118-
}
119-
}
120-
121109
impl<S: hash::Writer> hash::Hash<S> for Path {
122110
#[inline]
123111
fn hash(&self, state: &mut S) {

src/libstd/path/windows.rs

-12
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,6 @@ impl ToCStr for Path {
130130
}
131131
}
132132

133-
impl<'a> ToCStr for &'a Path {
134-
#[inline]
135-
fn to_c_str(&self) -> CString {
136-
(*self).to_c_str()
137-
}
138-
139-
#[inline]
140-
unsafe fn to_c_str_unchecked(&self) -> CString {
141-
(*self).to_c_str_unchecked()
142-
}
143-
}
144-
145133
impl<S: hash::Writer> hash::Hash<S> for Path {
146134
#[cfg(not(test))]
147135
#[inline]

0 commit comments

Comments
 (0)