From 6fcef0427abf4f02ec28fd041bd27c647234655b Mon Sep 17 00:00:00 2001 From: John Carey Date: Mon, 23 Sep 2024 15:26:45 -0700 Subject: [PATCH] Add unsafeFromByteString, unsafeFromShortByteString. They are no more unsafe than unsafeFromLazyByteString. --- src/Proto3/Wire/Encode.hs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Proto3/Wire/Encode.hs b/src/Proto3/Wire/Encode.hs index 9688cef..027f30b 100644 --- a/src/Proto3/Wire/Encode.hs +++ b/src/Proto3/Wire/Encode.hs @@ -59,6 +59,8 @@ module Proto3.Wire.Encode , messageLength , toLazyByteString , unsafeFromLazyByteString + , unsafeFromByteString + , unsafeFromShortByteString -- * Standard Integers , int32 @@ -190,15 +192,25 @@ messageLength = fromIntegral . fst . RB.runBuildR . unMessageBuilder toLazyByteString :: MessageBuilder -> BL.ByteString toLazyByteString = RB.toLazyByteString . unMessageBuilder --- | This lets you cast an arbitrary `ByteString` to a `MessageBuilder`, whether +-- | This lets you cast an arbitrary 'BL.ByteString' to a `MessageBuilder`, whether -- or not the `ByteString` corresponds to a valid serialized protobuf message -- -- Do not use this function unless you know what you're doing because it lets --- you assemble malformed protobuf `MessageBuilder`s +-- you assemble malformed protobuf `MessageBuilder`s. unsafeFromLazyByteString :: BL.ByteString -> MessageBuilder unsafeFromLazyByteString bytes' = MessageBuilder { unMessageBuilder = RB.lazyByteString bytes' } +-- | Like 'unsafeFromLazyByteString' only for strict 'B.ByteString's. +unsafeFromByteString :: B.ByteString -> MessageBuilder +unsafeFromByteString bytes' = + MessageBuilder { unMessageBuilder = RB.byteString bytes' } + +-- | Like 'unsafeFromLazyByteString' only for 'BS.ShortByteString's. +unsafeFromShortByteString :: BS.ShortByteString -> MessageBuilder +unsafeFromShortByteString bytes' = + MessageBuilder { unMessageBuilder = RB.shortByteString bytes' } + newtype MessageBoundedPrim w = MessageBoundedPrim { unMessageBoundedPrim :: Prim.BoundedPrim w }