Skip to content

Commit 2c02ee9

Browse files
committed
sethostname: convert to taking OsStr ref
Baed on discussions on the related PR, sethostname now takes an `S: AsRef<OsStr>` in order to allow for a greater range of inputs that allow for a more fluid interface. Signed-off-by: Paul Osborne <osbpau@gmail.com>
1 parent 3491234 commit 2c02ee9

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/unistd.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use fcntl::{fcntl, OFlag, O_CLOEXEC, FD_CLOEXEC};
55
use fcntl::FcntlArg::F_SETFD;
66
use libc::{self, c_char, c_void, c_int, c_uint, size_t, pid_t, off_t, uid_t, gid_t, mode_t};
77
use std::mem;
8-
use std::ffi::{CString, CStr, OsString};
9-
use std::os::unix::ffi::{OsStringExt};
8+
use std::ffi::{CString, CStr, OsString, OsStr};
9+
use std::os::unix::ffi::{OsStringExt, OsStrExt};
1010
use std::os::unix::io::RawFd;
1111
use std::path::{PathBuf};
1212
use void::Void;
@@ -487,7 +487,7 @@ pub fn daemon(nochdir: bool, noclose: bool) -> Result<()> {
487487
/// On some systems, the host name is limited to as few as 64 bytes. An error
488488
/// will be return if the name is not valid or the current process does not have
489489
/// permissions to update the host name.
490-
pub fn sethostname(name: &str) -> Result<()> {
490+
pub fn sethostname<S: AsRef<OsStr>>(name: S) -> Result<()> {
491491
// Handle some differences in type of the len arg across platforms.
492492
cfg_if! {
493493
if #[cfg(any(target_os = "dragonfly",
@@ -499,8 +499,8 @@ pub fn sethostname(name: &str) -> Result<()> {
499499
type sethostname_len_t = size_t;
500500
}
501501
}
502-
let ptr = name.as_ptr() as *const c_char;
503-
let len = name.len() as sethostname_len_t;
502+
let ptr = name.as_ref().as_bytes().as_ptr() as *const c_char;
503+
let len = name.as_ref().len() as sethostname_len_t;
504504

505505
let res = unsafe { libc::sethostname(ptr, len) };
506506
Errno::result(res).map(drop)

0 commit comments

Comments
 (0)