module Data.Byteable
( Byteable(..)
, constEqBytes
) where
import Data.ByteString (ByteString)
import Data.List (foldl')
import qualified Data.ByteString as B (length, zipWith)
class Byteable a where
toBytes :: a -> ByteString
instance Byteable ByteString where
toBytes bs = bs
constEqBytes :: Byteable a => a -> a -> Bool
constEqBytes a b = constEqByteString (toBytes a) (toBytes b)
constEqByteString :: ByteString -> ByteString -> Bool
constEqByteString a b
| len /= B.length b = False
| otherwise = foldl' (&&!) True $ B.zipWith (==) a b
where len = B.length a
(&&!) :: Bool -> Bool -> Bool
True &&! True = True
True &&! False = False
False &&! True = False
False &&! False = False