From 104902100d8894d7578694754590668d6d725a17 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Wed, 9 Sep 2015 22:37:59 +0300 Subject: Reduce code bloat from conversion traits in function parameters --- src/libstd/ffi/c_str.rs | 5 ++++- src/libstd/ffi/os_str.rs | 20 ++++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) (limited to 'src/libstd/ffi') diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 23daa87401a..6181c6c4ca6 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -181,7 +181,10 @@ impl CString { /// the position of the nul byte. #[stable(feature = "rust1", since = "1.0.0")] pub fn new>>(t: T) -> Result { - let bytes = t.into(); + Self::_new(t.into()) + } + + fn _new(bytes: Vec) -> Result { match bytes.iter().position(|x| *x == 0) { Some(i) => Err(NulError(i, bytes)), None => Ok(unsafe { CString::from_vec_unchecked(bytes) }), diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 751c76b9960..7409d9b45d2 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -73,18 +73,18 @@ impl OsString { /// convert; non UTF-8 data will produce `None`. #[unstable(feature = "convert", reason = "recently added", issue = "27704")] pub fn from_bytes(bytes: B) -> Option where B: Into> { - #[cfg(unix)] - fn from_bytes_inner(vec: Vec) -> Option { - use os::unix::ffi::OsStringExt; - Some(OsString::from_vec(vec)) - } + Self::_from_bytes(bytes.into()) + } - #[cfg(windows)] - fn from_bytes_inner(vec: Vec) -> Option { - String::from_utf8(vec).ok().map(OsString::from) - } + #[cfg(unix)] + fn _from_bytes(vec: Vec) -> Option { + use os::unix::ffi::OsStringExt; + Some(OsString::from_vec(vec)) + } - from_bytes_inner(bytes.into()) + #[cfg(windows)] + fn _from_bytes(vec: Vec) -> Option { + String::from_utf8(vec).ok().map(OsString::from) } /// Converts to an `OsStr` slice. -- cgit 1.4.1-3-g733a5