{-# LANGUAGE FlexibleInstances #-}
module Data.ByteString.Convert
  ( AsByteString(..)
  )
where

import qualified Data.ByteString               as B
import qualified Data.ByteString.Lazy          as L
import           Data.Word

-- |Convert to/from strict ByteStrings
class AsByteString a where
  toByteString :: a -> B.ByteString
  fromByteString :: B.ByteString -> a

instance AsByteString B.ByteString where
  toByteString :: ByteString -> ByteString
toByteString   = ByteString -> ByteString
forall a. a -> a
id
  fromByteString :: ByteString -> ByteString
fromByteString = ByteString -> ByteString
forall a. a -> a
id

instance AsByteString L.ByteString where
  toByteString :: ByteString -> ByteString
toByteString   = ByteString -> ByteString
L.toStrict
  fromByteString :: ByteString -> ByteString
fromByteString = ByteString -> ByteString
L.fromStrict

instance AsByteString [Word8] where
  toByteString :: [Word8] -> ByteString
toByteString   = [Word8] -> ByteString
B.pack
  fromByteString :: ByteString -> [Word8]
fromByteString = ByteString -> [Word8]
B.unpack