Skip to content

Commit 6c4b1f1

Browse files
committed
Auto merge of #32302 - sfackler:unix-socket, r=alexcrichton
Add unix socket support to the standard library r? @alexcrichton
2 parents 7f5c568 + c0d989e commit 6c4b1f1

File tree

7 files changed

+1141
-55
lines changed

7 files changed

+1141
-55
lines changed

Diff for: src/libstd/fs.rs

+3-34
Original file line numberDiff line numberDiff line change
@@ -1457,12 +1457,12 @@ mod tests {
14571457
use prelude::v1::*;
14581458
use io::prelude::*;
14591459

1460-
use env;
14611460
use fs::{self, File, OpenOptions};
14621461
use io::{ErrorKind, SeekFrom};
1463-
use path::{Path, PathBuf};
1464-
use rand::{self, StdRng, Rng};
1462+
use path::Path;
1463+
use rand::{StdRng, Rng};
14651464
use str;
1465+
use sys_common::io::test::{TempDir, tmpdir};
14661466

14671467
#[cfg(windows)]
14681468
use os::windows::fs::{symlink_dir, symlink_file};
@@ -1490,37 +1490,6 @@ mod tests {
14901490
}
14911491
) }
14921492

1493-
pub struct TempDir(PathBuf);
1494-
1495-
impl TempDir {
1496-
fn join(&self, path: &str) -> PathBuf {
1497-
let TempDir(ref p) = *self;
1498-
p.join(path)
1499-
}
1500-
1501-
fn path<'a>(&'a self) -> &'a Path {
1502-
let TempDir(ref p) = *self;
1503-
p
1504-
}
1505-
}
1506-
1507-
impl Drop for TempDir {
1508-
fn drop(&mut self) {
1509-
// Gee, seeing how we're testing the fs module I sure hope that we
1510-
// at least implement this correctly!
1511-
let TempDir(ref p) = *self;
1512-
check!(fs::remove_dir_all(p));
1513-
}
1514-
}
1515-
1516-
pub fn tmpdir() -> TempDir {
1517-
let p = env::temp_dir();
1518-
let mut r = rand::thread_rng();
1519-
let ret = p.join(&format!("rust-{}", r.next_u32()));
1520-
check!(fs::create_dir(&ret));
1521-
TempDir(ret)
1522-
}
1523-
15241493
// Several test fail on windows if the user does not have permission to
15251494
// create symlinks (the `SeCreateSymbolicLinkPrivilege`). Instead of
15261495
// disabling these test on Windows, use this function to test whether we

Diff for: src/libstd/sys/common/io.rs

+41-2
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,53 @@ pub unsafe fn read_to_end_uninitialized(r: &mut Read, buf: &mut Vec<u8>) -> io::
5151
}
5252
}
5353

54+
#[cfg(test)]
55+
pub mod test {
56+
use prelude::v1::*;
57+
use path::{Path, PathBuf};
58+
use env;
59+
use rand::{self, Rng};
60+
use fs;
61+
62+
pub struct TempDir(PathBuf);
63+
64+
impl TempDir {
65+
pub fn join(&self, path: &str) -> PathBuf {
66+
let TempDir(ref p) = *self;
67+
p.join(path)
68+
}
69+
70+
pub fn path<'a>(&'a self) -> &'a Path {
71+
let TempDir(ref p) = *self;
72+
p
73+
}
74+
}
75+
76+
impl Drop for TempDir {
77+
fn drop(&mut self) {
78+
// Gee, seeing how we're testing the fs module I sure hope that we
79+
// at least implement this correctly!
80+
let TempDir(ref p) = *self;
81+
fs::remove_dir_all(p).unwrap();
82+
}
83+
}
84+
85+
pub fn tmpdir() -> TempDir {
86+
let p = env::temp_dir();
87+
let mut r = rand::thread_rng();
88+
let ret = p.join(&format!("rust-{}", r.next_u32()));
89+
fs::create_dir(&ret).unwrap();
90+
TempDir(ret)
91+
}
92+
}
93+
5494
#[cfg(test)]
5595
mod tests {
5696
use prelude::v1::*;
5797
use io::prelude::*;
5898
use super::*;
5999
use io;
60100
use io::{ErrorKind, Take, Repeat, repeat};
61-
use test;
62101
use slice::from_raw_parts;
63102

64103
struct ErrorRepeat {
@@ -129,7 +168,7 @@ mod tests {
129168
}
130169

131170
#[bench]
132-
fn bench_uninitialized(b: &mut test::Bencher) {
171+
fn bench_uninitialized(b: &mut ::test::Bencher) {
133172
b.iter(|| {
134173
let mut lr = repeat(1).take(10000000);
135174
let mut vec = Vec::with_capacity(1024);

Diff for: src/libstd/sys/common/net.rs

+3-18
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,7 @@ impl TcpStream {
257257
}
258258

259259
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
260-
let raw: c_int = try!(getsockopt(&self.inner, c::SOL_SOCKET, c::SO_ERROR));
261-
if raw == 0 {
262-
Ok(None)
263-
} else {
264-
Ok(Some(io::Error::from_raw_os_error(raw as i32)))
265-
}
260+
self.inner.take_error()
266261
}
267262

268263
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
@@ -367,12 +362,7 @@ impl TcpListener {
367362
}
368363

369364
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
370-
let raw: c_int = try!(getsockopt(&self.inner, c::SOL_SOCKET, c::SO_ERROR));
371-
if raw == 0 {
372-
Ok(None)
373-
} else {
374-
Ok(Some(io::Error::from_raw_os_error(raw as i32)))
375-
}
365+
self.inner.take_error()
376366
}
377367

378368
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
@@ -564,12 +554,7 @@ impl UdpSocket {
564554
}
565555

566556
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
567-
let raw: c_int = try!(getsockopt(&self.inner, c::SOL_SOCKET, c::SO_ERROR));
568-
if raw == 0 {
569-
Ok(None)
570-
} else {
571-
Ok(Some(io::Error::from_raw_os_error(raw as i32)))
572-
}
557+
self.inner.take_error()
573558
}
574559

575560
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {

Diff for: src/libstd/sys/unix/ext/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub mod fs;
3535
pub mod process;
3636
pub mod raw;
3737
pub mod thread;
38+
pub mod net;
3839

3940
/// A prelude for conveniently writing platform-specific code.
4041
///

0 commit comments

Comments
 (0)