Skip to content

Commit 861e768

Browse files
committed
using AllowAmbiguousTypes and TypeApplications.
1 parent d3ff9b8 commit 861e768

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

Network/Socket/Posix/Cmsg.hsc

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
{-# LANGUAGE AllowAmbiguousTypes #-}
12
{-# LANGUAGE CPP #-}
23
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
34
{-# LANGUAGE PatternSynonyms #-}
45
{-# LANGUAGE ScopedTypeVariables #-}
6+
{-# LANGUAGE TypeApplications #-}
57

68
module Network.Socket.Posix.Cmsg where
79

@@ -11,7 +13,6 @@ module Network.Socket.Posix.Cmsg where
1113
#include <sys/socket.h>
1214

1315
import Data.ByteString.Internal
14-
import Data.Proxy
1516
import Foreign.ForeignPtr
1617
import System.IO.Unsafe (unsafeDupablePerformIO)
1718
import System.Posix.Types (Fd(..))
@@ -88,14 +89,14 @@ filterCmsg cid cmsgs = filter (\cmsg -> cmsgId cmsg == cid) cmsgs
8889
-- Each control message type has a numeric 'CmsgId' and a 'Storable'
8990
-- data representation.
9091
class Storable a => ControlMessage a where
91-
controlMessageId :: Proxy a -> CmsgId
92+
controlMessageId :: CmsgId
9293

9394
encodeCmsg :: forall a . ControlMessage a => a -> Cmsg
9495
encodeCmsg x = unsafeDupablePerformIO $ do
9596
bs <- create siz $ \p0 -> do
9697
let p = castPtr p0
9798
poke p x
98-
let cmsid = controlMessageId (Proxy :: Proxy a)
99+
let cmsid = controlMessageId @a
99100
return $ Cmsg cmsid bs
100101
where
101102
siz = sizeOf x
@@ -108,7 +109,7 @@ decodeCmsg (Cmsg cmsid (PS fptr off len))
108109
let p = castPtr (p0 `plusPtr` off)
109110
Just <$> peek p
110111
where
111-
cid = controlMessageId (Proxy :: Proxy a)
112+
cid = controlMessageId @a
112113
siz = sizeOf (undefined :: a)
113114

114115
----------------------------------------------------------------
@@ -121,31 +122,31 @@ newtype IPv4TTL = IPv4TTL CInt deriving (Eq, Show, Storable)
121122
#endif
122123

123124
instance ControlMessage IPv4TTL where
124-
controlMessageId _ = CmsgIdIPv4TTL
125+
controlMessageId = CmsgIdIPv4TTL
125126

126127
----------------------------------------------------------------
127128

128129
-- | Hop limit of IPv6.
129130
newtype IPv6HopLimit = IPv6HopLimit CInt deriving (Eq, Show, Storable)
130131

131132
instance ControlMessage IPv6HopLimit where
132-
controlMessageId _ = CmsgIdIPv6HopLimit
133+
controlMessageId = CmsgIdIPv6HopLimit
133134

134135
----------------------------------------------------------------
135136

136137
-- | TOS of IPv4.
137138
newtype IPv4TOS = IPv4TOS CChar deriving (Eq, Show, Storable)
138139

139140
instance ControlMessage IPv4TOS where
140-
controlMessageId _ = CmsgIdIPv4TOS
141+
controlMessageId = CmsgIdIPv4TOS
141142

142143
----------------------------------------------------------------
143144

144145
-- | Traffic class of IPv6.
145146
newtype IPv6TClass = IPv6TClass CInt deriving (Eq, Show, Storable)
146147

147148
instance ControlMessage IPv6TClass where
148-
controlMessageId _ = CmsgIdIPv6TClass
149+
controlMessageId = CmsgIdIPv6TClass
149150

150151
----------------------------------------------------------------
151152

@@ -156,7 +157,7 @@ instance Show IPv4PktInfo where
156157
show (IPv4PktInfo n sa ha) = "IPv4PktInfo " ++ show n ++ " " ++ show (hostAddressToTuple sa) ++ " " ++ show (hostAddressToTuple ha)
157158

158159
instance ControlMessage IPv4PktInfo where
159-
controlMessageId _ = CmsgIdIPv4PktInfo
160+
controlMessageId = CmsgIdIPv4PktInfo
160161

161162
instance Storable IPv4PktInfo where
162163
sizeOf _ = (#size struct in_pktinfo)
@@ -180,7 +181,7 @@ instance Show IPv6PktInfo where
180181
show (IPv6PktInfo n ha6) = "IPv6PktInfo " ++ show n ++ " " ++ show (hostAddress6ToTuple ha6)
181182

182183
instance ControlMessage IPv6PktInfo where
183-
controlMessageId _ = CmsgIdIPv6PktInfo
184+
controlMessageId = CmsgIdIPv6PktInfo
184185

185186
instance Storable IPv6PktInfo where
186187
sizeOf _ = (#size struct in6_pktinfo)
@@ -196,4 +197,4 @@ instance Storable IPv6PktInfo where
196197
----------------------------------------------------------------
197198

198199
instance ControlMessage Fd where
199-
controlMessageId _ = CmsgIdFd
200+
controlMessageId = CmsgIdFd

0 commit comments

Comments
 (0)