Skip to content

Commit

Permalink
Auto merge of #32302 - sfackler:unix-socket, r=alexcrichton
Browse files Browse the repository at this point in the history
Add unix socket support to the standard library

r? @alexcrichton
  • Loading branch information
bors committed Mar 21, 2016
2 parents 7f5c568 + c0d989e commit 6c4b1f1
Show file tree
Hide file tree
Showing 7 changed files with 1,141 additions and 55 deletions.
37 changes: 3 additions & 34 deletions src/libstd/fs.rs
Expand Up @@ -1457,12 +1457,12 @@ mod tests {
use prelude::v1::*;
use io::prelude::*;

use env;
use fs::{self, File, OpenOptions};
use io::{ErrorKind, SeekFrom};
use path::{Path, PathBuf};
use rand::{self, StdRng, Rng};
use path::Path;
use rand::{StdRng, Rng};
use str;
use sys_common::io::test::{TempDir, tmpdir};

#[cfg(windows)]
use os::windows::fs::{symlink_dir, symlink_file};
Expand Down Expand Up @@ -1490,37 +1490,6 @@ mod tests {
}
) }

pub struct TempDir(PathBuf);

impl TempDir {
fn join(&self, path: &str) -> PathBuf {
let TempDir(ref p) = *self;
p.join(path)
}

fn path<'a>(&'a self) -> &'a Path {
let TempDir(ref p) = *self;
p
}
}

impl Drop for TempDir {
fn drop(&mut self) {
// Gee, seeing how we're testing the fs module I sure hope that we
// at least implement this correctly!
let TempDir(ref p) = *self;
check!(fs::remove_dir_all(p));
}
}

pub fn tmpdir() -> TempDir {
let p = env::temp_dir();
let mut r = rand::thread_rng();
let ret = p.join(&format!("rust-{}", r.next_u32()));
check!(fs::create_dir(&ret));
TempDir(ret)
}

// Several test fail on windows if the user does not have permission to
// create symlinks (the `SeCreateSymbolicLinkPrivilege`). Instead of
// disabling these test on Windows, use this function to test whether we
Expand Down
43 changes: 41 additions & 2 deletions src/libstd/sys/common/io.rs
Expand Up @@ -51,14 +51,53 @@ pub unsafe fn read_to_end_uninitialized(r: &mut Read, buf: &mut Vec<u8>) -> io::
}
}

#[cfg(test)]
pub mod test {
use prelude::v1::*;
use path::{Path, PathBuf};
use env;
use rand::{self, Rng};
use fs;

pub struct TempDir(PathBuf);

impl TempDir {
pub fn join(&self, path: &str) -> PathBuf {
let TempDir(ref p) = *self;
p.join(path)
}

pub fn path<'a>(&'a self) -> &'a Path {
let TempDir(ref p) = *self;
p
}
}

impl Drop for TempDir {
fn drop(&mut self) {
// Gee, seeing how we're testing the fs module I sure hope that we
// at least implement this correctly!
let TempDir(ref p) = *self;
fs::remove_dir_all(p).unwrap();
}
}

pub fn tmpdir() -> TempDir {
let p = env::temp_dir();
let mut r = rand::thread_rng();
let ret = p.join(&format!("rust-{}", r.next_u32()));
fs::create_dir(&ret).unwrap();
TempDir(ret)
}
}

#[cfg(test)]
mod tests {
use prelude::v1::*;
use io::prelude::*;
use super::*;
use io;
use io::{ErrorKind, Take, Repeat, repeat};
use test;
use slice::from_raw_parts;

struct ErrorRepeat {
Expand Down Expand Up @@ -129,7 +168,7 @@ mod tests {
}

#[bench]
fn bench_uninitialized(b: &mut test::Bencher) {
fn bench_uninitialized(b: &mut ::test::Bencher) {
b.iter(|| {
let mut lr = repeat(1).take(10000000);
let mut vec = Vec::with_capacity(1024);
Expand Down
21 changes: 3 additions & 18 deletions src/libstd/sys/common/net.rs
Expand Up @@ -257,12 +257,7 @@ impl TcpStream {
}

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

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

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

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

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

pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
Expand Down
1 change: 1 addition & 0 deletions src/libstd/sys/unix/ext/mod.rs
Expand Up @@ -35,6 +35,7 @@ pub mod fs;
pub mod process;
pub mod raw;
pub mod thread;
pub mod net;

/// A prelude for conveniently writing platform-specific code.
///
Expand Down

0 comments on commit 6c4b1f1

Please sign in to comment.