@@ -2009,12 +2009,116 @@ pub fn socketpair<T: Into<Option<SockProtocol>>>(
2009
2009
unsafe { Ok ( ( OwnedFd :: from_raw_fd ( fds[ 0 ] ) , OwnedFd :: from_raw_fd ( fds[ 1 ] ) ) ) }
2010
2010
}
2011
2011
2012
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
2013
+ pub struct Backlog ( i32 ) ;
2014
+
2015
+ impl Backlog {
2016
+ /// Sets the listen queue size to system `SOMAXCONN` value
2017
+ #[ cfg( target_os = "redox" ) ]
2018
+ pub const DEFAULT : Self = Self ( 128 ) ;
2019
+ #[ cfg( not( target_os = "redox" ) ) ]
2020
+ pub const DEFAULT : Self = Self ( libc:: SOMAXCONN ) ;
2021
+ /// Sets the listen queue size to -1 for system supporting it
2022
+ #[ cfg( any( target_os = "linux" , target_os = "freebsd" ) ) ]
2023
+ pub const MAXALLOWABLE : Self = Self ( -1 ) ;
2024
+ /// Reasonable upper limit, while in theory we might be able
2025
+ /// to set it to a higher boundary, comes with a risk
2026
+ /// of resource exhaustion in return
2027
+ pub const MAXVALUE : Self = Self ( 511 ) ;
2028
+ }
2029
+
2030
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
2031
+ pub enum BacklogTryFromError {
2032
+ TooNegative ,
2033
+ TooPositive ,
2034
+ }
2035
+
2036
+ impl std:: fmt:: Display for BacklogTryFromError {
2037
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
2038
+ match self {
2039
+ Self :: TooNegative => write ! ( f, "Passed a positive backlog less than -1" ) ,
2040
+ Self :: TooPositive => write ! ( f, "Passed a positive backlog greater than `511`" )
2041
+ }
2042
+ }
2043
+ }
2044
+
2045
+ impl std:: error:: Error for BacklogTryFromError { }
2046
+
2047
+ impl < T : Into < Backlog > > From < Option < T > > for Backlog {
2048
+ fn from ( backlog : Option < T > ) -> Self {
2049
+ backlog. map_or ( Self :: DEFAULT , |b| b. into ( ) )
2050
+ }
2051
+ }
2052
+
2053
+ impl From < u16 > for Backlog {
2054
+ fn from ( backlog : u16 ) -> Self {
2055
+ Self ( i32:: from ( backlog) )
2056
+ }
2057
+ }
2058
+
2059
+ impl From < u8 > for Backlog {
2060
+ fn from ( backlog : u8 ) -> Self {
2061
+ Self ( i32:: from ( backlog) )
2062
+ }
2063
+ }
2064
+
2065
+ impl From < Backlog > for i32 {
2066
+ fn from ( backlog : Backlog ) -> Self {
2067
+ backlog. 0
2068
+ }
2069
+ }
2070
+
2071
+ impl TryFrom < i64 > for Backlog {
2072
+ type Error = BacklogTryFromError ;
2073
+ fn try_from ( backlog : i64 ) -> std:: result:: Result < Self , Self :: Error > {
2074
+ match backlog {
2075
+ ..=-2 => Err ( BacklogTryFromError :: TooNegative ) ,
2076
+ -1 ..=511 => Ok ( Self ( i32:: try_from ( backlog) . map_err ( |_| BacklogTryFromError :: TooPositive ) ?) ) ,
2077
+ _ => Err ( BacklogTryFromError :: TooPositive ) ,
2078
+ }
2079
+ }
2080
+ }
2081
+
2082
+ impl TryFrom < i32 > for Backlog {
2083
+ type Error = BacklogTryFromError ;
2084
+ fn try_from ( backlog : i32 ) -> std:: result:: Result < Self , Self :: Error > {
2085
+ match backlog {
2086
+ ..=-2 => Err ( BacklogTryFromError :: TooNegative ) ,
2087
+ -1 ..=511 => Ok ( Self ( backlog) ) ,
2088
+ _ => Err ( BacklogTryFromError :: TooPositive ) ,
2089
+ }
2090
+ }
2091
+ }
2092
+
2093
+ impl TryFrom < i16 > for Backlog {
2094
+ type Error = BacklogTryFromError ;
2095
+ fn try_from ( backlog : i16 ) -> std:: result:: Result < Self , Self :: Error > {
2096
+ match backlog {
2097
+ ..=-2 => Err ( BacklogTryFromError :: TooNegative ) ,
2098
+ -1 ..=511 => Ok ( Self ( i32:: from ( backlog) ) ) ,
2099
+ _ => Err ( BacklogTryFromError :: TooPositive ) ,
2100
+ }
2101
+ }
2102
+ }
2103
+
2104
+ impl TryFrom < i8 > for Backlog {
2105
+ type Error = BacklogTryFromError ;
2106
+ fn try_from ( backlog : i8 ) -> std:: result:: Result < Self , Self :: Error > {
2107
+ match backlog {
2108
+ ..=-2 => Err ( BacklogTryFromError :: TooNegative ) ,
2109
+ _ => Err ( BacklogTryFromError :: TooPositive ) ,
2110
+ }
2111
+ }
2112
+ }
2113
+
2114
+ /// Listen for connections on a socket
2115
+
2012
2116
/// Listen for connections on a socket
2013
2117
///
2014
2118
/// [Further reading](https://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.html)
2015
- pub fn listen < F : AsFd > ( sock : & F , backlog : usize ) -> Result < ( ) > {
2119
+ pub fn listen < F : AsFd , B : Into < Backlog > > ( sock : & F , backlog : B ) -> Result < ( ) > {
2016
2120
let fd = sock. as_fd ( ) . as_raw_fd ( ) ;
2017
- let res = unsafe { libc:: listen ( fd, backlog as c_int ) } ;
2121
+ let res = unsafe { libc:: listen ( fd, i32 :: from ( backlog. into ( ) ) ) } ;
2018
2122
2019
2123
Errno :: result ( res) . map ( drop)
2020
2124
}
0 commit comments