vector-binary-instances 0.1 → 0.1.1
raw patch · 2 files changed
+29/−1 lines, 2 files
Files
- Data/Vector/Binary.hs +28/−0
- vector-binary-instances.cabal +1/−1
Data/Vector/Binary.hs view
@@ -36,11 +36,38 @@ import qualified Data.Vector.Generic as G import Control.Monad +import System.IO.Unsafe+import qualified Data.Vector.Generic.Mutable as M+ instance (G.Vector v a, Binary a) => Binary (v a) where put v = do put (G.length v) mapM_ put (G.toList v) + -- this is morally sound, if very awkward.+ -- all effects are contained, and can't escape the unsafeFreeze+ {-# INLINE get #-}+ get = do+ n <- get++ -- new unitinialized array+ mv <- lift $ M.new n++ let fill i+ | i < n = do+ x <- get+ (unsafePerformIO $ M.unsafeWrite mv i x) `seq` return ()+ fill (i+1)++ | otherwise = return ()++ fill 0++ lift $ G.unsafeFreeze mv++lift = return .unsafePerformIO++{- -- Uses too much space going via lists. -- XXX todo: fill directly from a stream. get = do@@ -60,3 +87,4 @@ x `seq` go (x:xs) (i-1) {-# INLINE getMany #-} +-}
vector-binary-instances.cabal view
@@ -7,7 +7,7 @@ -- The package version. See the Haskell package versioning policy -- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for -- standards guiding when and how versions should be incremented.-Version: 0.1+Version: 0.1.1 -- A short (one-line) description of the package. Synopsis: Instances of Data.Binary and Data.Serialize for vector