Skip to content

std: Stabilize portions of std::os::$platform #23353

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 1 commit into from
Mar 15, 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
8 changes: 4 additions & 4 deletions src/libstd/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub fn getenv_as_bytes(n: &str) -> Option<Vec<u8>> {

#[cfg(unix)]
fn byteify(s: OsString) -> Vec<u8> {
use os::unix::*;
use os::unix::prelude::*;
s.into_vec()
}
#[cfg(windows)]
Expand Down Expand Up @@ -238,7 +238,7 @@ fn byteify(s: OsString) -> Vec<u8> {
pub fn setenv<T: BytesContainer>(n: &str, v: T) {
#[cfg(unix)]
fn _setenv(n: &str, v: &[u8]) {
use os::unix::*;
use os::unix::prelude::*;
let v: OsString = OsStringExt::from_vec(v.to_vec());
env::set_var(n, &v)
}
Expand Down Expand Up @@ -1705,13 +1705,13 @@ mod tests {

#[cfg(not(windows))]
fn get_fd(file: &File) -> libc::c_int {
use os::unix::AsRawFd;
use os::unix::prelude::*;
file.as_raw_fd()
}

#[cfg(windows)]
fn get_fd(file: &File) -> libc::HANDLE {
use os::windows::AsRawHandle;
use os::windows::prelude::*;
file.as_raw_handle()
}

Expand Down
6 changes: 3 additions & 3 deletions src/libstd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ mod tests {
#[cfg(all(unix, not(target_os="android")))]
#[test]
fn signal_reported_right() {
use os::unix::ExitStatusExt;
use os::unix::process::ExitStatusExt;

let p = Command::new("/bin/sh").arg("-c").arg("kill -9 $$").spawn();
assert!(p.is_ok());
Expand Down Expand Up @@ -633,7 +633,7 @@ mod tests {
#[cfg(all(unix, not(target_os="android")))]
#[test]
fn uid_works() {
use os::unix::*;
use os::unix::prelude::*;
use libc;
let mut p = Command::new("/bin/sh")
.arg("-c").arg("true")
Expand All @@ -646,7 +646,7 @@ mod tests {
#[cfg(all(unix, not(target_os="android")))]
#[test]
fn uid_to_root_fails() {
use os::unix::*;
use os::unix::prelude::*;
use libc;

// if we're already root, this isn't a valid test. Most of the bots run
Expand Down
85 changes: 65 additions & 20 deletions src/libstd/sys/unix/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,18 @@
//! }
//! ```

#![unstable(feature = "std_misc")]

use prelude::v1::*;

use ffi::{CString, NulError, OsStr, OsString};
use fs::{self, Permissions, OpenOptions};
use net;
use mem;
use process;
use sys;
use sys::os_str::Buf;
use sys_common::{AsInner, AsInnerMut, IntoInner, FromInner};
use libc::{self, gid_t, uid_t};
#![stable(feature = "rust1", since = "1.0.0")]

/// Unix-specific extensions to general I/O primitives
#[unstable(feature = "io_ext",
reason = "may want a slightly different organization or a more \
general file descriptor primitive")]
pub mod io {
#[allow(deprecated)] use old_io;
use fs;
use libc;
use net;
use sys_common::AsInner;

/// Raw file descriptors.
pub type Fd = libc::c_int;
Expand Down Expand Up @@ -132,55 +129,80 @@ impl AsRawFd for net::TcpListener {
impl AsRawFd for net::UdpSocket {
fn as_raw_fd(&self) -> Fd { *self.as_inner().socket().as_inner() }
}
}

////////////////////////////////////////////////////////////////////////////////
// OsString and OsStr
////////////////////////////////////////////////////////////////////////////////

/// Unix-specific extension to the primitives in the `std::ffi` module
#[stable(feature = "rust1", since = "1.0.0")]
pub mod ffi {
use ffi::{CString, NulError, OsStr, OsString};
use mem;
use prelude::v1::*;
use sys::os_str::Buf;
use sys_common::{FromInner, IntoInner, AsInner};

/// Unix-specific extensions to `OsString`.
#[stable(feature = "rust1", since = "1.0.0")]
pub trait OsStringExt {
/// Create an `OsString` from a byte vector.
#[stable(feature = "rust1", since = "1.0.0")]
fn from_vec(vec: Vec<u8>) -> Self;

/// Yield the underlying byte vector of this `OsString`.
#[stable(feature = "rust1", since = "1.0.0")]
fn into_vec(self) -> Vec<u8>;
}

#[stable(feature = "rust1", since = "1.0.0")]
impl OsStringExt for OsString {
fn from_vec(vec: Vec<u8>) -> OsString {
FromInner::from_inner(Buf { inner: vec })
}

fn into_vec(self) -> Vec<u8> {
self.into_inner().inner
}
}

/// Unix-specific extensions to `OsStr`.
#[stable(feature = "rust1", since = "1.0.0")]
pub trait OsStrExt {
fn from_bytes(slice: &[u8]) -> &OsStr;
#[stable(feature = "rust1", since = "1.0.0")]
fn from_bytes(slice: &[u8]) -> &Self;

/// Get the underlying byte view of the `OsStr` slice.
#[stable(feature = "rust1", since = "1.0.0")]
fn as_bytes(&self) -> &[u8];

/// Convert the `OsStr` slice into a `CString`.
#[stable(feature = "rust1", since = "1.0.0")]
fn to_cstring(&self) -> Result<CString, NulError>;
}

#[stable(feature = "rust1", since = "1.0.0")]
impl OsStrExt for OsStr {
fn from_bytes(slice: &[u8]) -> &OsStr {
unsafe { mem::transmute(slice) }
}
fn as_bytes(&self) -> &[u8] {
&self.as_inner().inner
}

fn to_cstring(&self) -> Result<CString, NulError> {
CString::new(self.as_bytes())
}
}
}

/// Unix-specific extensions to primitives in the `std::fs` module.
#[unstable(feature = "fs_ext",
reason = "may want a more useful mode abstraction")]
pub mod fs {
use sys_common::{FromInner, AsInner, AsInnerMut};
use fs::{Permissions, OpenOptions};

// Unix-specific extensions to `Permissions`
/// Unix-specific extensions to `Permissions`
pub trait PermissionsExt {
fn mode(&self) -> i32;
fn set_mode(&mut self, mode: i32);
Expand All @@ -194,7 +216,7 @@ impl PermissionsExt for Permissions {
}
}

// Unix-specific extensions to `OpenOptions`
/// Unix-specific extensions to `OpenOptions`
pub trait OpenOptionsExt {
/// Set the mode bits that a new file will be created with.
///
Expand All @@ -208,23 +230,37 @@ impl OpenOptionsExt for OpenOptions {
self.as_inner_mut().mode(mode); self
}
}
}

////////////////////////////////////////////////////////////////////////////////
// Process and Command
////////////////////////////////////////////////////////////////////////////////

/// Unix-specific extensions to primitives in the `std::process` module.
#[stable(feature = "rust1", since = "1.0.0")]
pub mod process {
use prelude::v1::*;
use libc::{uid_t, gid_t};
use process;
use sys;
use sys_common::{AsInnerMut, AsInner};

/// Unix-specific extensions to the `std::process::Command` builder
#[stable(feature = "rust1", since = "1.0.0")]
pub trait CommandExt {
/// Sets the child process's user id. This translates to a
/// `setuid` call in the child process. Failure in the `setuid`
/// call will cause the spawn to fail.
#[stable(feature = "rust1", since = "1.0.0")]
fn uid(&mut self, id: uid_t) -> &mut process::Command;

/// Similar to `uid`, but sets the group id of the child process. This has
/// the same semantics as the `uid` field.
#[stable(feature = "rust1", since = "1.0.0")]
fn gid(&mut self, id: gid_t) -> &mut process::Command;
}

#[stable(feature = "rust1", since = "1.0.0")]
impl CommandExt for process::Command {
fn uid(&mut self, id: uid_t) -> &mut process::Command {
self.as_inner_mut().uid = Some(id);
Expand All @@ -238,11 +274,14 @@ impl CommandExt for process::Command {
}

/// Unix-specific extensions to `std::process::ExitStatus`
#[stable(feature = "rust1", since = "1.0.0")]
pub trait ExitStatusExt {
/// If the process was terminated by a signal, returns that signal.
#[stable(feature = "rust1", since = "1.0.0")]
fn signal(&self) -> Option<i32>;
}

#[stable(feature = "rust1", since = "1.0.0")]
impl ExitStatusExt for process::ExitStatus {
fn signal(&self) -> Option<i32> {
match *self.as_inner() {
Expand All @@ -251,6 +290,7 @@ impl ExitStatusExt for process::ExitStatus {
}
}
}
}

////////////////////////////////////////////////////////////////////////////////
// Prelude
Expand All @@ -259,9 +299,14 @@ impl ExitStatusExt for process::ExitStatus {
/// A prelude for conveniently writing platform-specific code.
///
/// Includes all extension traits, and some important type definitions.
#[stable(feature = "rust1", since = "1.0.0")]
pub mod prelude {
#[doc(no_inline)]
pub use super::{Fd, AsRawFd, OsStrExt, OsStringExt, PermissionsExt};
pub use super::io::{Fd, AsRawFd};
#[doc(no_inline)] #[stable(feature = "rust1", since = "1.0.0")]
pub use super::ffi::{OsStrExt, OsStringExt};
#[doc(no_inline)]
pub use super::{CommandExt, ExitStatusExt};
pub use super::fs::{PermissionsExt, OpenOptionsExt};
#[doc(no_inline)] #[stable(feature = "rust1", since = "1.0.0")]
pub use super::process::{CommandExt, ExitStatusExt};
}
2 changes: 1 addition & 1 deletion src/libstd/sys/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#![allow(unused_imports)] // lots of cfg code here

use prelude::v1::*;
use os::unix::*;
use os::unix::prelude::*;

use error::Error as StdError;
use ffi::{CString, CStr, OsString, OsStr, AsOsStr};
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/unix/process2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

use prelude::v1::*;
use os::unix::prelude::*;

use collections::HashMap;
use env;
Expand All @@ -17,7 +18,6 @@ use fmt;
use io::{self, Error, ErrorKind};
use libc::{self, pid_t, c_void, c_int, gid_t, uid_t};
use mem;
use os::unix::OsStrExt;
use ptr;
use sys::pipe2::AnonPipe;
use sys::{self, retry, c, cvt};
Expand Down
Loading