Skip to content

Commit

Permalink
Add unsafeFromByteString, unsafeFromShortByteString.
Browse files Browse the repository at this point in the history
They are no more unsafe than unsafeFromLazyByteString.
  • Loading branch information
j6carey committed Sep 23, 2024
1 parent 55d555b commit 6fcef04
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Proto3/Wire/Encode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ module Proto3.Wire.Encode
, messageLength
, toLazyByteString
, unsafeFromLazyByteString
, unsafeFromByteString
, unsafeFromShortByteString

-- * Standard Integers
, int32
Expand Down Expand Up @@ -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 }

Expand Down

0 comments on commit 6fcef04

Please sign in to comment.