Skip to content
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

Add unix socket support to the standard library #32302

Merged
merged 1 commit into from
Mar 21, 2016
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
37 changes: 3 additions & 34 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading