Skip to content

Commit af0c446

Browse files
committed
libs: add std::os::unix module
The new `std::os::unix` module exposes several extension traits for extracting file descriptors from `std::io` types.
1 parent 4156bc4 commit af0c446

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

src/libstd/sys/unix/ext.rs

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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.
4+
//
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.
10+
11+
//! Experimental extensions to `std` for Unix platforms.
12+
//!
13+
//! For now, this module is limited to extracting file descriptors,
14+
//! but its functionality will grow over time.
15+
//!
16+
//! # Example
17+
//!
18+
//! ```rust,ignore
19+
//! #![feature(globs)]
20+
//!
21+
//! use std::io::fs::File;
22+
//! use std::os::unix::prelude::*;
23+
//!
24+
//! fn main() {
25+
//! let f = File::create(&Path::new("foo.txt")).unwrap();
26+
//! let fd = f.as_raw_fd();
27+
//!
28+
//! // use fd with native unix bindings
29+
//! }
30+
//! ```
31+
32+
#![experimental]
33+
34+
use sys_common::AsInner;
35+
use libc;
36+
37+
use io;
38+
39+
/// Raw file descriptors.
40+
pub type Fd = libc::c_int;
41+
42+
/// Extract raw file descriptor
43+
pub trait AsRawFd {
44+
/// Extract the raw file descriptor, without taking any ownership.
45+
fn as_raw_fd(&self) -> Fd;
46+
}
47+
48+
impl AsRawFd for io::fs::File {
49+
fn as_raw_fd(&self) -> Fd {
50+
self.as_inner().fd()
51+
}
52+
}
53+
54+
impl AsRawFd for io::pipe::PipeStream {
55+
fn as_raw_fd(&self) -> Fd {
56+
self.as_inner().fd()
57+
}
58+
}
59+
60+
impl AsRawFd for io::net::pipe::UnixStream {
61+
fn as_raw_fd(&self) -> Fd {
62+
self.as_inner().fd()
63+
}
64+
}
65+
66+
impl AsRawFd for io::net::pipe::UnixListener {
67+
fn as_raw_fd(&self) -> Fd {
68+
self.as_inner().fd()
69+
}
70+
}
71+
72+
impl AsRawFd for io::net::pipe::UnixAcceptor {
73+
fn as_raw_fd(&self) -> Fd {
74+
self.as_inner().fd()
75+
}
76+
}
77+
78+
impl AsRawFd for io::net::tcp::TcpStream {
79+
fn as_raw_fd(&self) -> Fd {
80+
self.as_inner().fd()
81+
}
82+
}
83+
84+
impl AsRawFd for io::net::tcp::TcpListener {
85+
fn as_raw_fd(&self) -> Fd {
86+
self.as_inner().fd()
87+
}
88+
}
89+
90+
impl AsRawFd for io::net::tcp::TcpAcceptor {
91+
fn as_raw_fd(&self) -> Fd {
92+
self.as_inner().fd()
93+
}
94+
}
95+
96+
impl AsRawFd for io::net::udp::UdpSocket {
97+
fn as_raw_fd(&self) -> Fd {
98+
self.as_inner().fd()
99+
}
100+
}
101+
102+
/// A prelude for conveniently writing platform-specific code.
103+
///
104+
/// Includes all extension traits, and some important type definitions.
105+
pub mod prelude {
106+
pub use super::{Fd, AsRawFd};
107+
}

src/libstd/sys/unix/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ macro_rules! helper_init( (static $name:ident: Helper<$m:ty>) => (
3333
) )
3434

3535
pub mod c;
36+
pub mod ext;
3637
pub mod fs;
3738
pub mod os;
3839
pub mod tcp;

0 commit comments

Comments
 (0)