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

Format code using 'cargo fmt' #23

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 17 additions & 17 deletions src/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::path::Path;
use libc;
use mio::event::Evented;
use mio::unix::EventedFd;
use mio::{Poll, Token, Ready, PollOpt};
use mio::{Poll, PollOpt, Ready, Token};

use cvt;
use socket::{sockaddr_un, Socket};
Expand Down Expand Up @@ -52,8 +52,10 @@ impl UnixDatagram {
pub fn pair() -> io::Result<(UnixDatagram, UnixDatagram)> {
unsafe {
let (a, b) = try!(Socket::pair(libc::SOCK_DGRAM));
Ok((UnixDatagram::from_raw_fd(a.into_fd()),
UnixDatagram::from_raw_fd(b.into_fd())))
Ok((
UnixDatagram::from_raw_fd(a.into_fd()),
UnixDatagram::from_raw_fd(b.into_fd()),
))
}
}

Expand All @@ -78,9 +80,7 @@ impl UnixDatagram {
/// object references. Both handles can be used to accept incoming
/// connections and options set on one listener will affect the other.
pub fn try_clone(&self) -> io::Result<UnixDatagram> {
self.inner.try_clone().map(|i| {
UnixDatagram { inner: i }
})
self.inner.try_clone().map(|i| UnixDatagram { inner: i })
}

/// Returns the address of this socket.
Expand Down Expand Up @@ -143,19 +143,17 @@ impl UnixDatagram {
}

impl Evented for UnixDatagram {
fn register(&self,
poll: &Poll,
token: Token,
events: Ready,
opts: PollOpt) -> io::Result<()> {
fn register(&self, poll: &Poll, token: Token, events: Ready, opts: PollOpt) -> io::Result<()> {
EventedFd(&self.as_raw_fd()).register(poll, token, events, opts)
}

fn reregister(&self,
poll: &Poll,
token: Token,
events: Ready,
opts: PollOpt) -> io::Result<()> {
fn reregister(
&self,
poll: &Poll,
token: Token,
events: Ready,
opts: PollOpt,
) -> io::Result<()> {
EventedFd(&self.as_raw_fd()).reregister(poll, token, events, opts)
}

Expand All @@ -178,6 +176,8 @@ impl IntoRawFd for UnixDatagram {

impl FromRawFd for UnixDatagram {
unsafe fn from_raw_fd(fd: i32) -> UnixDatagram {
UnixDatagram { inner: net::UnixDatagram::from_raw_fd(fd) }
UnixDatagram {
inner: net::UnixDatagram::from_raw_fd(fd),
}
}
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ mod listener;
mod socket;
mod stream;

pub use stream::UnixStream;
pub use listener::UnixListener;
pub use datagram::UnixDatagram;
pub use listener::UnixListener;
pub use stream::UnixStream;

fn cvt(i: libc::c_int) -> io::Result<libc::c_int> {
if i == -1 {
Expand Down
2 changes: 1 addition & 1 deletion src/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use mio::event::Evented;
use mio::unix::EventedFd;
use mio::{Poll, PollOpt, Ready, Token};

use UnixStream;
use cvt;
use socket::{sockaddr_un, Socket};
use UnixStream;

/// A structure representing a Unix domain socket server.
///
Expand Down
33 changes: 20 additions & 13 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ impl Socket {
}
}

let fd = Socket { fd: try!(cvt(libc::socket(libc::AF_UNIX, ty, 0))) };
let fd = Socket {
fd: try!(cvt(libc::socket(libc::AF_UNIX, ty, 0))),
};
try!(cvt(libc::ioctl(fd.fd, libc::FIOCLEX)));
let mut nonblocking = 1 as c_ulong;
try!(cvt(libc::ioctl(fd.fd, libc::FIONBIO, &mut nonblocking)));
Expand All @@ -57,15 +59,18 @@ impl Socket {
if cfg!(target_os = "linux") || cfg!(target_os = "android") {
let flags = ty | SOCK_CLOEXEC | SOCK_NONBLOCK;
match cvt(libc::socketpair(libc::AF_UNIX, flags, 0, fds.as_mut_ptr())) {
Ok(_) => {
return Ok((Socket { fd: fds[0] }, Socket { fd: fds[1] }))
}
Err(ref e) if e.raw_os_error() == Some(libc::EINVAL) => {},
Ok(_) => return Ok((Socket { fd: fds[0] }, Socket { fd: fds[1] })),
Err(ref e) if e.raw_os_error() == Some(libc::EINVAL) => {}
Err(e) => return Err(e),
}
}

try!(cvt(libc::socketpair(libc::AF_UNIX, ty, 0, fds.as_mut_ptr())));
try!(cvt(libc::socketpair(
libc::AF_UNIX,
ty,
0,
fds.as_mut_ptr()
)));
let a = Socket { fd: fds[0] };
let b = Socket { fd: fds[1] };
try!(cvt(libc::ioctl(a.fd, libc::FIOCLEX)));
Expand Down Expand Up @@ -96,8 +101,7 @@ impl Drop for Socket {
}
}

pub unsafe fn sockaddr_un(path: &Path)
-> io::Result<(libc::sockaddr_un, libc::socklen_t)> {
pub unsafe fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un, libc::socklen_t)> {
let mut addr: libc::sockaddr_un = mem::zeroed();
addr.sun_family = libc::AF_UNIX as libc::sa_family_t;

Expand All @@ -106,12 +110,16 @@ pub unsafe fn sockaddr_un(path: &Path)
match (bytes.get(0), bytes.len().cmp(&addr.sun_path.len())) {
// Abstract paths don't need a null terminator
(Some(&0), Ordering::Greater) => {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"path must be no longer than SUN_LEN"));
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"path must be no longer than SUN_LEN",
));
}
(_, Ordering::Greater) | (_, Ordering::Equal) => {
return Err(io::Error::new(io::ErrorKind::InvalidInput,
"path must be shorter than SUN_LEN"));
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"path must be shorter than SUN_LEN",
));
}
_ => {}
}
Expand All @@ -138,4 +146,3 @@ fn sun_path_offset() -> usize {
path - base
}
}

48 changes: 22 additions & 26 deletions src/stream.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::cmp;
use std::io::prelude::*;
use std::io;
use std::io::prelude::*;
use std::net::Shutdown;
use std::os::unix::net;
use std::os::unix::prelude::*;
use std::path::Path;
use std::net::Shutdown;

use iovec::IoVec;
use iovec::unix as iovec;
use iovec::IoVec;
use libc;
use mio::event::Evented;
use mio::unix::EventedFd;
use mio::{Poll, Token, Ready, PollOpt};
use mio::{Poll, PollOpt, Ready, Token};

use cvt;
use socket::{sockaddr_un, Socket};
Expand Down Expand Up @@ -77,8 +77,10 @@ impl UnixStream {
/// Returns two `UnixStream`s which are connected to each other.
pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
Socket::pair(libc::SOCK_STREAM).map(|(a, b)| unsafe {
(UnixStream::from_raw_fd(a.into_fd()),
UnixStream::from_raw_fd(b.into_fd()))
(
UnixStream::from_raw_fd(a.into_fd()),
UnixStream::from_raw_fd(b.into_fd()),
)
})
}

Expand All @@ -89,9 +91,7 @@ impl UnixStream {
/// data, and options set on one stream will be propogated to the other
/// stream.
pub fn try_clone(&self) -> io::Result<UnixStream> {
self.inner.try_clone().map(|s| {
UnixStream { inner: s }
})
self.inner.try_clone().map(|s| UnixStream { inner: s })
}

/// Returns the socket address of the local half of this connection.
Expand Down Expand Up @@ -134,9 +134,7 @@ impl UnixStream {
unsafe {
let slice = iovec::as_os_slice_mut(bufs);
let len = cmp::min(<libc::c_int>::max_value() as usize, slice.len());
let rc = libc::readv(self.inner.as_raw_fd(),
slice.as_ptr(),
len as libc::c_int);
let rc = libc::readv(self.inner.as_raw_fd(), slice.as_ptr(), len as libc::c_int);
if rc < 0 {
Err(io::Error::last_os_error())
} else {
Expand All @@ -161,9 +159,7 @@ impl UnixStream {
unsafe {
let slice = iovec::as_os_slice(bufs);
let len = cmp::min(<libc::c_int>::max_value() as usize, slice.len());
let rc = libc::writev(self.inner.as_raw_fd(),
slice.as_ptr(),
len as libc::c_int);
let rc = libc::writev(self.inner.as_raw_fd(), slice.as_ptr(), len as libc::c_int);
if rc < 0 {
Err(io::Error::last_os_error())
} else {
Expand All @@ -174,19 +170,17 @@ impl UnixStream {
}

impl Evented for UnixStream {
fn register(&self,
poll: &Poll,
token: Token,
events: Ready,
opts: PollOpt) -> io::Result<()> {
fn register(&self, poll: &Poll, token: Token, events: Ready, opts: PollOpt) -> io::Result<()> {
EventedFd(&self.as_raw_fd()).register(poll, token, events, opts)
}

fn reregister(&self,
poll: &Poll,
token: Token,
events: Ready,
opts: PollOpt) -> io::Result<()> {
fn reregister(
&self,
poll: &Poll,
token: Token,
events: Ready,
opts: PollOpt,
) -> io::Result<()> {
EventedFd(&self.as_raw_fd()).reregister(poll, token, events, opts)
}

Expand Down Expand Up @@ -241,6 +235,8 @@ impl IntoRawFd for UnixStream {

impl FromRawFd for UnixStream {
unsafe fn from_raw_fd(fd: i32) -> UnixStream {
UnixStream { inner: net::UnixStream::from_raw_fd(fd) }
UnixStream {
inner: net::UnixStream::from_raw_fd(fd),
}
}
}