bytes icon indicating copy to clipboard operation
bytes copied to clipboard

I miss isolate

Open jprupp opened this issue 4 years ago • 0 comments

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.

jprupp avatar Feb 20 '21 19:02 jprupp