packages feed

vector-bytes-instances (empty) → 0.1

raw patch · 5 files changed

+196/−0 lines, 5 filesdep +basedep +bytesdep +tastysetup-changed

Dependencies added: base, bytes, tasty, tasty-quickcheck, vector, vector-bytes-instances

Files

+ Data/Vector/Serial.hs view
@@ -0,0 +1,94 @@+{-# OPTIONS_GHC -fno-warn-orphans #-}++module Data.Vector.Serial+  ( genericDeserializeVector+  , genericDeserializeVectorWith+  , genericSerializeVector+  , genericSerializeVectorWith+  ) where++import Data.Bytes.Serial as Bytes+import Data.Bytes.Get as Bytes+import Data.Bytes.Put as Bytes+import qualified Data.Vector.Generic as G+import qualified Data.Vector.Generic.Mutable as GM+import qualified Data.Vector.Unboxed as U+import qualified Data.Vector.Storable as S+import qualified Data.Vector.Primitive as P+import Data.Vector (Vector)+import Foreign.Storable (Storable)+import System.IO.Unsafe++--------------------------------------------------------------------------------++-- | Orphan in "Data.Vector.Serial". Boxed, generic vectors.+instance Bytes.Serial a => Bytes.Serial (Vector a) where+    serialize = genericSerializeVector+    deserialize = genericDeserializeVector+    {-# INLINE deserialize #-}++-- | Orphan in "Data.Vector.Serial". Unboxed vectors+instance (U.Unbox a, Bytes.Serial a) => Bytes.Serial (U.Vector a) where+    serialize = genericSerializeVector+    deserialize = genericDeserializeVector+    {-# INLINE deserialize #-}++-- | Orphan in "Data.Vector.Serial". Primitive vectors+instance (P.Prim a, Bytes.Serial a) => Bytes.Serial (P.Vector a) where+    serialize = genericSerializeVector+    deserialize = genericDeserializeVector+    {-# INLINE deserialize #-}++-- | Orphan in "Data.Vector.Serial". Storable vectors+instance (Storable a, Bytes.Serial a) => Bytes.Serial (S.Vector a) where+    serialize = genericSerializeVector+    deserialize = genericDeserializeVector+    {-# INLINE deserialize #-}++------------------------------------------------------------------------++-- | Deserialize vector using custom parsers.+genericDeserializeVectorWith+    :: (G.Vector v a, Bytes.Serial a, Bytes.MonadGet m)+    => m Int       -- ^ Parser for vector size+    -> m a         -- ^ Parser for vector's element+    -> m (v a)+{-# INLINE genericDeserializeVectorWith #-}+genericDeserializeVectorWith deserializeN deserializeA = do+    n <- deserializeN+    v <- return $ unsafePerformIO $ GM.unsafeNew n+    let go 0 = return ()+        go i = do x <- deserializeA+                  () <- return $ unsafePerformIO $ GM.unsafeWrite v (n-i) x+                  go (i-1)+    () <- go n+    return $ unsafePerformIO $ G.unsafeFreeze v++-- | Generic serialize for anything in the 'G.Vector' class which uses custom+--   encoders.+genericSerializeVectorWith+    :: (G.Vector v a, Bytes.Serial a, Bytes.MonadPut m)+    => (Int -> m ())  -- ^ Encoder for vector size+    -> (a   -> m ())  -- ^ Encoder for vector's element+    -> v a+    -> m ()+{-# INLINE genericSerializeVectorWith #-}+genericSerializeVectorWith serializeN serializeA v = do+    serializeN (G.length v)+    G.mapM_ serializeA v++-- | Generic function for vector deserialization.+genericDeserializeVector+    :: (G.Vector v a, Bytes.Serial a, Bytes.MonadGet m)+    => m (v a)+{-# INLINE genericDeserializeVector #-}+genericDeserializeVector = genericDeserializeVectorWith deserialize deserialize++-- | Generic serialize for anything in the 'G.Vector' class.+genericSerializeVector+    :: (G.Vector v a, Bytes.Serial a, Bytes.MonadPut m)+    => v a+    -> m ()+{-# INLINE genericSerializeVector #-}+genericSerializeVector = genericSerializeVectorWith serialize serialize+
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Renzo Carbonara (c) 2016++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Don Stewart nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ test/Main.hs view
@@ -0,0 +1,29 @@+{-# LANGUAGE ScopedTypeVariables #-}++import qualified Data.Bytes.Get as Bytes+import qualified Data.Bytes.Put as Bytes+import qualified Data.Bytes.Serial as Bytes+import qualified Data.Vector as V+import qualified Data.Vector.Generic as VG+import qualified Data.Vector.Unboxed as VU+import Data.Vector.Serial ()+import qualified Data.Vector.Storable as VS+import Test.Tasty+import Test.Tasty.QuickCheck++roundTrip+  :: forall v a+  .  (Eq (v a), Bytes.Serial (v a), VG.Vector v a)+  => v a+  -> Property+roundTrip v =+    let ev' = Bytes.runGetS Bytes.deserialize (Bytes.runPutS (Bytes.serialize v))+    in property (ev' == Right v)++main = defaultMain $ testGroup "Vector Serial instances"+    [ testProperty "Unboxed"  $ roundTrip $ VU.enumFromTo z 100+    , testProperty "Storable" $ roundTrip $ VS.enumFromTo z 100+    , testProperty "Boxed"    $ roundTrip $ V.enumFromTo  z 100+    ]+  where+    z = 0 :: Int
+ vector-bytes-instances.cabal view
@@ -0,0 +1,41 @@+name: vector-bytes-instances+version: 0.1+synopsis: Serial (from the bytes package) for Vector (from the vector package)+description:+  Serial (from the bytes package) for Vector (from the vector package)+  .+  Based on the original BSD3-licensed work by Don Stewart in the+  vector-binary-instances library.+homepage: https://github.com/k0001/vector-bytes-instances+bug-reports: https://github.com/k0001/vector-bytes-instances/issues+license: BSD3+license-file: LICENSE+author: Renzo Carbonara+maintainer: renzocarbonara¡gmail!com+stability: Experimental+category: Data+build-type: Simple+cabal-version: >=1.8++library+  ghc-options: -Wall -O2+  exposed-modules: Data.Vector.Serial+  build-depends:+    base > 3 && < 4.10,+    vector >= 0.6 && < 0.12,+    bytes >= 0.13 && < 0.16++test-suite tests+  type: exitcode-stdio-1.0+  main-is: test/Main.hs+  build-depends:+      base+    , vector-bytes-instances+    , vector+    , bytes+    , tasty+    , tasty-quickcheck++source-repository head+  type: git+  location: https://github.com/k0001/vector-bytes-instances