bytes 0.8 → 0.9
raw patch · 3 files changed
+114/−37 lines, 3 filesdep ~binaryPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: binary
API changes (from Hackage documentation)
- Data.Bytes.Get: uncheckedLookAhead :: MonadGet m => Unchecked m -> m (Bytes m)
- Data.Bytes.Get: uncheckedSkip :: MonadGet m => Unchecked m -> m ()
+ Data.Bytes.Get: lookAheadE :: MonadGet m => m (Either a b) -> m (Either a b)
+ Data.Bytes.Get: lookAheadM :: MonadGet m => m (Maybe a) -> m (Maybe a)
- 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.Get: class (Integral (Remaining m), Monad m, Applicative m) => MonadGet m where type family Remaining m :: * type family Bytes m :: * skip = lift . skip ensure = lift . ensure 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: remaining :: MonadGet m => m (Unchecked m)
+ Data.Bytes.Get: remaining :: MonadGet m => m (Remaining m)
Files
- CHANGELOG.markdown +6/−0
- bytes.cabal +2/−2
- src/Data/Bytes/Get.hs +106/−35
CHANGELOG.markdown view
@@ -1,3 +1,9 @@+0.9+-----+* Added proper support for `binary` 0.7.+* Restored `lookAheadM` and `lookAheadE`, thanks to the return of `lookAheadE` in `binary` 0.7.+* Renamed `Unchecked` to `Remaining`, and removed the `uncheckedLookAhead` function, as it is no longer supported downstream.+ 0.8 ----- * Trustworthiness
bytes.cabal view
@@ -1,6 +1,6 @@ name: bytes category: Data, Serialization-version: 0.8+version: 0.9 license: BSD3 cabal-version: >= 1.8 license-file: LICENSE@@ -42,7 +42,7 @@ library build-depends: base >= 4.3 && < 5,- binary >= 0.5 && < 0.8,+ binary >= 0.7 && < 0.8, bytestring >= 0.9 && < 0.11, cereal >= 0.3.5 && < 0.4, containers >= 0.5 && < 1,
src/Data/Bytes/Get.hs view
@@ -3,6 +3,7 @@ {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE DefaultSignatures #-} {-# LANGUAGE Trustworthy #-}+{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-} -------------------------------------------------------------------- -- | -- Copyright : (c) Edward Kmett 2013@@ -37,9 +38,9 @@ import qualified Data.Serialize.Get as S import Data.Word -class (Integral (Unchecked m), Monad m, Applicative m) => MonadGet m where+class (Integral (Remaining m), Monad m, Applicative m) => MonadGet m where -- | An 'Integral' number type used for unchecked skips and counting.- type Unchecked m :: *+ type Remaining m :: * -- | The underlying ByteString type used by this instance type Bytes m :: *@@ -51,13 +52,6 @@ skip = lift . skip #endif - -- | Skip ahead @n@ bytes. No error if there isn't enough bytes.- uncheckedSkip :: Unchecked m -> m ()-#ifndef HLINT- default uncheckedSkip :: (MonadTrans t, MonadGet n, m ~ t n) => Unchecked n -> m ()- 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@@ -70,13 +64,14 @@ -- Fails if @ga@ fails. lookAhead :: m a -> m a - -- | Get the next up to @n@ bytes as a lazy ByteString, without consuming them.- uncheckedLookAhead :: Unchecked m -> m (Bytes m)-#ifndef HLINT- default uncheckedLookAhead :: (MonadTrans t, MonadGet n, m ~ t n) => Unchecked n -> m (Bytes n)- uncheckedLookAhead = lift . uncheckedLookAhead-#endif+ -- | Like 'lookAhead', but consume the input if @gma@ returns 'Just _'.+ -- Fails if @gma@ fails.+ lookAheadM :: m (Maybe a) -> m (Maybe a) + -- | Like 'lookAhead', but consume the input if @gea@ returns 'Right _'.+ -- Fails if @gea@ fails.+ lookAheadE :: m (Either a b) -> m (Either a b)+ -- | Pull @n@ bytes from the input, as a strict ByteString. getBytes :: Int -> m Strict.ByteString #ifndef HLINT@@ -87,9 +82,9 @@ -- | Get the number of remaining unparsed bytes. -- Useful for checking whether all input has been consumed. -- Note that this forces the rest of the input.- remaining :: m (Unchecked m)+ remaining :: m (Remaining m) #ifndef HLINT- default remaining :: (MonadTrans t, MonadGet n, m ~ t n) => m (Unchecked n)+ default remaining :: (MonadTrans t, MonadGet n, m ~ t n) => m (Remaining n) remaining = lift remaining #endif @@ -198,22 +193,22 @@ #endif instance MonadGet B.Get where- type Unchecked B.Get = Int64+ type Remaining B.Get = Int64 type Bytes B.Get = Lazy.ByteString skip = B.skip {-# INLINE skip #-}- uncheckedSkip = B.uncheckedSkip- {-# INLINE uncheckedSkip #-} lookAhead = B.lookAhead {-# INLINE lookAhead #-}- uncheckedLookAhead = B.uncheckedLookAhead- {-# INLINE uncheckedLookAhead #-}+ lookAheadM = B.lookAheadM+ {-# INLINE lookAheadM #-}+ lookAheadE = B.lookAheadE+ {-# INLINE lookAheadE #-} ensure n = do bs <- lookAhead $ getByteString n unless (Strict.length bs >= n) $ fail "ensure: Required more bytes" return bs {-# INLINE ensure #-}- getBytes = B.getBytes+ getBytes = B.getByteString {-# INLINE getBytes #-} remaining = B.remaining {-# INLINE remaining #-}@@ -247,16 +242,16 @@ {-# INLINE getWordhost #-} instance MonadGet S.Get where- type Unchecked S.Get = Int+ type Remaining S.Get = Int type Bytes S.Get = Strict.ByteString skip = S.skip {-# INLINE skip #-}- uncheckedSkip = S.uncheckedSkip- {-# INLINE uncheckedSkip #-} lookAhead = S.lookAhead {-# INLINE lookAhead #-}- uncheckedLookAhead = S.uncheckedLookAhead- {-# INLINE uncheckedLookAhead #-}+ lookAheadM = S.lookAheadM+ {-# INLINE lookAheadM #-}+ lookAheadE = S.lookAheadE+ {-# INLINE lookAheadE #-} getBytes = S.getBytes {-# INLINE getBytes #-} ensure = S.ensure@@ -293,43 +288,119 @@ {-# INLINE getWordhost #-} instance MonadGet m => MonadGet (Lazy.StateT s m) where- type Unchecked (Lazy.StateT s m) = Unchecked m+ type Remaining (Lazy.StateT s m) = Remaining m type Bytes (Lazy.StateT s m) = Bytes m lookAhead (Lazy.StateT m) = Lazy.StateT (lookAhead . m) {-# INLINE lookAhead #-}+ lookAheadM (Lazy.StateT m) = Lazy.StateT (liftM factor . lookAheadE . liftM distribute . m)+ where+ distribute (Nothing, s') = Left (Nothing, s')+ distribute (Just a, s') = Right (Just a, s')+ factor = either id id+ {-# INLINE lookAheadM #-}+ lookAheadE (Lazy.StateT m) = Lazy.StateT (liftM factor . lookAheadE . liftM distribute . m)+ where+ distribute (Left a, s') = Left (Left a, s')+ distribute (Right b, s') = Right (Right b, s')+ factor = either id id+ {-# INLINE lookAheadE #-} instance MonadGet m => MonadGet (Strict.StateT s m) where- type Unchecked (Strict.StateT s m) = Unchecked m+ type Remaining (Strict.StateT s m) = Remaining m type Bytes (Strict.StateT s m) = Bytes m lookAhead (Strict.StateT m) = Strict.StateT (lookAhead . m) {-# INLINE lookAhead #-}+ lookAheadM (Strict.StateT m) = Strict.StateT (liftM factor . lookAheadE . liftM distribute . m)+ where+ distribute (Nothing, s') = Left (Nothing, s')+ distribute (Just a, s') = Right (Just a, s')+ factor = either id id+ {-# INLINE lookAheadM #-}+ lookAheadE (Strict.StateT m) = Strict.StateT (liftM factor . lookAheadE . liftM distribute . m)+ where+ distribute (Left a, s') = Left (Left a, s')+ distribute (Right b, s') = Right (Right b, s')+ factor = either id id+ {-# INLINE lookAheadE #-} instance MonadGet m => MonadGet (ReaderT e m) where- type Unchecked (ReaderT e m) = Unchecked m+ type Remaining (ReaderT e m) = Remaining m type Bytes (ReaderT e m) = Bytes m lookAhead (ReaderT m) = ReaderT (lookAhead . m) {-# INLINE lookAhead #-}+ lookAheadM (ReaderT m) = ReaderT (lookAheadM . m)+ {-# INLINE lookAheadM #-}+ lookAheadE (ReaderT m) = ReaderT (lookAheadE . m)+ {-# INLINE lookAheadE #-} instance (MonadGet m, Monoid w) => MonadGet (Lazy.WriterT w m) where- type Unchecked (Lazy.WriterT w m) = Unchecked m+ type Remaining (Lazy.WriterT w m) = Remaining m type Bytes (Lazy.WriterT w m) = Bytes m lookAhead (Lazy.WriterT m) = Lazy.WriterT (lookAhead m) {-# INLINE lookAhead #-}+ lookAheadM (Lazy.WriterT m) = Lazy.WriterT (liftM factor $ lookAheadE $ liftM distribute m)+ where+ distribute (Nothing, s') = Left (Nothing, s')+ distribute (Just a, s') = Right (Just a, s')+ factor = either id id+ {-# INLINE lookAheadM #-}+ lookAheadE (Lazy.WriterT m) = Lazy.WriterT (liftM factor $ lookAheadE $ liftM distribute m)+ where+ distribute (Left a, s') = Left (Left a, s')+ distribute (Right b, s') = Right (Right b, s')+ factor = either id id+ {-# INLINE lookAheadE #-} instance (MonadGet m, Monoid w) => MonadGet (Strict.WriterT w m) where- type Unchecked (Strict.WriterT w m) = Unchecked m+ type Remaining (Strict.WriterT w m) = Remaining m type Bytes (Strict.WriterT w m) = Bytes m lookAhead (Strict.WriterT m) = Strict.WriterT (lookAhead m) {-# INLINE lookAhead #-}+ lookAheadM (Strict.WriterT m) = Strict.WriterT (liftM factor $ lookAheadE $ liftM distribute m)+ where+ distribute (Nothing, s') = Left (Nothing, s')+ distribute (Just a, s') = Right (Just a, s')+ factor = either id id+ {-# INLINE lookAheadM #-}+ lookAheadE (Strict.WriterT m) = Strict.WriterT (liftM factor $ lookAheadE $ liftM distribute m)+ where+ distribute (Left a, s') = Left (Left a, s')+ distribute (Right b, s') = Right (Right b, s')+ factor = either id id+ {-# INLINE lookAheadE #-} instance (MonadGet m, Monoid w) => MonadGet (Strict.RWST r w s m) where- type Unchecked (Strict.RWST r w s m) = Unchecked m+ type Remaining (Strict.RWST r w s m) = Remaining m type Bytes (Strict.RWST r w s m) = Bytes m lookAhead (Strict.RWST m) = Strict.RWST $ \r s -> lookAhead (m r s) {-# INLINE lookAhead #-}+ lookAheadM (Strict.RWST m) = Strict.RWST (\r s -> liftM factor $ lookAheadE $ liftM distribute $ m r s )+ where+ distribute (Nothing, s',w') = Left (Nothing, s', w')+ distribute (Just a, s',w') = Right (Just a, s', w')+ factor = either id id+ {-# INLINE lookAheadM #-}+ lookAheadE (Strict.RWST m) = Strict.RWST (\r s -> liftM factor $ lookAheadE $ liftM distribute $ m r s)+ where+ distribute (Left a, s', w') = Left (Left a, s', w')+ distribute (Right b, s', w') = Right (Right b, s', w')+ factor = either id id+ {-# INLINE lookAheadE #-} instance (MonadGet m, Monoid w) => MonadGet (Lazy.RWST r w s m) where- type Unchecked (Lazy.RWST r w s m) = Unchecked m+ type Remaining (Lazy.RWST r w s m) = Remaining m type Bytes (Lazy.RWST r w s m) = Bytes m lookAhead (Lazy.RWST m) = Lazy.RWST $ \r s -> lookAhead (m r s) {-# INLINE lookAhead #-}+ lookAheadM (Lazy.RWST m) = Lazy.RWST (\r s -> liftM factor $ lookAheadE $ liftM distribute $ m r s )+ where+ distribute (Nothing, s',w') = Left (Nothing, s', w')+ distribute (Just a, s',w') = Right (Just a, s', w')+ factor = either id id+ {-# INLINE lookAheadM #-}+ lookAheadE (Lazy.RWST m) = Lazy.RWST (\r s -> liftM factor $ lookAheadE $ liftM distribute $ m r s)+ where+ distribute (Left a, s', w') = Left (Left a, s', w')+ distribute (Right b, s', w') = Right (Right b, s', w')+ factor = either id id+ {-# INLINE lookAheadE #-}