protocol-buffers icon indicating copy to clipboard operation
protocol-buffers copied to clipboard

Add strict ByteString support

Open schernichkin opened this issue 6 years ago • 0 comments

Exposed API use lazy bytestrings, however internally protobuf works with strict chunks. Some libraries can provide stream of strict bytestrings directly. Having an API which accepts strict bytestrings will allow to get get rid of unnecessary wrapping when feeding protobuf deserializer. Example:

protobufSink :: (Monad m, ReflectDescriptor a, Wire a) => ConduitT ByteString {- strict, taken from http-conduit -} o m a
protobufSink = do
  maybeInput <- await
  case maybeInput of 
    Just input -> go $ runGet messageGetM (LBS.fromStrict input) {- unnecessary wrapping -}
      where 
        go (Failed i s) = fail ("Failed at "++show i++" : "++s)
        go (Finished _ _ a) = return a  
        go (Partial f) = f . fmap LBS.fromStrict {- unnecessary wrapping -} <$> await >>= go
    Nothing -> fail "No input."

schernichkin avatar Dec 27 '18 16:12 schernichkin