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

Use namespaced enum variants. #21

Merged
merged 2 commits into from
Nov 19, 2014
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
13 changes: 7 additions & 6 deletions src/errno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::num::from_uint;
use libc::c_int;

pub use self::consts::*;
pub use self::consts::Errno::*;

pub type SysResult<T> = Result<T, SysError>;

Expand Down Expand Up @@ -555,8 +556,8 @@ mod consts {
EHWPOISON = 133,
}

pub const EWOULDBLOCK: Errno = EAGAIN;
pub const EDEADLOCK: Errno = EDEADLK;
pub const EWOULDBLOCK: Errno = Errno::EAGAIN;
pub const EDEADLOCK: Errno = Errno::EDEADLK;
}

#[cfg(any(target_os = "macos", target_os = "ios"))]
Expand Down Expand Up @@ -672,9 +673,9 @@ mod consts {
EQFULL = 106,
}

pub const ELAST: Errno = EQFULL;
pub const EWOULDBLOCK: Errno = EAGAIN;
pub const EDEADLOCK: Errno = EDEADLK;
pub const ELAST: Errno = Errno::EQFULL;
pub const EWOULDBLOCK: Errno = Errno::EAGAIN;
pub const EDEADLOCK: Errno = Errno::EDEADLK;

pub const EL2NSYNC: Errno = UnknownErrno;
pub const EL2NSYNC: Errno = Errno::UnknownErrno;
}
2 changes: 2 additions & 0 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ pub enum FcntlArg<'a> {

// TODO: Figure out how to handle value fcntl returns
pub fn fcntl(fd: Fd, arg: FcntlArg) -> SysResult<()> {
use self::FcntlArg::*;

let res = unsafe {
match arg {
F_SETFD(flag) => ffi::fcntl(fd, ffi::F_SETFD, flag.bits()),
Expand Down
19 changes: 15 additions & 4 deletions src/sys/socket.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{mem, ptr, fmt};
use std::num::Int;
use libc::{c_void, c_int, socklen_t, size_t, ssize_t};
use fcntl::{Fd, fcntl, F_SETFL, F_SETFD, FD_CLOEXEC, O_NONBLOCK};
use fcntl::{Fd, fcntl, FD_CLOEXEC, O_NONBLOCK};
use fcntl::FcntlArg::{F_SETFD, F_SETFL};
use errno::{SysResult, SysError, from_ffi};
use features;

Expand Down Expand Up @@ -257,6 +257,8 @@ pub fn listen(sockfd: Fd, backlog: uint) -> SysResult<()> {
}

pub fn bind(sockfd: Fd, addr: &SockAddr) -> SysResult<()> {
use self::SockAddr::*;

let res = unsafe {
match *addr {
SockIpV4(ref addr) => ffi::bind(sockfd, mem::transmute(addr), mem::size_of::<sockaddr_in>() as socklen_t),
Expand Down Expand Up @@ -330,6 +332,8 @@ fn accept4_polyfill(sockfd: Fd, flags: SockFlag) -> SysResult<Fd> {
}

pub fn connect(sockfd: Fd, addr: &SockAddr) -> SysResult<()> {
use self::SockAddr::*;

let res = unsafe {
match *addr {
SockIpV4(ref addr) => ffi::connect(sockfd, mem::transmute(addr), mem::size_of::<sockaddr_in>() as socklen_t),
Expand All @@ -344,7 +348,8 @@ pub fn connect(sockfd: Fd, addr: &SockAddr) -> SysResult<()> {
mod sa_helpers {
use std::mem;
use libc::{sockaddr_storage, sockaddr_in, sockaddr_in6, sockaddr_un};
use super::{SockAddr, SockIpV6, SockIpV4, SockUnix};
use super::SockAddr;
use super::SockAddr::*;

pub fn to_sockaddr_ipv4(addr: &sockaddr_storage) -> SockAddr {
let sin : &sockaddr_in = unsafe { mem::transmute(addr) };
Expand Down Expand Up @@ -385,6 +390,8 @@ pub fn recvfrom(sockfd: Fd, buf: &mut [u8]) -> SysResult<(uint, SockAddr)> {
}

fn print_ipv4_addr(sin: &sockaddr_in, f: &mut fmt::Formatter) -> fmt::Result {
use std::num::Int;

let ip_addr = Int::from_be(sin.sin_addr.s_addr);
let port = Int::from_be(sin.sin_port);

Expand All @@ -399,7 +406,7 @@ fn print_ipv4_addr(sin: &sockaddr_in, f: &mut fmt::Formatter) -> fmt::Result {
impl fmt::Show for SockAddr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
SockIpV4(sin) => print_ipv4_addr(&sin, f),
SockAddr::SockIpV4(sin) => print_ipv4_addr(&sin, f),
_ => unimplemented!()
}
}
Expand All @@ -420,6 +427,8 @@ fn sendto_sockaddr<T>(sockfd: Fd, buf: &[u8], flags: SockMessageFlags, addr: &T)
}

pub fn sendto(sockfd: Fd, buf: &[u8], addr: &SockAddr, flags: SockMessageFlags) -> SysResult<uint> {
use self::SockAddr::*;

let ret = match *addr {
SockIpV4(ref addr) => sendto_sockaddr(sockfd, buf, flags, addr),
SockIpV6(ref addr) => sendto_sockaddr(sockfd, buf, flags, addr),
Expand Down Expand Up @@ -482,6 +491,8 @@ fn getsockname_sockaddr<T>(sockfd: Fd, addr: &T) -> SysResult<bool> {
}

pub fn getsockname(sockfd: Fd, addr: &mut SockAddr) -> SysResult<bool> {
use self::SockAddr::*;

match *addr {
SockIpV4(ref addr) => getsockname_sockaddr(sockfd, addr),
SockIpV6(ref addr) => getsockname_sockaddr(sockfd, addr),
Expand Down
2 changes: 2 additions & 0 deletions src/sys/wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub enum WaitStatus {
}

pub fn waitpid(pid: pid_t, options: WaitPidFlag) -> SysResult<WaitStatus> {
use self::WaitStatus::*;

let mut status: i32 = 0;

let res = unsafe { ffi::waitpid(pid as pid_t, &mut status as *mut c_int, options.bits()) };
Expand Down
10 changes: 7 additions & 3 deletions src/unistd.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{mem, ptr};
use std::c_str::{CString, ToCStr};
use libc::{c_char, c_void, c_int, size_t, pid_t, off_t};
use fcntl::{fcntl, Fd, OFlag, O_NONBLOCK, O_CLOEXEC, FD_CLOEXEC, F_SETFD, F_SETFL};
use fcntl::{fcntl, Fd, OFlag, O_NONBLOCK, O_CLOEXEC, FD_CLOEXEC};
use fcntl::FcntlArg::{F_SETFD, F_SETFL};

use errno::{SysResult, SysError, from_ffi};
use core::raw::Slice as RawSlice;

Expand Down Expand Up @@ -59,20 +61,22 @@ pub enum Fork {
impl Fork {
pub fn is_child(&self) -> bool {
match *self {
Child => true,
Fork::Child => true,
_ => false
}
}

pub fn is_parent(&self) -> bool {
match *self {
Parent(_) => true,
Fork::Parent(_) => true,
_ => false
}
}
}

pub fn fork() -> SysResult<Fork> {
use self::Fork::*;

let res = unsafe { ffi::fork() };

if res < 0 {
Expand Down