diff options
-rw-r--r-- | src/sys/socket/mod.rs | 15 | ||||
-rw-r--r-- | src/sys/socket/sockopt.rs | 35 | ||||
-rw-r--r-- | src/sys/time.rs | 1 |
3 files changed, 22 insertions, 29 deletions
diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index 33f02ee7..0db5bc29 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -278,31 +278,26 @@ pub enum SockLevel { /// Represents a socket option that can be accessed or set. Used as an argument /// to `getsockopt` and `setsockopt`. pub trait SockOpt : Copy + fmt::Debug { - /// Type of `getsockopt` return value - type Get; - - /// Type of value used to set the socket option. Used as the argument to - /// `setsockopt`. - type Set; + type Val; #[doc(hidden)] - fn get(&self, fd: Fd, level: c_int) -> Result<Self::Get>; + fn get(&self, fd: Fd, level: c_int) -> Result<Self::Val>; #[doc(hidden)] - fn set(&self, fd: Fd, level: c_int, val: Self::Set) -> Result<()>; + fn set(&self, fd: Fd, level: c_int, val: &Self::Val) -> Result<()>; } /// Get the current value for the requested socket option /// /// [Further reading](http://man7.org/linux/man-pages/man2/setsockopt.2.html) -pub fn getsockopt<O: SockOpt>(fd: Fd, level: SockLevel, opt: O) -> Result<O::Get> { +pub fn getsockopt<O: SockOpt>(fd: Fd, level: SockLevel, opt: O) -> Result<O::Val> { opt.get(fd, level as c_int) } /// Sets the value for the requested socket option /// /// [Further reading](http://man7.org/linux/man-pages/man2/setsockopt.2.html) -pub fn setsockopt<O: SockOpt>(fd: Fd, level: SockLevel, opt: O, val: O::Set) -> Result<()> { +pub fn setsockopt<O: SockOpt>(fd: Fd, level: SockLevel, opt: O, val: &O::Val) -> Result<()> { opt.set(fd, level as c_int, val) } diff --git a/src/sys/socket/sockopt.rs b/src/sys/socket/sockopt.rs index 2bccc81f..032c62a1 100644 --- a/src/sys/socket/sockopt.rs +++ b/src/sys/socket/sockopt.rs @@ -10,26 +10,25 @@ use std::mem; // TODO: Figure out how to ommit gets when not supported by opt macro_rules! sockopt_impl { ($name:ident, $flag:path, bool) => { - sockopt_impl!($name, $flag, bool, GetBool, bool, SetBool); + sockopt_impl!($name, $flag, bool, GetBool, SetBool); }; ($name:ident, $flag:path, u8) => { - sockopt_impl!($name, $flag, u8, GetU8, u8, SetU8); + sockopt_impl!($name, $flag, u8, GetU8, SetU8); }; ($name:ident, $flag:path, $ty:ty) => { - sockopt_impl!($name, $flag, $ty, GetStruct<$ty>, &'a $ty, SetStruct<$ty>); + sockopt_impl!($name, $flag, $ty, GetStruct<$ty>, SetStruct<$ty>); }; - ($name:ident, $flag:path, $get_ty:ty, $getter:ty, $set_ty:ty, $setter:ty) => { + ($name:ident, $flag:path, $ty:ty, $getter:ty, $setter:ty) => { #[derive(Clone, Copy, Debug)] pub struct $name; - impl<'a> SockOpt for $name { - type Get = $get_ty; - type Set = $set_ty; + impl SockOpt for $name { + type Val = $ty; - fn get(&self, fd: Fd, level: c_int) -> Result<$get_ty> { + fn get(&self, fd: Fd, level: c_int) -> Result<$ty> { unsafe { let mut getter: $getter = Get::blank(); @@ -46,7 +45,7 @@ macro_rules! sockopt_impl { } } - fn set(&self, fd: Fd, level: c_int, val: $set_ty) -> Result<()> { + fn set(&self, fd: Fd, level: c_int, val: &$ty) -> Result<()> { unsafe { let setter: $setter = Set::new(val); @@ -93,8 +92,8 @@ trait Get<T> { unsafe fn unwrap(self) -> T; } -trait Set<T> { - fn new(val: T) -> Self; +trait Set<'a, T> { + fn new(val: &'a T) -> Self; unsafe fn ffi_ptr(&self) -> *const c_void; unsafe fn ffi_len(&self) -> socklen_t; } @@ -127,7 +126,7 @@ struct SetStruct<'a, T: 'static> { ptr: &'a T, } -impl<'a, T> Set<&'a T> for SetStruct<'a, T> { +impl<'a, T> Set<'a, T> for SetStruct<'a, T> { fn new(ptr: &'a T) -> SetStruct<'a, T> { SetStruct { ptr: ptr } } @@ -169,9 +168,9 @@ struct SetBool { val: c_int, } -impl Set<bool> for SetBool { - fn new(val: bool) -> SetBool { - SetBool { val: if val { 1 } else { 0 } } +impl<'a> Set<'a, bool> for SetBool { + fn new(val: &'a bool) -> SetBool { + SetBool { val: if *val { 1 } else { 0 } } } unsafe fn ffi_ptr(&self) -> *const c_void { @@ -211,9 +210,9 @@ struct SetU8 { val: uint8_t, } -impl Set<u8> for SetU8 { - fn new(val: u8) -> SetU8 { - SetU8 { val: val as uint8_t } +impl<'a> Set<'a, u8> for SetU8 { + fn new(val: &'a u8) -> SetU8 { + SetU8 { val: *val as uint8_t } } unsafe fn ffi_ptr(&self) -> *const c_void { diff --git a/src/sys/time.rs b/src/sys/time.rs index 5c39d987..1750481c 100644 --- a/src/sys/time.rs +++ b/src/sys/time.rs @@ -58,7 +58,6 @@ impl TimeVal { /// Makes a new `TimeVal` with given number of microseconds. #[inline] - #[unstable(feature = "std_misc")] pub fn microseconds(microseconds: i64) -> TimeVal { let (secs, micros) = div_mod_floor_64(microseconds, MICROS_PER_SEC); assert!(secs >= MIN_SECONDS && secs <= MAX_SECONDS, "TimeVal out of bounds"); |