bytes 0.3 → 0.5
raw patch · 5 files changed
+68/−3 lines, 5 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Data.Bytes.Get: ensure :: MonadGet m => Int -> m ByteString
+ Data.Bytes.Serial: instance Serial ()
+ Data.Bytes.Serial: instance Serial ByteString
+ Data.Bytes.Serial: instance Serial Double
+ Data.Bytes.Serial: instance Serial Float
+ Data.Bytes.Serial: restore :: (MonadGet m, Storable a) => m a
+ Data.Bytes.Serial: store :: (MonadPut m, Storable a) => a -> m ()
- Data.Bytes.Get: class (Integral (Unchecked m), Monad m) => MonadGet m where type family Unchecked m :: * type family Bytes m :: * skip = lift . skip uncheckedSkip = lift . uncheckedSkip uncheckedLookAhead = lift . uncheckedLookAhead getBytes = lift . getBytes remaining = lift remaining isEmpty = lift isEmpty getWord8 = lift getWord8 getByteString = lift . getByteString getLazyByteString = lift . getLazyByteString getWord16be = lift getWord16be getWord16le = lift getWord16le getWord16host = lift getWord16host getWord32be = lift getWord32be getWord32le = lift getWord32le getWord32host = lift getWord32host getWord64be = lift getWord64be getWord64le = lift getWord64le getWord64host = lift getWord64host getWordhost = lift getWordhost
+ Data.Bytes.Get: class (Integral (Unchecked m), Monad m, Applicative m) => MonadGet m where type family Unchecked m :: * type family Bytes m :: * skip = lift . skip uncheckedSkip = lift . uncheckedSkip ensure = lift . ensure uncheckedLookAhead = lift . uncheckedLookAhead getBytes = lift . getBytes remaining = lift remaining isEmpty = lift isEmpty getWord8 = lift getWord8 getByteString = lift . getByteString getLazyByteString = lift . getLazyByteString getWord16be = lift getWord16be getWord16le = lift getWord16le getWord16host = lift getWord16host getWord32be = lift getWord32be getWord32le = lift getWord32le getWord32host = lift getWord32host getWord64be = lift getWord64be getWord64le = lift getWord64le getWord64host = lift getWord64host getWordhost = lift getWordhost
- Data.Bytes.Put: class Monad m => MonadPut m where putWord8 = lift . putWord8 putByteString = lift . putByteString putLazyByteString = lift . putLazyByteString flush = lift flush putWord16le = lift . putWord16le putWord16be = lift . putWord16be putWord16host = lift . putWord16host putWord32le = lift . putWord32le putWord32be = lift . putWord32be putWord32host = lift . putWord32host putWord64le = lift . putWord64le putWord64be = lift . putWord64be putWord64host = lift . putWord64host putWordhost = lift . putWordhost
+ Data.Bytes.Put: class (Applicative m, Monad m) => MonadPut m where putWord8 = lift . putWord8 putByteString = lift . putByteString putLazyByteString = lift . putLazyByteString flush = lift flush putWord16le = lift . putWord16le putWord16be = lift . putWord16be putWord16host = lift . putWord16host putWord32le = lift . putWord32le putWord32be = lift . putWord32be putWord32host = lift . putWord32host putWord64le = lift . putWord64le putWord64be = lift . putWord64be putWord64host = lift . putWord64host putWordhost = lift . putWordhost
Files
- CHANGELOG.markdown +4/−0
- bytes.cabal +1/−1
- src/Data/Bytes/Get.hs +17/−1
- src/Data/Bytes/Put.hs +2/−1
- src/Data/Bytes/Serial.hs +44/−0
CHANGELOG.markdown view
@@ -1,3 +1,7 @@+0.4+---+* Added a missing () instance+ 0.3 ----- * Added `Serial2` and various missing `Serial1` instances.
bytes.cabal view
@@ -1,6 +1,6 @@ name: bytes category: Data, Serialization-version: 0.3+version: 0.5 license: BSD3 cabal-version: >= 1.8 license-file: LICENSE
src/Data/Bytes/Get.hs view
@@ -21,6 +21,7 @@ ( MonadGet(..) ) where +import Control.Applicative import Control.Monad.Reader import Control.Monad.RWS.Lazy as Lazy import Control.Monad.RWS.Strict as Strict@@ -35,7 +36,7 @@ import qualified Data.Serialize.Get as S import Data.Word -class (Integral (Unchecked m), Monad m) => MonadGet m where+class (Integral (Unchecked m), Monad m, Applicative m) => MonadGet m where -- | An 'Integral' number type used for unchecked skips and counting. type Unchecked m :: * @@ -56,6 +57,14 @@ uncheckedSkip = lift . uncheckedSkip #endif + -- | If at least @n@ bytes are available return at least that much of the current input.+ -- Otherwise fail.+ ensure :: Int -> m Strict.ByteString+#ifndef HLINT+ default ensure :: (MonadTrans t, MonadGet n, m ~ t n) => Int -> m Strict.ByteString+ ensure = lift . ensure+#endif+ -- | Run @ga@, but return without consuming its input. -- Fails if @ga@ fails. lookAhead :: m a -> m a@@ -210,6 +219,11 @@ {-# INLINE lookAheadE #-} uncheckedLookAhead = B.uncheckedLookAhead {-# INLINE uncheckedLookAhead #-}+ ensure n = do+ bs <- lookAhead $ getByteString n+ unless (Strict.length bs >= n) $ fail "ensure: Required more bytes"+ return bs+ {-# INLINE ensure #-} getBytes = B.getBytes {-# INLINE getBytes #-} remaining = B.remaining@@ -260,6 +274,8 @@ {-# INLINE uncheckedLookAhead #-} getBytes = S.getBytes {-# INLINE getBytes #-}+ ensure = S.ensure+ {-# INLINE ensure #-} remaining = S.remaining {-# INLINE remaining #-} isEmpty = S.isEmpty
src/Data/Bytes/Put.hs view
@@ -24,6 +24,7 @@ ( MonadPut(..) ) where +import Control.Applicative import Control.Monad.Reader import Control.Monad.RWS.Lazy as Lazy import Control.Monad.RWS.Strict as Strict@@ -41,7 +42,7 @@ -- MonadPut ------------------------------------------------------------------------------ -class Monad m => MonadPut m where+class (Applicative m, Monad m) => MonadPut m where -- | Efficiently write a byte into the output buffer putWord8 :: Word8 -> m () #ifndef HLINT
src/Data/Bytes/Serial.hs view
@@ -4,7 +4,11 @@ {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE UndecidableInstances #-}+#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 704+{-# LANGUAGE Trustworthy #-}+#endif -------------------------------------------------------------------- -- | -- Copyright : (c) Edward Kmett 2013@@ -26,14 +30,22 @@ , Serial1(..), serialize1, deserialize1 , GSerial1(..) , Serial2(..), serialize2, deserialize2+ , store, restore ) where import Control.Monad import Data.Bytes.Get import Data.Bytes.Put+import Data.ByteString.Internal+import Data.ByteString.Lazy as Lazy+import Data.ByteString as Strict import Data.Int import Data.Word+import Foreign.ForeignPtr+import Foreign.Ptr+import Foreign.Storable import GHC.Generics+import System.IO.Unsafe ------------------------------------------------------------------------------ -- Serialization@@ -52,6 +64,19 @@ deserialize = liftM to gdeserialize #endif +instance Serial Strict.ByteString where+ serialize bs = putWord32host (fromIntegral (Strict.length bs)) >> putByteString bs+ deserialize = do+ n <- getWord32host+ getByteString (fromIntegral n)++instance Serial Lazy.ByteString where+ serialize bs = putWord64host (fromIntegral (Lazy.length bs)) >> putLazyByteString bs+ deserialize = do+ n <- getWord64host+ getLazyByteString (fromIntegral n)++instance Serial () instance Serial a => Serial [a] instance Serial a => Serial (Maybe a) instance (Serial a, Serial b) => Serial (Either a b)@@ -61,6 +86,25 @@ instance (Serial a, Serial b, Serial c, Serial d, Serial e) => Serial (a, b, c, d, e) instance Serial Bool++store :: (MonadPut m, Storable a) => a -> m ()+store a = putByteString bs+ where bs = unsafePerformIO $ create (sizeOf a) $ \ p -> poke (castPtr p) a++restore :: forall m a. (MonadGet m, Storable a) => m a+restore = do+ let required = sizeOf (undefined :: a)+ PS fp o n <- getByteString required+ unless (n >= required) $ fail "restore: Required more bytes"+ return $ unsafePerformIO $ withForeignPtr fp $ \p -> peekByteOff p o++instance Serial Double where+ serialize = store+ deserialize = restore++instance Serial Float where+ serialize = store+ deserialize = restore instance Serial Char where serialize = putWord32host . fromIntegral . fromEnum