ekmett / bytes

Serialization primitives that work with both cereal and binary.
http://hackage.haskell.org/package/bytes
Other
22 stars 13 forks source link

I miss isolate #55

Open jprupp opened 3 years ago

jprupp commented 3 years ago

The isolate function available in both binary and cereal provides a very useful abstraction.

With isolate:

isolate (fromIntegral len) $ case cmd of
  MCVersion -> MVersion <$> get
  MCAddr -> MAddr <$> get
  _ -> fail $ "get: command " ++ show cmd ++ " should not carry a payload"

Without isolate:

ensure (fromIntegral len) >>= \bs ->
  let f = case cmd of
          MCVersion -> MVersion <$> deserialize
          MCAddr -> MAddr <$> deserialize
          _ -> fail $ "get: command " ++ show cmd ++ " should not carry a payload"
  either fail return (runGetS f bs)

Getting out of the monad to get right back into it feels clumsy.