1
+ {-# LANGUAGE AllowAmbiguousTypes #-}
1
2
{-# LANGUAGE CPP #-}
2
3
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3
4
{-# LANGUAGE PatternSynonyms #-}
4
5
{-# LANGUAGE ScopedTypeVariables #-}
6
+ {-# LANGUAGE TypeApplications #-}
5
7
6
8
module Network.Socket.Posix.Cmsg where
7
9
@@ -11,7 +13,6 @@ module Network.Socket.Posix.Cmsg where
11
13
#include <sys/socket.h>
12
14
13
15
import Data.ByteString.Internal
14
- import Data.Proxy
15
16
import Foreign.ForeignPtr
16
17
import System.IO.Unsafe (unsafeDupablePerformIO )
17
18
import System.Posix.Types (Fd (.. ))
@@ -88,14 +89,14 @@ filterCmsg cid cmsgs = filter (\cmsg -> cmsgId cmsg == cid) cmsgs
88
89
-- Each control message type has a numeric 'CmsgId' and a 'Storable'
89
90
-- data representation.
90
91
class Storable a => ControlMessage a where
91
- controlMessageId :: Proxy a -> CmsgId
92
+ controlMessageId :: CmsgId
92
93
93
94
encodeCmsg :: forall a . ControlMessage a => a -> Cmsg
94
95
encodeCmsg x = unsafeDupablePerformIO $ do
95
96
bs <- create siz $ \ p0 -> do
96
97
let p = castPtr p0
97
98
poke p x
98
- let cmsid = controlMessageId ( Proxy :: Proxy a )
99
+ let cmsid = controlMessageId @ a
99
100
return $ Cmsg cmsid bs
100
101
where
101
102
siz = sizeOf x
@@ -108,7 +109,7 @@ decodeCmsg (Cmsg cmsid (PS fptr off len))
108
109
let p = castPtr (p0 `plusPtr` off)
109
110
Just <$> peek p
110
111
where
111
- cid = controlMessageId ( Proxy :: Proxy a )
112
+ cid = controlMessageId @ a
112
113
siz = sizeOf (undefined :: a )
113
114
114
115
----------------------------------------------------------------
@@ -121,31 +122,31 @@ newtype IPv4TTL = IPv4TTL CInt deriving (Eq, Show, Storable)
121
122
#endif
122
123
123
124
instance ControlMessage IPv4TTL where
124
- controlMessageId _ = CmsgIdIPv4TTL
125
+ controlMessageId = CmsgIdIPv4TTL
125
126
126
127
----------------------------------------------------------------
127
128
128
129
-- | Hop limit of IPv6.
129
130
newtype IPv6HopLimit = IPv6HopLimit CInt deriving (Eq , Show , Storable )
130
131
131
132
instance ControlMessage IPv6HopLimit where
132
- controlMessageId _ = CmsgIdIPv6HopLimit
133
+ controlMessageId = CmsgIdIPv6HopLimit
133
134
134
135
----------------------------------------------------------------
135
136
136
137
-- | TOS of IPv4.
137
138
newtype IPv4TOS = IPv4TOS CChar deriving (Eq , Show , Storable )
138
139
139
140
instance ControlMessage IPv4TOS where
140
- controlMessageId _ = CmsgIdIPv4TOS
141
+ controlMessageId = CmsgIdIPv4TOS
141
142
142
143
----------------------------------------------------------------
143
144
144
145
-- | Traffic class of IPv6.
145
146
newtype IPv6TClass = IPv6TClass CInt deriving (Eq , Show , Storable )
146
147
147
148
instance ControlMessage IPv6TClass where
148
- controlMessageId _ = CmsgIdIPv6TClass
149
+ controlMessageId = CmsgIdIPv6TClass
149
150
150
151
----------------------------------------------------------------
151
152
@@ -156,7 +157,7 @@ instance Show IPv4PktInfo where
156
157
show (IPv4PktInfo n sa ha) = " IPv4PktInfo " ++ show n ++ " " ++ show (hostAddressToTuple sa) ++ " " ++ show (hostAddressToTuple ha)
157
158
158
159
instance ControlMessage IPv4PktInfo where
159
- controlMessageId _ = CmsgIdIPv4PktInfo
160
+ controlMessageId = CmsgIdIPv4PktInfo
160
161
161
162
instance Storable IPv4PktInfo where
162
163
sizeOf _ = (# size struct in_pktinfo)
@@ -180,7 +181,7 @@ instance Show IPv6PktInfo where
180
181
show (IPv6PktInfo n ha6) = " IPv6PktInfo " ++ show n ++ " " ++ show (hostAddress6ToTuple ha6)
181
182
182
183
instance ControlMessage IPv6PktInfo where
183
- controlMessageId _ = CmsgIdIPv6PktInfo
184
+ controlMessageId = CmsgIdIPv6PktInfo
184
185
185
186
instance Storable IPv6PktInfo where
186
187
sizeOf _ = (# size struct in6_pktinfo)
@@ -196,4 +197,4 @@ instance Storable IPv6PktInfo where
196
197
----------------------------------------------------------------
197
198
198
199
instance ControlMessage Fd where
199
- controlMessageId _ = CmsgIdFd
200
+ controlMessageId = CmsgIdFd
0 commit comments