{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_HADDOCK hide #-}
module Data.UUID.Types.Internal.Builder
(ByteSource(..)
,ByteSink
,Takes1Byte
,Takes2Bytes
,Takes3Bytes
,Takes4Bytes
) where
import Data.Bits
import Data.Word
type Takes1Byte g = Word8 -> g
type Takes2Bytes g = Word8 -> Word8 -> g
type Takes3Bytes g = Word8 -> Word8 -> Word8 -> g
type Takes4Bytes g = Word8 -> Word8 -> Word8 -> Word8 -> g
type family ByteSink w g
type instance ByteSink Word8 g = Takes1Byte g
type instance ByteSink Word16 g = Takes2Bytes g
type instance ByteSink Word32 g = Takes4Bytes g
type instance ByteSink Int g = Takes4Bytes g
class ByteSource w where
(/-/) :: ByteSink w g -> w -> g
infixl 6 /-/
instance ByteSource Word8 where
f :: ByteSink Word8 g
f /-/ :: ByteSink Word8 g -> Word8 -> g
/-/ w :: Word8
w = ByteSink Word8 g
Word8 -> g
f Word8
w
instance ByteSource Word16 where
f :: ByteSink Word16 g
f /-/ :: ByteSink Word16 g -> Word16 -> g
/-/ w :: Word16
w = ByteSink Word16 g
Takes2Bytes g
f Word8
b1 Word8
b2
where b1 :: Word8
b1 = Word16 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word16
w Word16 -> Int -> Word16
forall a. Bits a => a -> Int -> a
`shiftR` 8)
b2 :: Word8
b2 = Word16 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
w
instance ByteSource Word32 where
f :: ByteSink Word32 g
f /-/ :: ByteSink Word32 g -> Word32 -> g
/-/ w :: Word32
w = ByteSink Word32 g
Takes4Bytes g
f Word8
b1 Word8
b2 Word8
b3 Word8
b4
where b1 :: Word8
b1 = Word32 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32
w Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
`shiftR` 24)
b2 :: Word8
b2 = Word32 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32
w Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
`shiftR` 16)
b3 :: Word8
b3 = Word32 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32
w Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
`shiftR` 8)
b4 :: Word8
b4 = Word32 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
w
instance ByteSource Int where
f :: ByteSink Int g
f /-/ :: ByteSink Int g -> Int -> g
/-/ w :: Int
w = ByteSink Int g
Takes4Bytes g
f Word8
b1 Word8
b2 Word8
b3 Word8
b4
where b1 :: Word8
b1 = Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
w Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` 24)
b2 :: Word8
b2 = Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
w Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` 16)
b3 :: Word8
b3 = Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
w Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` 8)
b4 :: Word8
b4 = Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
w