Skip to content

Commit a41d302

Browse files
committed
WIP readshow for CmsgId and ghc-cpp guards
implements bijective read/show instances for CmsgId adds missing ghc-dependent cpp guards around version-dependent pragmas and inlined haddock annotations adds commented-out pre-implementation of bijective read/show boilerplate for MsgFlag type
1 parent c0322a1 commit a41d302

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

Network/Socket/Flag.hsc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import qualified Data.Semigroup as Sem
1010

1111
import Network.Socket.Imports
1212

13+
{-
14+
import Network.Socket.ReadShow
15+
16+
import qualified Text.Read as P
17+
-}
18+
1319
-- | Message flags. To combine flags, use '(<>)'.
1420
newtype MsgFlag = MsgFlag { fromMsgFlag :: CInt }
1521
deriving (Show, Eq, Ord, Num, Bits)
@@ -78,3 +84,16 @@ pattern MSG_WAITALL = MsgFlag (#const MSG_WAITALL)
7884
#else
7985
pattern MSG_WAITALL = MsgFlag 0
8086
#endif
87+
88+
{-
89+
msgFlagPairs :: [Pair MsgFlag String]
90+
msgFlagPairs =
91+
[ (MSG_OOB, "MSG_OOB")
92+
, (MSG_DONTROUTE, "MSG_DONTROUTE")
93+
, (MSG_PEEK, "MSG_PEEK")
94+
, (MSG_EOR, "MSG_EOR")
95+
, (MSG_TRUNC, "MSG_TRUNC")
96+
, (MSG_CTRUNC, "MSG_CTRUNC")
97+
, (MSG_WAITALL, "MSG_WAITALL")
98+
]
99+
-}

Network/Socket/Options.hsc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,13 @@ import Network.Socket.ReadShow
4444
-- The existence of a constructor does not imply that the relevant option
4545
-- is supported on your system: see 'isSupportedSocketOption'
4646
data SocketOption = SockOpt
47+
#if __GLASGOW_HASKELL__ >= 806
4748
!CInt -- ^ Option Level
4849
!CInt -- ^ Option Name
50+
#else
51+
!CInt -- Option Level
52+
!CInt -- Option Name
53+
#endif
4954
deriving (Eq)
5055

5156
-- | Does the 'SocketOption' exist on this system?
@@ -420,10 +425,6 @@ socketOptionBijection :: Bijection SocketOption String
420425
socketOptionBijection = Bijection{..}
421426
where
422427
cso = "CustomSockOpt"
423-
_parse :: String -> (CInt, CInt)
424-
_parse xy =
425-
let (xs, ('_':ys)) = break (=='_') xy
426-
in (read xs, read ys)
427428
defFwd = \(CustomSockOpt (n,m)) -> cso++show n++"_"++show m
428429
defBwd s = case splitAt (length cso) s of
429430
("CustomSockOpt", nm) -> CustomSockOpt $ _parse nm

Network/Socket/Posix/Cmsg.hsc

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE CPP #-}
33
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
44
{-# LANGUAGE PatternSynonyms #-}
5+
{-# LANGUAGE RecordWildCards #-}
56
{-# LANGUAGE ScopedTypeVariables #-}
67
{-# LANGUAGE TypeApplications #-}
78

@@ -19,6 +20,9 @@ import System.Posix.Types (Fd(..))
1920

2021
import Network.Socket.Imports
2122
import Network.Socket.Types
23+
import Network.Socket.ReadShow
24+
25+
import qualified Text.Read as P
2226

2327
-- | Control message (ancillary data) including a pair of level and type.
2428
data Cmsg = Cmsg {
@@ -32,7 +36,7 @@ data Cmsg = Cmsg {
3236
data CmsgId = CmsgId {
3337
cmsgLevel :: !CInt
3438
, cmsgType :: !CInt
35-
} deriving (Eq, Show)
39+
} deriving (Eq)
3640

3741
-- | The identifier for 'IPv4TTL'.
3842
pattern CmsgIdIPv4TTL :: CmsgId
@@ -220,3 +224,31 @@ instance Storable IPv6PktInfo where
220224

221225
instance ControlMessage Fd where
222226
controlMessageId = CmsgIdFd
227+
228+
cmsgIdPairs :: [Pair CmsgId String]
229+
cmsgIdPairs =
230+
[ (CmsgIdIPv4TTL, "CmsgIdIPv4TTL")
231+
, (CmsgIdIPv6HopLimit, "CmsgIdIPv6HopLimit")
232+
, (CmsgIdIPv4TOS, "CmsgIdIPv4TOS")
233+
, (CmsgIdIPv6TClass, "CmsgIdIPv6TClass")
234+
, (CmsgIdIPv4PktInfo, "CmsgIdIPv4PktInfo")
235+
, (CmsgIdIPv6PktInfo, "CmsgIdIPv6PktInfo")
236+
, (CmsgIdFd, "CmsgIdFd")
237+
]
238+
239+
cmsgIdBijection :: Bijection CmsgId String
240+
cmsgIdBijection = Bijection{..}
241+
where
242+
defname = "CmsgId"
243+
defFwd = \(CmsgId l t) -> defname++show l++"_" ++show t
244+
defBwd s =
245+
case splitAt (length defname) s of
246+
("CmsgId", nm) -> uncurry CmsgId $ _parse nm
247+
_ -> error "cmsgIdBijection: exception in WIP ReadShow code"
248+
pairs = cmsgIdPairs
249+
250+
instance Show CmsgId where
251+
show = forward cmsgIdBijection
252+
253+
instance Read CmsgId where
254+
readPrec = P.lexP >>= \(P.Ident x) -> return $ backward cmsgIdBijection x

Network/Socket/ReadShow.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,19 @@ data Bijection a b
3838
, pairs :: [Pair a b]
3939
}
4040

41+
-- | apply a bijection over an LHS-value
4142
forward :: (Eq a) => Bijection a b -> a -> b
4243
forward Bijection{..} = lookForward defFwd pairs
4344

45+
-- | apply a bijection over an RHS-value
4446
backward :: (Eq b) => Bijection a b -> b -> a
4547
backward Bijection{..} = lookBackward defBwd pairs
48+
49+
-- | parse an underscore-separated pair into a tuple
50+
-- should not be used if either type might have
51+
-- literal underscores in the Read pre-image
52+
_parse :: (Read a, Read b) => String -> (a, b)
53+
_parse xy =
54+
let (xs, '_':ys) = break (=='_') xy
55+
in (read xs, read ys)
56+

0 commit comments

Comments
 (0)