{-|
  Copyright   :  (C) 2026, QBayLogic B.V.
  License     :  BSD2 (see the file LICENSE)
  Maintainer  :  QBayLogic B.V. <devops@qbaylogic.com>
-}

{-# OPTIONS_GHC -Wno-orphans #-}

module GHC.Data.FastString.Extra where

import GHC.Data.FastString
  (FastString (..), FastZString, bytesFS, fastZStringToByteString, mkFastStringByteList)
import Data.Binary (Binary (..), Get)
import Data.ByteString (ByteString)
import Data.Hashable (Hashable(hashWithSalt))
import Unsafe.Coerce (unsafeCoerce)

instance Hashable FastString where
  hashWithSalt :: Int -> FastString -> Int
hashWithSalt Int
salt FastString
fs = Int -> Int -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (FastString -> Int
uniq FastString
fs)

instance Binary FastString where
  put :: FastString -> Put
put FastString
str = ByteString -> Put
forall t. Binary t => t -> Put
put (ByteString -> Put) -> ByteString -> Put
forall a b. (a -> b) -> a -> b
$ FastString -> ByteString
bytesFS FastString
str
  get :: Get FastString
get = [Word8] -> FastString
mkFastStringByteList ([Word8] -> FastString) -> Get [Word8] -> Get FastString
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Get [Word8]
forall t. Binary t => Get t
get

instance Binary FastZString where
  put :: FastZString -> Put
put = ByteString -> Put
forall t. Binary t => t -> Put
put (ByteString -> Put)
-> (FastZString -> ByteString) -> FastZString -> Put
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FastZString -> ByteString
fastZStringToByteString
  get :: Get FastZString
get = Get ByteString -> Get FastZString
forall a b. a -> b
unsafeCoerce (Get ByteString
forall t. Binary t => Get t
get :: Get ByteString)