{-# LANGUAGE FlexibleContexts     #-}
{-# LANGUAGE TypeFamilies         #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
-- | Module with cereal instances for data types defined in fixed
--   vector
module Data.Vector.Fixed.Instances.Cereal where

import           Data.Vector.Fixed             (Arity)
import qualified Data.Vector.Fixed           as F
import qualified Data.Vector.Fixed.Boxed     as B
import qualified Data.Vector.Fixed.Unboxed   as U
import qualified Data.Vector.Fixed.Primitive as P
import qualified Data.Vector.Fixed.Storable  as S
import           Data.Serialize                (Serialize(..))


instance (Arity n, Serialize a) => Serialize (B.Vec n a) where
  put :: Putter (Vec n a)
put = (a -> PutM ()) -> Putter (Vec n a)
forall (v :: * -> *) a (f :: * -> *) b.
(Vector v a, Applicative f) =>
(a -> f b) -> v a -> f ()
F.mapM_ a -> PutM ()
forall t. Serialize t => Putter t
put
  get :: Get (Vec n a)
get = Get a -> Get (Vec n a)
forall (v :: * -> *) a (f :: * -> *).
(Vector v a, Applicative f) =>
f a -> f (v a)
F.replicateM Get a
forall t. Serialize t => Get t
get

instance (Arity n, P.Prim a, Serialize a) => Serialize (P.Vec n a) where
  put :: Putter (Vec n a)
put = (a -> PutM ()) -> Putter (Vec n a)
forall (v :: * -> *) a (f :: * -> *) b.
(Vector v a, Applicative f) =>
(a -> f b) -> v a -> f ()
F.mapM_ a -> PutM ()
forall t. Serialize t => Putter t
put
  get :: Get (Vec n a)
get = Get a -> Get (Vec n a)
forall (v :: * -> *) a (f :: * -> *).
(Vector v a, Applicative f) =>
f a -> f (v a)
F.replicateM Get a
forall t. Serialize t => Get t
get

instance (Arity n, S.Storable a, Serialize a) => Serialize (S.Vec n a) where
  put :: Putter (Vec n a)
put = (a -> PutM ()) -> Putter (Vec n a)
forall (v :: * -> *) a (f :: * -> *) b.
(Vector v a, Applicative f) =>
(a -> f b) -> v a -> f ()
F.mapM_ a -> PutM ()
forall t. Serialize t => Putter t
put
  get :: Get (Vec n a)
get = Get a -> Get (Vec n a)
forall (v :: * -> *) a (f :: * -> *).
(Vector v a, Applicative f) =>
f a -> f (v a)
F.replicateM Get a
forall t. Serialize t => Get t
get

instance (U.Unbox n a, Serialize a) => Serialize (U.Vec n a) where
  put :: Putter (Vec n a)
put = (a -> PutM ()) -> Putter (Vec n a)
forall (v :: * -> *) a (f :: * -> *) b.
(Vector v a, Applicative f) =>
(a -> f b) -> v a -> f ()
F.mapM_ a -> PutM ()
forall t. Serialize t => Putter t
put
  get :: Get (Vec n a)
get = Get a -> Get (Vec n a)
forall (v :: * -> *) a (f :: * -> *).
(Vector v a, Applicative f) =>
f a -> f (v a)
F.replicateM Get a
forall t. Serialize t => Get t
get

instance (Arity n, Serialize a) => Serialize (F.VecList n a) where
  put :: Putter (VecList n a)
put = (a -> PutM ()) -> Putter (VecList n a)
forall (v :: * -> *) a (f :: * -> *) b.
(Vector v a, Applicative f) =>
(a -> f b) -> v a -> f ()
F.mapM_ a -> PutM ()
forall t. Serialize t => Putter t
put
  get :: Get (VecList n a)
get = Get a -> Get (VecList n a)
forall (v :: * -> *) a (f :: * -> *).
(Vector v a, Applicative f) =>
f a -> f (v a)
F.replicateM Get a
forall t. Serialize t => Get t
get

instance (Serialize a) => Serialize (F.Only a) where
  put :: Putter (Only a)
put (F.Only a
a) = Putter a
forall t. Serialize t => Putter t
put a
a
  get :: Get (Only a)
get = a -> Only a
forall a. a -> Only a
F.Only (a -> Only a) -> Get a -> Get (Only a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Get a
forall t. Serialize t => Get t
get

instance Serialize (F.Empty a) where
  put :: Putter (Empty a)
put Empty a
_ = () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  get :: Get (Empty a)
get = Empty a -> Get (Empty a)
forall (m :: * -> *) a. Monad m => a -> m a
return Empty a
forall k (a :: k). Empty a
F.Empty