1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Experimental extensions to `std` for Unix platforms.
13 //! For now, this module is limited to extracting file descriptors,
14 //! but its functionality will grow over time.
19 //! #![feature(globs)]
21 //! use std::old_io::fs::File;
22 //! use std::os::unix::prelude::*;
25 //! let f = File::create(&Path::new("foo.txt")).unwrap();
26 //! let fd = f.as_raw_fd();
28 //! // use fd with native unix bindings
32 #![stable(feature = "rust1", since = "1.0.0")]
34 /// Unix-specific extensions to general I/O primitives
35 #[unstable(feature = "io_ext",
36 reason = "may want a slightly different organization or a more \
37 general file descriptor primitive")]
39 #[allow(deprecated)] use old_io;
43 use sys_common::AsInner;
45 /// Raw file descriptors.
46 pub type Fd = libc::c_int;
48 /// Extract raw file descriptor
50 /// Extract the raw file descriptor, without taking any ownership.
51 fn as_raw_fd(&self) -> Fd;
55 impl AsRawFd for old_io::fs::File {
56 fn as_raw_fd(&self) -> Fd {
61 impl AsRawFd for fs::File {
62 fn as_raw_fd(&self) -> Fd {
63 self.as_inner().fd().raw()
68 impl AsRawFd for old_io::pipe::PipeStream {
69 fn as_raw_fd(&self) -> Fd {
75 impl AsRawFd for old_io::net::pipe::UnixStream {
76 fn as_raw_fd(&self) -> Fd {
82 impl AsRawFd for old_io::net::pipe::UnixListener {
83 fn as_raw_fd(&self) -> Fd {
89 impl AsRawFd for old_io::net::pipe::UnixAcceptor {
90 fn as_raw_fd(&self) -> Fd {
96 impl AsRawFd for old_io::net::tcp::TcpStream {
97 fn as_raw_fd(&self) -> Fd {
103 impl AsRawFd for old_io::net::tcp::TcpListener {
104 fn as_raw_fd(&self) -> Fd {
110 impl AsRawFd for old_io::net::tcp::TcpAcceptor {
111 fn as_raw_fd(&self) -> Fd {
117 impl AsRawFd for old_io::net::udp::UdpSocket {
118 fn as_raw_fd(&self) -> Fd {
123 impl AsRawFd for net::TcpStream {
124 fn as_raw_fd(&self) -> Fd { *self.as_inner().socket().as_inner() }
126 impl AsRawFd for net::TcpListener {
127 fn as_raw_fd(&self) -> Fd { *self.as_inner().socket().as_inner() }
129 impl AsRawFd for net::UdpSocket {
130 fn as_raw_fd(&self) -> Fd { *self.as_inner().socket().as_inner() }
134 ////////////////////////////////////////////////////////////////////////////////
135 // OsString and OsStr
136 ////////////////////////////////////////////////////////////////////////////////
138 /// Unix-specific extension to the primitives in the `std::ffi` module
139 #[stable(feature = "rust1", since = "1.0.0")]
141 use ffi::{OsStr, OsString};
144 use sys::os_str::Buf;
145 use sys_common::{FromInner, IntoInner, AsInner};
147 /// Unix-specific extensions to `OsString`.
148 #[stable(feature = "rust1", since = "1.0.0")]
149 pub trait OsStringExt {
150 /// Create an `OsString` from a byte vector.
151 #[stable(feature = "rust1", since = "1.0.0")]
152 fn from_vec(vec: Vec<u8>) -> Self;
154 /// Yield the underlying byte vector of this `OsString`.
155 #[stable(feature = "rust1", since = "1.0.0")]
156 fn into_vec(self) -> Vec<u8>;
159 #[stable(feature = "rust1", since = "1.0.0")]
160 impl OsStringExt for OsString {
161 fn from_vec(vec: Vec<u8>) -> OsString {
162 FromInner::from_inner(Buf { inner: vec })
164 fn into_vec(self) -> Vec<u8> {
165 self.into_inner().inner
169 /// Unix-specific extensions to `OsStr`.
170 #[stable(feature = "rust1", since = "1.0.0")]
172 #[stable(feature = "rust1", since = "1.0.0")]
173 fn from_bytes(slice: &[u8]) -> &Self;
175 /// Get the underlying byte view of the `OsStr` slice.
176 #[stable(feature = "rust1", since = "1.0.0")]
177 fn as_bytes(&self) -> &[u8];
180 #[stable(feature = "rust1", since = "1.0.0")]
181 impl OsStrExt for OsStr {
182 fn from_bytes(slice: &[u8]) -> &OsStr {
183 unsafe { mem::transmute(slice) }
185 fn as_bytes(&self) -> &[u8] {
186 &self.as_inner().inner
191 /// Unix-specific extensions to primitives in the `std::fs` module.
192 #[unstable(feature = "fs_ext",
193 reason = "may want a more useful mode abstraction")]
195 use sys_common::{FromInner, AsInner, AsInnerMut};
196 use fs::{Permissions, OpenOptions};
198 /// Unix-specific extensions to `Permissions`
199 pub trait PermissionsExt {
200 fn mode(&self) -> i32;
201 fn set_mode(&mut self, mode: i32);
204 impl PermissionsExt for Permissions {
205 fn mode(&self) -> i32 { self.as_inner().mode() }
207 fn set_mode(&mut self, mode: i32) {
208 *self = FromInner::from_inner(FromInner::from_inner(mode));
212 /// Unix-specific extensions to `OpenOptions`
213 pub trait OpenOptionsExt {
214 /// Set the mode bits that a new file will be created with.
216 /// If a new file is created as part of a `File::open_opts` call then this
217 /// specified `mode` will be used as the permission bits for the new file.
218 fn mode(&mut self, mode: i32) -> &mut Self;
221 impl OpenOptionsExt for OpenOptions {
222 fn mode(&mut self, mode: i32) -> &mut OpenOptions {
223 self.as_inner_mut().mode(mode); self
228 ////////////////////////////////////////////////////////////////////////////////
229 // Process and Command
230 ////////////////////////////////////////////////////////////////////////////////
232 /// Unix-specific extensions to primitives in the `std::process` module.
233 #[stable(feature = "rust1", since = "1.0.0")]
236 use libc::{uid_t, gid_t};
239 use sys_common::{AsInnerMut, AsInner};
241 /// Unix-specific extensions to the `std::process::Command` builder
242 #[stable(feature = "rust1", since = "1.0.0")]
243 pub trait CommandExt {
244 /// Sets the child process's user id. This translates to a
245 /// `setuid` call in the child process. Failure in the `setuid`
246 /// call will cause the spawn to fail.
247 #[stable(feature = "rust1", since = "1.0.0")]
248 fn uid(&mut self, id: uid_t) -> &mut process::Command;
250 /// Similar to `uid`, but sets the group id of the child process. This has
251 /// the same semantics as the `uid` field.
252 #[stable(feature = "rust1", since = "1.0.0")]
253 fn gid(&mut self, id: gid_t) -> &mut process::Command;
256 #[stable(feature = "rust1", since = "1.0.0")]
257 impl CommandExt for process::Command {
258 fn uid(&mut self, id: uid_t) -> &mut process::Command {
259 self.as_inner_mut().uid = Some(id);
263 fn gid(&mut self, id: gid_t) -> &mut process::Command {
264 self.as_inner_mut().gid = Some(id);
269 /// Unix-specific extensions to `std::process::ExitStatus`
270 #[stable(feature = "rust1", since = "1.0.0")]
271 pub trait ExitStatusExt {
272 /// If the process was terminated by a signal, returns that signal.
273 #[stable(feature = "rust1", since = "1.0.0")]
274 fn signal(&self) -> Option<i32>;
277 #[stable(feature = "rust1", since = "1.0.0")]
278 impl ExitStatusExt for process::ExitStatus {
279 fn signal(&self) -> Option<i32> {
280 match *self.as_inner() {
281 sys::process2::ExitStatus::Signal(s) => Some(s),
288 ////////////////////////////////////////////////////////////////////////////////
290 ////////////////////////////////////////////////////////////////////////////////
292 /// A prelude for conveniently writing platform-specific code.
294 /// Includes all extension traits, and some important type definitions.
295 #[stable(feature = "rust1", since = "1.0.0")]
298 pub use super::io::{Fd, AsRawFd};
299 #[doc(no_inline)] #[stable(feature = "rust1", since = "1.0.0")]
300 pub use super::ffi::{OsStrExt, OsStringExt};
302 pub use super::fs::{PermissionsExt, OpenOptionsExt};
303 #[doc(no_inline)] #[stable(feature = "rust1", since = "1.0.0")]
304 pub use super::process::{CommandExt, ExitStatusExt};