{-# LANGUAGE CPP #-}

module Network.QUIC.Imports (
    Bytes
  , ByteString(..)
  , ShortByteString(..)
  , Builder
  , module Control.Applicative
  , module Control.Monad
  , module Data.Bits
  , module Data.Foldable
  , module Data.IORef
  , module Data.Int
  , module Data.Monoid
  , module Data.Ord
  , module Data.Word
  , module Data.Array
  , module Data.Array.IO
  , module Data.Maybe
  , module Numeric
  , module Network.ByteOrder
  , module Network.QUIC.Utils
#if !MIN_VERSION_base(4,17,0)
  , (!<<.), (!>>.)
#endif
  , atomicModifyIORef''
  , copyBS
  ) where

import Control.Applicative
import Control.Monad
import Data.Array
import Data.Array.IO
import Data.Bits
import Data.ByteString.Builder (Builder)
import Data.ByteString.Internal (ByteString(..))
import Data.ByteString.Short.Internal (ShortByteString(..))
import Data.Foldable
import Data.IORef
import Data.Int
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.Word
import Foreign.ForeignPtr
import Foreign.Marshal.Utils (copyBytes)
import Foreign.Ptr
import Network.ByteOrder
import Network.QUIC.Utils
import Numeric

-- | All internal byte sequences.
--   `ByteString` should be used for FFI related stuff.
type Bytes = ShortByteString

#if !MIN_VERSION_base(4,17,0)
infixl 8 !<<.
(!<<.) :: Bits a => a -> Int -> a
(!<<.) = unsafeShiftL

infixl 8 !>>.
(!>>.) :: Bits a => a -> Int -> a
(!>>.) = unsafeShiftR
#endif

atomicModifyIORef'' :: IORef a -> (a -> a) -> IO ()
atomicModifyIORef'' :: forall a. IORef a -> (a -> a) -> IO ()
atomicModifyIORef'' IORef a
ref a -> a
f = IORef a -> (a -> (a, ())) -> IO ()
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef a
ref ((a -> (a, ())) -> IO ()) -> (a -> (a, ())) -> IO ()
forall a b. (a -> b) -> a -> b
$ \a
x -> (a -> a
f a
x, ())

copyBS :: Buffer -> ByteString -> IO Int
copyBS :: Buffer -> ByteString -> IO Int
copyBS Buffer
dst (PS ForeignPtr Word8
fptr Int
off Int
len) = ForeignPtr Word8 -> (Buffer -> IO Int) -> IO Int
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
fptr ((Buffer -> IO Int) -> IO Int) -> (Buffer -> IO Int) -> IO Int
forall a b. (a -> b) -> a -> b
$ \Buffer
src0 -> do
    let src :: Ptr b
src = Buffer
src0 Buffer -> Int -> Ptr b
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
off
    Buffer -> Buffer -> Int -> IO ()
forall a. Ptr a -> Ptr a -> Int -> IO ()
copyBytes Buffer
dst Buffer
forall {b}. Ptr b
src Int
len
    Int -> IO Int
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Int
len