module Streamly.Internal.Serialize.ToBytes
(
ToBytes (..)
, unit
, bool
, ordering
, word8
, word16be
, word16le
, word32be
, word32le
, word64be
, word64le
, word64host
, int8
, int16be
, int16le
, int32be
, int32le
, int64be
, int64le
, float32be
, float32le
, double64be
, double64le
, charLatin1
, charUtf8
)
where
#include "MachDeps.h"
import Data.Bits (shiftR)
import Data.Char (ord)
import Data.Int (Int8, Int16, Int32, Int64)
import Data.Word (Word8, Word16, Word32, Word64)
import GHC.Float (castDoubleToWord64, castFloatToWord32)
import Streamly.Internal.Data.Stream.StreamD (Stream)
import Streamly.Internal.Data.Stream.StreamD (Step(..))
import Streamly.Internal.Unicode.Stream (readCharUtf8)
import qualified Streamly.Internal.Data.Stream.StreamD as Stream
import qualified Streamly.Internal.Data.Stream.StreamD as D
{-# INLINE unit #-}
unit :: Applicative m => Stream m Word8
unit :: forall (m :: * -> *). Applicative m => Stream m Word8
unit = forall (m :: * -> *) a. Applicative m => a -> Stream m a
Stream.fromPure Word8
0
{-# INLINE boolToWord8 #-}
boolToWord8 :: Bool -> Word8
boolToWord8 :: Bool -> Word8
boolToWord8 Bool
False = Word8
0
boolToWord8 Bool
True = Word8
1
{-# INLINE bool #-}
bool :: Applicative m => Bool -> Stream m Word8
bool :: forall (m :: * -> *). Applicative m => Bool -> Stream m Word8
bool = forall (m :: * -> *) a. Applicative m => a -> Stream m a
Stream.fromPure forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Word8
boolToWord8
{-# INLINE orderingToWord8 #-}
orderingToWord8 :: Ordering -> Word8
orderingToWord8 :: Ordering -> Word8
orderingToWord8 Ordering
LT = Word8
0
orderingToWord8 Ordering
EQ = Word8
1
orderingToWord8 Ordering
GT = Word8
2
{-# INLINE ordering #-}
ordering :: Applicative m => Ordering -> Stream m Word8
ordering :: forall (m :: * -> *). Applicative m => Ordering -> Stream m Word8
ordering = forall (m :: * -> *) a. Applicative m => a -> Stream m a
Stream.fromPure forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ordering -> Word8
orderingToWord8
{-# INLINE word8 #-}
word8 :: Applicative m => Word8 -> Stream m Word8
word8 :: forall (m :: * -> *). Applicative m => Word8 -> Stream m Word8
word8 = forall (m :: * -> *) a. Applicative m => a -> Stream m a
Stream.fromPure
data W16State = W16B1 | W16B2 | W16Done
{-# INLINE word16beD #-}
word16beD :: Applicative m => Word16 -> D.Stream m Word8
word16beD :: forall (m :: * -> *). Applicative m => Word16 -> Stream m Word8
word16beD Word16
w = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream forall {f :: * -> *} {p}.
Applicative f =>
p -> W16State -> f (Step W16State Word8)
step W16State
W16B1
where
step :: p -> W16State -> f (Step W16State Word8)
step p
_ W16State
W16B1 = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield (forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Bits a => a -> Int -> a
shiftR Word16
w Int
8) :: Word8) W16State
W16B2
step p
_ W16State
W16B2 = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
w :: Word8) W16State
W16Done
step p
_ W16State
W16Done = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall s a. Step s a
Stop
{-# INLINE word16be #-}
word16be :: Monad m => Word16 -> Stream m Word8
word16be :: forall (m :: * -> *). Monad m => Word16 -> Stream m Word8
word16be = forall (m :: * -> *). Applicative m => Word16 -> Stream m Word8
word16beD
{-# INLINE word16leD #-}
word16leD :: Applicative m => Word16 -> D.Stream m Word8
word16leD :: forall (m :: * -> *). Applicative m => Word16 -> Stream m Word8
word16leD Word16
w = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream forall {f :: * -> *} {p}.
Applicative f =>
p -> W16State -> f (Step W16State Word8)
step W16State
W16B1
where
step :: p -> W16State -> f (Step W16State Word8)
step p
_ W16State
W16B1 = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
w :: Word8) W16State
W16B2
step p
_ W16State
W16B2 = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield (forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Bits a => a -> Int -> a
shiftR Word16
w Int
8) :: Word8) W16State
W16Done
step p
_ W16State
W16Done = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall s a. Step s a
Stop
{-# INLINE word16le #-}
word16le :: Monad m => Word16 -> Stream m Word8
word16le :: forall (m :: * -> *). Monad m => Word16 -> Stream m Word8
word16le = forall (m :: * -> *). Applicative m => Word16 -> Stream m Word8
word16leD
data W32State = W32B1 | W32B2 | W32B3 | W32B4 | W32Done
{-# INLINE word32beD #-}
word32beD :: Applicative m => Word32 -> D.Stream m Word8
word32beD :: forall (m :: * -> *). Applicative m => Word32 -> Stream m Word8
word32beD Word32
w = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream forall {f :: * -> *} {p}.
Applicative f =>
p -> W32State -> f (Step W32State Word8)
step W32State
W32B1
where
yield :: Int -> s -> f (Step s Word8)
yield Int
n s
s = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield (forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Bits a => a -> Int -> a
shiftR Word32
w Int
n) :: Word8) s
s
step :: p -> W32State -> f (Step W32State Word8)
step p
_ W32State
W32B1 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
24 W32State
W32B2
step p
_ W32State
W32B2 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
16 W32State
W32B3
step p
_ W32State
W32B3 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
8 W32State
W32B4
step p
_ W32State
W32B4 = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
w :: Word8) W32State
W32Done
step p
_ W32State
W32Done = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall s a. Step s a
Stop
{-# INLINE word32be #-}
word32be :: Monad m => Word32 -> Stream m Word8
word32be :: forall (m :: * -> *). Monad m => Word32 -> Stream m Word8
word32be = forall (m :: * -> *). Applicative m => Word32 -> Stream m Word8
word32beD
{-# INLINE word32leD #-}
word32leD :: Applicative m => Word32 -> D.Stream m Word8
word32leD :: forall (m :: * -> *). Applicative m => Word32 -> Stream m Word8
word32leD Word32
w = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream forall {f :: * -> *} {p}.
Applicative f =>
p -> W32State -> f (Step W32State Word8)
step W32State
W32B1
where
yield :: Int -> s -> f (Step s Word8)
yield Int
n s
s = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield (forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Bits a => a -> Int -> a
shiftR Word32
w Int
n) :: Word8) s
s
step :: p -> W32State -> f (Step W32State Word8)
step p
_ W32State
W32B1 = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
w :: Word8) W32State
W32B2
step p
_ W32State
W32B2 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
8 W32State
W32B3
step p
_ W32State
W32B3 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
16 W32State
W32B4
step p
_ W32State
W32B4 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
24 W32State
W32Done
step p
_ W32State
W32Done = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall s a. Step s a
Stop
{-# INLINE word32le #-}
word32le :: Monad m => Word32 -> Stream m Word8
word32le :: forall (m :: * -> *). Monad m => Word32 -> Stream m Word8
word32le = forall (m :: * -> *). Applicative m => Word32 -> Stream m Word8
word32leD
data W64State =
W64B1 | W64B2 | W64B3 | W64B4 | W64B5 | W64B6 | W64B7 | W64B8 | W64Done
{-# INLINE word64beD #-}
word64beD :: Applicative m => Word64 -> D.Stream m Word8
word64beD :: forall (m :: * -> *). Applicative m => Word64 -> Stream m Word8
word64beD Word64
w = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream forall {f :: * -> *} {p}.
Applicative f =>
p -> W64State -> f (Step W64State Word8)
step W64State
W64B1
where
yield :: Int -> s -> f (Step s Word8)
yield Int
n s
s = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield (forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Bits a => a -> Int -> a
shiftR Word64
w Int
n) :: Word8) s
s
step :: p -> W64State -> f (Step W64State Word8)
step p
_ W64State
W64B1 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
56 W64State
W64B2
step p
_ W64State
W64B2 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
48 W64State
W64B3
step p
_ W64State
W64B3 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
40 W64State
W64B4
step p
_ W64State
W64B4 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
32 W64State
W64B5
step p
_ W64State
W64B5 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
24 W64State
W64B6
step p
_ W64State
W64B6 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
16 W64State
W64B7
step p
_ W64State
W64B7 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
8 W64State
W64B8
step p
_ W64State
W64B8 = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
w :: Word8) W64State
W64Done
step p
_ W64State
W64Done = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall s a. Step s a
Stop
{-# INLINE word64be #-}
word64be :: Monad m => Word64 -> Stream m Word8
word64be :: forall (m :: * -> *). Monad m => Word64 -> Stream m Word8
word64be = forall (m :: * -> *). Applicative m => Word64 -> Stream m Word8
word64beD
{-# INLINE word64leD #-}
word64leD :: Applicative m => Word64 -> D.Stream m Word8
word64leD :: forall (m :: * -> *). Applicative m => Word64 -> Stream m Word8
word64leD Word64
w = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream forall {f :: * -> *} {p}.
Applicative f =>
p -> W64State -> f (Step W64State Word8)
step W64State
W64B1
where
yield :: Int -> s -> f (Step s Word8)
yield Int
n s
s = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield (forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Bits a => a -> Int -> a
shiftR Word64
w Int
n) :: Word8) s
s
step :: p -> W64State -> f (Step W64State Word8)
step p
_ W64State
W64B1 = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
w :: Word8) W64State
W64B2
step p
_ W64State
W64B2 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
8 W64State
W64B3
step p
_ W64State
W64B3 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
16 W64State
W64B4
step p
_ W64State
W64B4 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
24 W64State
W64B5
step p
_ W64State
W64B5 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
32 W64State
W64B6
step p
_ W64State
W64B6 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
40 W64State
W64B7
step p
_ W64State
W64B7 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
48 W64State
W64B8
step p
_ W64State
W64B8 = forall {f :: * -> *} {s}.
Applicative f =>
Int -> s -> f (Step s Word8)
yield Int
56 W64State
W64Done
step p
_ W64State
W64Done = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall s a. Step s a
Stop
{-# INLINE word64le #-}
word64le :: Monad m => Word64 -> Stream m Word8
word64le :: forall (m :: * -> *). Monad m => Word64 -> Stream m Word8
word64le = forall (m :: * -> *). Applicative m => Word64 -> Stream m Word8
word64leD
{-# INLINE int8 #-}
int8 :: Applicative m => Int8 -> Stream m Word8
int8 :: forall (m :: * -> *). Applicative m => Int8 -> Stream m Word8
int8 Int8
i = forall (m :: * -> *). Applicative m => Word8 -> Stream m Word8
word8 (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int8
i :: Word8)
{-# INLINE int16be #-}
int16be :: Monad m => Int16 -> Stream m Word8
int16be :: forall (m :: * -> *). Monad m => Int16 -> Stream m Word8
int16be Int16
i = forall (m :: * -> *). Monad m => Word16 -> Stream m Word8
word16be (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int16
i :: Word16)
{-# INLINE int16le #-}
int16le :: Monad m => Int16 -> Stream m Word8
int16le :: forall (m :: * -> *). Monad m => Int16 -> Stream m Word8
int16le Int16
i = forall (m :: * -> *). Monad m => Word16 -> Stream m Word8
word16le (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int16
i :: Word16)
{-# INLINE int32be #-}
int32be :: Monad m => Int32 -> Stream m Word8
int32be :: forall (m :: * -> *). Monad m => Int32 -> Stream m Word8
int32be Int32
i = forall (m :: * -> *). Monad m => Word32 -> Stream m Word8
word32be (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
i :: Word32)
{-# INLINE int32le #-}
int32le :: Monad m => Int32 -> Stream m Word8
int32le :: forall (m :: * -> *). Monad m => Int32 -> Stream m Word8
int32le Int32
i = forall (m :: * -> *). Monad m => Word32 -> Stream m Word8
word32le (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
i :: Word32)
{-# INLINE int64be #-}
int64be :: Monad m => Int64 -> Stream m Word8
int64be :: forall (m :: * -> *). Monad m => Int64 -> Stream m Word8
int64be Int64
i = forall (m :: * -> *). Monad m => Word64 -> Stream m Word8
word64be (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
i :: Word64)
{-# INLINE int64le #-}
int64le :: Monad m => Int64 -> Stream m Word8
int64le :: forall (m :: * -> *). Monad m => Int64 -> Stream m Word8
int64le Int64
i = forall (m :: * -> *). Monad m => Word64 -> Stream m Word8
word64le (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
i :: Word64)
{-# INLINE float32be #-}
float32be :: Monad m => Float -> Stream m Word8
float32be :: forall (m :: * -> *). Monad m => Float -> Stream m Word8
float32be = forall (m :: * -> *). Applicative m => Word32 -> Stream m Word8
word32beD forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Word32
castFloatToWord32
{-# INLINE float32le #-}
float32le :: Monad m => Float -> Stream m Word8
float32le :: forall (m :: * -> *). Monad m => Float -> Stream m Word8
float32le = forall (m :: * -> *). Applicative m => Word32 -> Stream m Word8
word32leD forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Word32
castFloatToWord32
{-# INLINE double64be #-}
double64be :: Monad m => Double -> Stream m Word8
double64be :: forall (m :: * -> *). Monad m => Double -> Stream m Word8
double64be = forall (m :: * -> *). Applicative m => Word64 -> Stream m Word8
word64beD forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Word64
castDoubleToWord64
{-# INLINE double64le #-}
double64le :: Monad m => Double -> Stream m Word8
double64le :: forall (m :: * -> *). Monad m => Double -> Stream m Word8
double64le = forall (m :: * -> *). Applicative m => Word64 -> Stream m Word8
word64leD forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Word64
castDoubleToWord64
{-# INLINE charLatin1 #-}
charLatin1 :: Applicative m => Char -> Stream m Word8
charLatin1 :: forall (m :: * -> *). Applicative m => Char -> Stream m Word8
charLatin1 = forall (m :: * -> *) a. Applicative m => a -> Stream m a
Stream.fromPure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Int
ord
{-# INLINE charUtf8 #-}
charUtf8 :: Monad m => Char -> Stream m Word8
charUtf8 :: forall (m :: * -> *). Monad m => Char -> Stream m Word8
charUtf8 = forall (m :: * -> *) a b.
Applicative m =>
Unfold m a b -> a -> Stream m b
Stream.unfold forall (m :: * -> *). Monad m => Unfold m Char Word8
readCharUtf8
{-# INLINE word64host #-}
word64host :: Monad m => Word64 -> Stream m Word8
word64host :: forall (m :: * -> *). Monad m => Word64 -> Stream m Word8
word64host =
#ifdef WORDS_BIGENDIAN
word64be
#else
forall (m :: * -> *). Monad m => Word64 -> Stream m Word8
word64le
#endif
class ToBytes a where
toBytes :: a -> Stream m Word8