{-# LANGUAGE ScopedTypeVariables, Unsafe #-}
{-# OPTIONS_GHC -Wall #-}
module Data.Vector.Storable.UnsafeSerialize (
unsafeGetVector
, unsafePutVector
) where
import Control.Monad (when)
import qualified Data.ByteString.Internal as BS
import Data.Int (Int64)
import Data.Serialize (Get, getBytes, putByteString, Putter, Serialize(..))
import Data.Vector.Storable ( unsafeFromForeignPtr0
, unsafeToForeignPtr0
, Vector)
import Data.Vector.Storable.Internal (updPtr)
import Foreign.ForeignPtr (castForeignPtr)
import Foreign.Marshal.Array (advancePtr)
import Foreign.Storable (Storable, sizeOf)
unsafeGetVector :: forall a. Storable a => Get (Vector a)
{-# INLINE unsafeGetVector #-}
unsafeGetVector :: Get (Vector a)
unsafeGetVector = do
Int64
len64 <- Get Int64
forall t. Serialize t => Get t
get :: Get Int64
Bool -> Get () -> Get ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int64
len64 Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
forall a. Bounded a => a
maxBound :: Int)) (Get () -> Get ()) -> Get () -> Get ()
forall a b. (a -> b) -> a -> b
$
String -> Get ()
forall (m :: * -> *) a. MonadFail m => String -> m a
fail "Host can't deserialize a Vector longer than (maxBound :: Int)"
let len :: Int
len = Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
len64
nbytes :: Int
nbytes = Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
* a -> Int
forall a. Storable a => a -> Int
sizeOf (a
forall a. HasCallStack => a
undefined :: a)
ByteString
bs <- Int -> Get ByteString
getBytes Int
nbytes
let (fp :: ForeignPtr Word8
fp, off :: Int
off, _) = ByteString -> (ForeignPtr Word8, Int, Int)
BS.toForeignPtr ByteString
bs
fp' :: ForeignPtr Word8
fp' | Int
off Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= 0 = (Ptr Word8 -> Ptr Word8) -> ForeignPtr Word8 -> ForeignPtr Word8
forall a. (Ptr a -> Ptr a) -> ForeignPtr a -> ForeignPtr a
updPtr (Ptr Word8 -> Int -> Ptr Word8
forall a. Storable a => Ptr a -> Int -> Ptr a
`advancePtr` Int
off) ForeignPtr Word8
fp
| Bool
otherwise = ForeignPtr Word8
fp
Vector a -> Get (Vector a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector a -> Get (Vector a)) -> Vector a -> Get (Vector a)
forall a b. (a -> b) -> a -> b
$ ForeignPtr a -> Int -> Vector a
forall a. Storable a => ForeignPtr a -> Int -> Vector a
unsafeFromForeignPtr0 (ForeignPtr Word8 -> ForeignPtr a
forall a b. ForeignPtr a -> ForeignPtr b
castForeignPtr ForeignPtr Word8
fp') Int
len
unsafePutVector :: forall a. Storable a => Putter (Vector a)
{-# INLINE unsafePutVector #-}
unsafePutVector :: Putter (Vector a)
unsafePutVector v :: Vector a
v = do
let (fp :: ForeignPtr a
fp, len :: Int
len) = Vector a -> (ForeignPtr a, Int)
forall a. Storable a => Vector a -> (ForeignPtr a, Int)
unsafeToForeignPtr0 Vector a
v
nbytes :: Int
nbytes = Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
* a -> Int
forall a. Storable a => a -> Int
sizeOf (a
forall a. HasCallStack => a
undefined :: a)
bs :: ByteString
bs = ForeignPtr Word8 -> Int -> Int -> ByteString
BS.fromForeignPtr (ForeignPtr a -> ForeignPtr Word8
forall a b. ForeignPtr a -> ForeignPtr b
castForeignPtr ForeignPtr a
fp) 0 Int
nbytes
Putter Int64
forall t. Serialize t => Putter t
put (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len :: Int64)
Putter ByteString
putByteString ByteString
bs