{-
  Copyright 2016 Awake Networks

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-}

-- | Low level functions for writing the protobufs wire format.
--
-- Because protobuf messages are encoded as a collection of fields,
-- one can use the 'Monoid' instance for 'MessageBuilder' to encode multiple
-- fields.
--
-- One should be careful to make sure that 'FieldNumber's appear in
-- increasing order.
--
-- In protocol buffers version 3, all fields are optional. To omit a value
-- for a field, simply do not append it to the 'MessageBuilder'. One can
-- create functions for wrapping optional fields with a 'Maybe' type.
--
-- Similarly, repeated fields can be encoded by concatenating several values
-- with the same 'FieldNumber'.
--
-- For example:
--
-- > strings :: Foldable f => FieldNumber -> f String -> MessageBuilder
-- > strings = foldMap . string
-- >
-- > 1 `strings` Just "some string" <>
-- > 2 `strings` [ "foo", "bar", "baz" ]

{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}

module Proto3.Wire.Encode
    ( -- * `MessageBuilder` type
      MessageBuilder
    , reverseMessageBuilder
    , vectorMessageBuilder
    , messageLength
    , toLazyByteString
    , unsafeFromLazyByteString

      -- * Standard Integers
    , int32
    , int64
      -- * Unsigned Integers
    , uint32
    , uint64
      -- * Signed Integers
    , sint32
    , sint64
      -- * Non-varint Numbers
    , fixed32
    , fixed64
    , sfixed32
    , sfixed64
    , float
    , double
    , enum
    , bool
      -- * Strings
    , bytes
    , string
    , text
    , shortText
    , byteString
    , lazyByteString
    , shortByteString
      -- * Embedded Messages
    , embedded
      -- * Packed repeated fields
    , packedVarints
    , packedVarintsV
    , packedBoolsV
    , packedFixed32
    , packedFixed32V
    , packedFixed64
    , packedFixed64V
    , packedFloats
    , packedFloatsV
    , packedDoubles
    , packedDoublesV
      -- * ZigZag codec
    , zigZagEncode
    ) where

import           Data.Bits                     ( (.|.), shiftL, shiftR, xor,
                                                 FiniteBits, finiteBitSize )
import qualified Data.ByteString               as B
import qualified Data.ByteString.Lazy          as BL
import qualified Data.ByteString.Short         as BS
import           Data.Coerce                   ( coerce )
import           Data.Int                      ( Int32, Int64 )
import qualified Data.Text.Lazy                as Text.Lazy
import qualified Data.Text.Short               as Text.Short
import           Data.Vector.Generic           ( Vector )
import           Data.Word                     ( Word8, Word32, Word64 )
import           GHC.TypeLits                  ( KnownNat, Nat, type (+) )
import           Parameterized.Data.Semigroup  ( PNullary, PSemigroup(..),
                                                 (&<>) )
import           Parameterized.Data.Monoid     ( PMEmpty(..) )
import qualified Proto3.Wire.Reverse           as RB
import qualified Proto3.Wire.Reverse.Prim      as Prim
import           Proto3.Wire.Class
import           Proto3.Wire.Types

-- $setup
-- >>> :set -XOverloadedStrings -XOverloadedLists
-- >>> :module Proto3.Wire.Encode Proto3.Wire.Class Data.Word

-- | zigzag-encoded numeric type.
zigZagEncode :: (Num a, FiniteBits a) => a -> a
zigZagEncode :: forall a. (Num a, FiniteBits a) => a -> a
zigZagEncode a
i = (a
i a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftL` Int
1) a -> a -> a
forall a. Bits a => a -> a -> a
`xor` (a
i a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftR` Int
n)
  where n :: Int
n = a -> Int
forall b. FiniteBits b => b -> Int
finiteBitSize a
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
{-# INLINE zigZagEncode #-}

-- | A `MessageBuilder` represents a serialized protobuf message
--
-- Use the utilities provided by this module to create `MessageBuilder`s
--
-- You can concatenate two messages using the `Monoid` instance for
-- `MessageBuilder`
--
-- Use `toLazyByteString` when you're done assembling the `MessageBuilder`
newtype MessageBuilder = MessageBuilder { MessageBuilder -> BuildR
unMessageBuilder :: RB.BuildR }
  deriving (Semigroup MessageBuilder
MessageBuilder
Semigroup MessageBuilder =>
MessageBuilder
-> (MessageBuilder -> MessageBuilder -> MessageBuilder)
-> ([MessageBuilder] -> MessageBuilder)
-> Monoid MessageBuilder
[MessageBuilder] -> MessageBuilder
MessageBuilder -> MessageBuilder -> MessageBuilder
forall a.
Semigroup a =>
a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
$cmempty :: MessageBuilder
mempty :: MessageBuilder
$cmappend :: MessageBuilder -> MessageBuilder -> MessageBuilder
mappend :: MessageBuilder -> MessageBuilder -> MessageBuilder
$cmconcat :: [MessageBuilder] -> MessageBuilder
mconcat :: [MessageBuilder] -> MessageBuilder
Monoid, NonEmpty MessageBuilder -> MessageBuilder
MessageBuilder -> MessageBuilder -> MessageBuilder
(MessageBuilder -> MessageBuilder -> MessageBuilder)
-> (NonEmpty MessageBuilder -> MessageBuilder)
-> (forall b. Integral b => b -> MessageBuilder -> MessageBuilder)
-> Semigroup MessageBuilder
forall b. Integral b => b -> MessageBuilder -> MessageBuilder
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
$c<> :: MessageBuilder -> MessageBuilder -> MessageBuilder
<> :: MessageBuilder -> MessageBuilder -> MessageBuilder
$csconcat :: NonEmpty MessageBuilder -> MessageBuilder
sconcat :: NonEmpty MessageBuilder -> MessageBuilder
$cstimes :: forall b. Integral b => b -> MessageBuilder -> MessageBuilder
stimes :: forall b. Integral b => b -> MessageBuilder -> MessageBuilder
Semigroup)

instance Show MessageBuilder where
  showsPrec :: Int -> MessageBuilder -> ShowS
showsPrec Int
prec MessageBuilder
builder =
      Bool -> ShowS -> ShowS
showParen (Int
prec Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10)
        (String -> ShowS
showString String
"Proto3.Wire.Encode.unsafeFromLazyByteString " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ShowS
forall a. Show a => a -> ShowS
shows ByteString
bytes')
    where
      bytes' :: ByteString
bytes' = MessageBuilder -> ByteString
toLazyByteString MessageBuilder
builder

-- | Convert a message builder to a 'RB.BuildR'.
reverseMessageBuilder :: MessageBuilder -> RB.BuildR
reverseMessageBuilder :: MessageBuilder -> BuildR
reverseMessageBuilder = MessageBuilder -> BuildR
unMessageBuilder

-- | Eta-expands a function that produces a 'MessageBuilder', so that
-- its input is not evaluated until the builder state is presented.
--
-- This odd combinator seems to help performance at times, though
-- it may change behavior on nonterminating values of type @a@.
etaMessageBuilder :: forall a . (a -> MessageBuilder) -> a -> MessageBuilder
etaMessageBuilder :: forall a. (a -> MessageBuilder) -> a -> MessageBuilder
etaMessageBuilder = ((a -> BuildR) -> a -> BuildR)
-> (a -> MessageBuilder) -> a -> MessageBuilder
forall a b. Coercible a b => a -> b
coerce (forall a. (a -> BuildR) -> a -> BuildR
RB.etaBuildR @a)

-- | Essentially 'foldMap', but iterates right to left for efficiency.
vectorMessageBuilder ::
  forall v a . Vector v a => (a -> MessageBuilder) -> v a -> MessageBuilder
vectorMessageBuilder :: forall (v :: * -> *) a.
Vector v a =>
(a -> MessageBuilder) -> v a -> MessageBuilder
vectorMessageBuilder = ((a -> BuildR) -> v a -> BuildR)
-> (a -> MessageBuilder) -> v a -> MessageBuilder
forall a b. Coercible a b => a -> b
coerce (forall (v :: * -> *) a.
Vector v a =>
(a -> BuildR) -> v a -> BuildR
RB.vectorBuildR @v @a)

-- | O(n): Retrieve the length of a message, in bytes.
messageLength :: MessageBuilder -> Word
messageLength :: MessageBuilder -> Word
messageLength = Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word) -> (MessageBuilder -> Int) -> MessageBuilder -> Word
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, ByteString) -> Int
forall a b. (a, b) -> a
fst ((Int, ByteString) -> Int)
-> (MessageBuilder -> (Int, ByteString)) -> MessageBuilder -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuildR -> (Int, ByteString)
RB.runBuildR (BuildR -> (Int, ByteString))
-> (MessageBuilder -> BuildR)
-> MessageBuilder
-> (Int, ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MessageBuilder -> BuildR
unMessageBuilder

-- | Convert a message to a lazy `BL.ByteString`
toLazyByteString :: MessageBuilder -> BL.ByteString
toLazyByteString :: MessageBuilder -> ByteString
toLazyByteString = BuildR -> ByteString
RB.toLazyByteString (BuildR -> ByteString)
-> (MessageBuilder -> BuildR) -> MessageBuilder -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MessageBuilder -> BuildR
unMessageBuilder

-- | This lets you cast an arbitrary `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
unsafeFromLazyByteString :: BL.ByteString -> MessageBuilder
unsafeFromLazyByteString :: ByteString -> MessageBuilder
unsafeFromLazyByteString ByteString
bytes' =
    MessageBuilder { unMessageBuilder :: BuildR
unMessageBuilder = ByteString -> BuildR
RB.lazyByteString ByteString
bytes' }

newtype MessageBoundedPrim w
  = MessageBoundedPrim { forall (w :: Nat). MessageBoundedPrim w -> BoundedPrim w
unMessageBoundedPrim :: Prim.BoundedPrim w }

type instance PNullary MessageBoundedPrim width = MessageBoundedPrim width

instance (w1 + w2) ~ w3 =>
         PSemigroup MessageBoundedPrim w1 w2 w3
  where
    pmappend :: PNullary MessageBoundedPrim w1
-> PNullary MessageBoundedPrim w2 -> PNullary MessageBoundedPrim w3
pmappend = (BoundedPrim Any -> BoundedPrim Any -> BoundedPrim (Any + Any))
-> MessageBoundedPrim w1
-> MessageBoundedPrim w2
-> MessageBoundedPrim w3
forall a b. Coercible a b => a -> b
coerce (forall k (n :: k -> *) (t :: k) (u :: k) (v :: k).
PSemigroup n t u v =>
PNullary n t -> PNullary n u -> PNullary n v
pmappend @Nat @Prim.BoundedPrim)
    {-# INLINE CONLIKE pmappend #-}

instance Prim.AssocPlusNat MessageBoundedPrim u v w
  where
    assocLPlusNat :: Proxy# '(u, v, w)
-> PNullary MessageBoundedPrim (u + (v + w))
-> PNullary MessageBoundedPrim ((u + v) + w)
assocLPlusNat = \Proxy# '(u, v, w)
p -> (BoundedPrim (u + (v + w)) -> BoundedPrim ((u + v) + w))
-> MessageBoundedPrim (u + (v + w))
-> MessageBoundedPrim ((u + v) + w)
forall a b. Coercible a b => a -> b
coerce (forall (n :: Nat -> *) (u :: Nat) (v :: Nat) (w :: Nat).
AssocPlusNat n u v w =>
Proxy# '(u, v, w)
-> PNullary n (u + (v + w)) -> PNullary n ((u + v) + w)
Prim.assocLPlusNat @Prim.BoundedPrim Proxy# '(u, v, w)
p)
    {-# INLINE CONLIKE assocLPlusNat #-}

    assocRPlusNat :: Proxy# '(u, v, w)
-> PNullary MessageBoundedPrim ((u + v) + w)
-> PNullary MessageBoundedPrim (u + (v + w))
assocRPlusNat = \Proxy# '(u, v, w)
p -> (BoundedPrim ((u + v) + w) -> BoundedPrim (u + (v + w)))
-> MessageBoundedPrim ((u + v) + w)
-> MessageBoundedPrim (u + (v + w))
forall a b. Coercible a b => a -> b
coerce (forall (n :: Nat -> *) (u :: Nat) (v :: Nat) (w :: Nat).
AssocPlusNat n u v w =>
Proxy# '(u, v, w)
-> PNullary n ((u + v) + w) -> PNullary n (u + (v + w))
Prim.assocRPlusNat @Prim.BoundedPrim Proxy# '(u, v, w)
p)
    {-# INLINE CONLIKE assocRPlusNat #-}

instance Prim.CommPlusNat MessageBoundedPrim u v
  where
    commPlusNat :: Proxy# '(u, v)
-> PNullary MessageBoundedPrim (u + v)
-> PNullary MessageBoundedPrim (v + u)
commPlusNat = \Proxy# '(u, v)
p -> (BoundedPrim (u + v) -> BoundedPrim (v + u))
-> MessageBoundedPrim (u + v) -> MessageBoundedPrim (v + u)
forall a b. Coercible a b => a -> b
coerce (forall (n :: Nat -> *) (u :: Nat) (v :: Nat).
CommPlusNat n u v =>
Proxy# '(u, v) -> PNullary n (u + v) -> PNullary n (v + u)
Prim.commPlusNat @Prim.BoundedPrim Proxy# '(u, v)
p)
    {-# INLINE CONLIKE commPlusNat #-}

instance PMEmpty MessageBoundedPrim 0
  where
    pmempty :: PNullary MessageBoundedPrim 0
pmempty = BoundedPrim 0 -> MessageBoundedPrim 0
forall a b. Coercible a b => a -> b
coerce (forall k (n :: k -> *) (id :: k). PMEmpty n id => PNullary n id
pmempty @Nat @Prim.BoundedPrim)
    {-# INLINE CONLIKE pmempty #-}

instance Prim.Max u v ~ w =>
         Prim.PChoose MessageBoundedPrim u v w
  where
    pbool :: PNullary MessageBoundedPrim u
-> PNullary MessageBoundedPrim v
-> Bool
-> PNullary MessageBoundedPrim w
pbool = (BoundedPrim Any
 -> BoundedPrim Any
 -> Bool
 -> BoundedPrim
      (If (OrdCond (CmpNat Any Any) 'True 'True 'False) Any Any))
-> MessageBoundedPrim u
-> MessageBoundedPrim v
-> Bool
-> MessageBoundedPrim w
forall a b. Coercible a b => a -> b
coerce (forall {k} (n :: k -> *) (f :: k) (t :: k) (w :: k).
PChoose n f t w =>
PNullary n f -> PNullary n t -> Bool -> PNullary n w
forall (n :: Nat -> *) (f :: Nat) (t :: Nat) (w :: Nat).
PChoose n f t w =>
PNullary n f -> PNullary n t -> Bool -> PNullary n w
Prim.pbool @Prim.BoundedPrim)
    {-# INLINE CONLIKE pbool #-}

instance Prim.AssocMaxNat MessageBoundedPrim u v w
  where
    assocLMaxNat :: Proxy# '(u, v, w)
-> PNullary MessageBoundedPrim (Max u (Max v w))
-> PNullary MessageBoundedPrim (Max (Max u v) w)
assocLMaxNat = \Proxy# '(u, v, w)
p -> (BoundedPrim
   (If
      (OrdCond
         (CmpNat (If (OrdCond (CmpNat w v) 'True 'True 'False) v w) u)
         'True
         'True
         'False)
      u
      (If (OrdCond (CmpNat w v) 'True 'True 'False) v w))
 -> BoundedPrim
      (If
         (OrdCond
            (CmpNat w (If (OrdCond (CmpNat v u) 'True 'True 'False) u v))
            'True
            'True
            'False)
         (If (OrdCond (CmpNat v u) 'True 'True 'False) u v)
         w))
-> MessageBoundedPrim
     (If
        (OrdCond
           (CmpNat (If (OrdCond (CmpNat w v) 'True 'True 'False) v w) u)
           'True
           'True
           'False)
        u
        (If (OrdCond (CmpNat w v) 'True 'True 'False) v w))
-> MessageBoundedPrim
     (If
        (OrdCond
           (CmpNat w (If (OrdCond (CmpNat v u) 'True 'True 'False) u v))
           'True
           'True
           'False)
        (If (OrdCond (CmpNat v u) 'True 'True 'False) u v)
        w)
forall a b. Coercible a b => a -> b
coerce (forall {k} (n :: k -> *) (u :: k) (v :: k) (w :: k).
AssocMaxNat n u v w =>
Proxy# '(u, v, w)
-> PNullary n (Max u (Max v w)) -> PNullary n (Max (Max u v) w)
forall (n :: Nat -> *) (u :: Nat) (v :: Nat) (w :: Nat).
AssocMaxNat n u v w =>
Proxy# '(u, v, w)
-> PNullary n (Max u (Max v w)) -> PNullary n (Max (Max u v) w)
Prim.assocLMaxNat @Prim.BoundedPrim Proxy# '(u, v, w)
p)
    {-# INLINE CONLIKE assocLMaxNat #-}

    assocRMaxNat :: Proxy# '(u, v, w)
-> PNullary MessageBoundedPrim (Max (Max u v) w)
-> PNullary MessageBoundedPrim (Max u (Max v w))
assocRMaxNat = \Proxy# '(u, v, w)
p -> (BoundedPrim
   (If
      (OrdCond
         (CmpNat w (If (OrdCond (CmpNat v u) 'True 'True 'False) u v))
         'True
         'True
         'False)
      (If (OrdCond (CmpNat v u) 'True 'True 'False) u v)
      w)
 -> BoundedPrim
      (If
         (OrdCond
            (CmpNat (If (OrdCond (CmpNat w v) 'True 'True 'False) v w) u)
            'True
            'True
            'False)
         u
         (If (OrdCond (CmpNat w v) 'True 'True 'False) v w)))
-> MessageBoundedPrim
     (If
        (OrdCond
           (CmpNat w (If (OrdCond (CmpNat v u) 'True 'True 'False) u v))
           'True
           'True
           'False)
        (If (OrdCond (CmpNat v u) 'True 'True 'False) u v)
        w)
-> MessageBoundedPrim
     (If
        (OrdCond
           (CmpNat (If (OrdCond (CmpNat w v) 'True 'True 'False) v w) u)
           'True
           'True
           'False)
        u
        (If (OrdCond (CmpNat w v) 'True 'True 'False) v w))
forall a b. Coercible a b => a -> b
coerce (forall {k} (n :: k -> *) (u :: k) (v :: k) (w :: k).
AssocMaxNat n u v w =>
Proxy# '(u, v, w)
-> PNullary n (Max (Max u v) w) -> PNullary n (Max u (Max v w))
forall (n :: Nat -> *) (u :: Nat) (v :: Nat) (w :: Nat).
AssocMaxNat n u v w =>
Proxy# '(u, v, w)
-> PNullary n (Max (Max u v) w) -> PNullary n (Max u (Max v w))
Prim.assocRMaxNat @Prim.BoundedPrim Proxy# '(u, v, w)
p)
    {-# INLINE CONLIKE assocRMaxNat #-}

instance Prim.CommMaxNat MessageBoundedPrim u v
  where
    commMaxNat :: Proxy# '(u, v)
-> PNullary MessageBoundedPrim (Max u v)
-> PNullary MessageBoundedPrim (Max v u)
commMaxNat = \Proxy# '(u, v)
p -> (BoundedPrim (If (OrdCond (CmpNat v u) 'True 'True 'False) u v)
 -> BoundedPrim (If (OrdCond (CmpNat u v) 'True 'True 'False) v u))
-> MessageBoundedPrim
     (If (OrdCond (CmpNat v u) 'True 'True 'False) u v)
-> MessageBoundedPrim
     (If (OrdCond (CmpNat u v) 'True 'True 'False) v u)
forall a b. Coercible a b => a -> b
coerce (forall {k} (n :: k -> *) (u :: k) (v :: k).
CommMaxNat n u v =>
Proxy# '(u, v) -> PNullary n (Max u v) -> PNullary n (Max v u)
forall (n :: Nat -> *) (u :: Nat) (v :: Nat).
CommMaxNat n u v =>
Proxy# '(u, v) -> PNullary n (Max u v) -> PNullary n (Max v u)
Prim.commMaxNat @Prim.BoundedPrim Proxy# '(u, v)
p)
    {-# INLINE CONLIKE commMaxNat #-}

liftBoundedPrim :: KnownNat w => MessageBoundedPrim w -> MessageBuilder
liftBoundedPrim :: forall (w :: Nat).
KnownNat w =>
MessageBoundedPrim w -> MessageBuilder
liftBoundedPrim (MessageBoundedPrim BoundedPrim w
p) = BuildR -> MessageBuilder
MessageBuilder (BoundedPrim w -> BuildR
forall (w :: Nat). KnownNat w => BoundedPrim w -> BuildR
Prim.liftBoundedPrim BoundedPrim w
p)
{-# INLINE liftBoundedPrim #-}

base128Varint32 :: Word32 -> MessageBoundedPrim 5
base128Varint32 :: Word32 -> MessageBoundedPrim 5
base128Varint32 = BoundedPrim 5 -> MessageBoundedPrim 5
forall (w :: Nat). BoundedPrim w -> MessageBoundedPrim w
MessageBoundedPrim (BoundedPrim 5 -> MessageBoundedPrim 5)
-> (Word32 -> BoundedPrim 5) -> Word32 -> MessageBoundedPrim 5
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> BoundedPrim 5
Prim.word32Base128LEVar
{-# INLINE base128Varint32 #-}

base128Varint64 :: Word64 -> MessageBoundedPrim 10
base128Varint64 :: Word64 -> MessageBoundedPrim 10
base128Varint64 = BoundedPrim 10 -> MessageBoundedPrim 10
forall (w :: Nat). BoundedPrim w -> MessageBoundedPrim w
MessageBoundedPrim (BoundedPrim 10 -> MessageBoundedPrim 10)
-> (Word64 -> BoundedPrim 10) -> Word64 -> MessageBoundedPrim 10
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> BoundedPrim 10
Prim.word64Base128LEVar
{-# INLINE base128Varint64 #-}

base128Varint64_inline :: Word64 -> MessageBoundedPrim 10
base128Varint64_inline :: Word64 -> MessageBoundedPrim 10
base128Varint64_inline = BoundedPrim 10 -> MessageBoundedPrim 10
forall (w :: Nat). BoundedPrim w -> MessageBoundedPrim w
MessageBoundedPrim (BoundedPrim 10 -> MessageBoundedPrim 10)
-> (Word64 -> BoundedPrim 10) -> Word64 -> MessageBoundedPrim 10
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> BoundedPrim 10
Prim.word64Base128LEVar_inline
{-# INLINE base128Varint64_inline #-}

wireType :: WireType -> Word8
wireType :: WireType -> Word8
wireType WireType
Varint = Word8
0
wireType WireType
Fixed32 = Word8
5
wireType WireType
Fixed64 = Word8
1
wireType WireType
LengthDelimited = Word8
2

fieldHeader :: FieldNumber -> WireType -> MessageBoundedPrim 10
fieldHeader :: FieldNumber -> WireType -> MessageBoundedPrim 10
fieldHeader = \FieldNumber
num WireType
wt -> Word64 -> MessageBoundedPrim 10
base128Varint64_inline
    ((FieldNumber -> Word64
getFieldNumber FieldNumber
num Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftL` Int
3) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (WireType -> Word8
wireType WireType
wt))
{-# INLINE fieldHeader #-}

-- | Encode a 32-bit "standard" integer
--
-- For example:
--
-- >>> 1 `int32` 42
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\b*"
-- >>> 1 `int64` (-42)
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\b\214\255\255\255\255\255\255\255\255\SOH"
--
-- NOTE: Protobuf encoding converts an @int32@ to a 64-bit unsigned value
-- before encoding it, not a 32-bit value (which would be more efficient).
--
-- To quote the specification: "If you use int32 or int64 as the type for
-- a negative number, the resulting varint is always ten bytes long..."
-- <https://developers.google.com/protocol-buffers/docs/encoding#varints>
int32 :: FieldNumber -> Int32 -> MessageBuilder
int32 :: FieldNumber -> Int32 -> MessageBuilder
int32 = \FieldNumber
num Int32
i -> MessageBoundedPrim 20 -> MessageBuilder
forall (w :: Nat).
KnownNat w =>
MessageBoundedPrim w -> MessageBuilder
liftBoundedPrim (MessageBoundedPrim 20 -> MessageBuilder)
-> MessageBoundedPrim 20 -> MessageBuilder
forall a b. (a -> b) -> a -> b
$
    FieldNumber -> WireType -> MessageBoundedPrim 10
fieldHeader FieldNumber
num WireType
Varint PNullary MessageBoundedPrim 10
-> PNullary MessageBoundedPrim 10 -> PNullary MessageBoundedPrim 20
forall k (n :: k -> *) (t :: k) (u :: k) (v :: k).
PSemigroup n t u v =>
PNullary n t -> PNullary n u -> PNullary n v
&<> Word64 -> MessageBoundedPrim 10
base128Varint64 (Int32 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
i)
{-# INLINE int32 #-}

-- | Encode a 64-bit "standard" integer
--
-- For example:
--
-- >>> 1 `int32` 42
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\b*"
-- >>> 1 `int64` (-42)
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\b\214\255\255\255\255\255\255\255\255\SOH"
int64 :: FieldNumber -> Int64 -> MessageBuilder
int64 :: FieldNumber -> Int64 -> MessageBuilder
int64 = \FieldNumber
num Int64
i -> MessageBoundedPrim 20 -> MessageBuilder
forall (w :: Nat).
KnownNat w =>
MessageBoundedPrim w -> MessageBuilder
liftBoundedPrim (MessageBoundedPrim 20 -> MessageBuilder)
-> MessageBoundedPrim 20 -> MessageBuilder
forall a b. (a -> b) -> a -> b
$
    FieldNumber -> WireType -> MessageBoundedPrim 10
fieldHeader FieldNumber
num WireType
Varint PNullary MessageBoundedPrim 10
-> PNullary MessageBoundedPrim 10 -> PNullary MessageBoundedPrim 20
forall k (n :: k -> *) (t :: k) (u :: k) (v :: k).
PSemigroup n t u v =>
PNullary n t -> PNullary n u -> PNullary n v
&<> Word64 -> MessageBoundedPrim 10
base128Varint64 (Int64 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
i)
{-# INLINE int64 #-}

-- | Encode a 32-bit unsigned integer
--
-- For example:
--
-- >>> 1 `uint32` 42
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\b*"
uint32 :: FieldNumber -> Word32 -> MessageBuilder
uint32 :: FieldNumber -> Word32 -> MessageBuilder
uint32 = \FieldNumber
num Word32
i -> MessageBoundedPrim 15 -> MessageBuilder
forall (w :: Nat).
KnownNat w =>
MessageBoundedPrim w -> MessageBuilder
liftBoundedPrim (MessageBoundedPrim 15 -> MessageBuilder)
-> MessageBoundedPrim 15 -> MessageBuilder
forall a b. (a -> b) -> a -> b
$
    FieldNumber -> WireType -> MessageBoundedPrim 10
fieldHeader FieldNumber
num WireType
Varint PNullary MessageBoundedPrim 10
-> PNullary MessageBoundedPrim 5 -> PNullary MessageBoundedPrim 15
forall k (n :: k -> *) (t :: k) (u :: k) (v :: k).
PSemigroup n t u v =>
PNullary n t -> PNullary n u -> PNullary n v
&<> Word32 -> MessageBoundedPrim 5
base128Varint32 Word32
i
{-# INLINE uint32 #-}

-- | Encode a 64-bit unsigned integer
--
-- For example:
--
-- >>> 1 `uint64` 42
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\b*"
uint64 :: FieldNumber -> Word64 -> MessageBuilder
uint64 :: FieldNumber -> Word64 -> MessageBuilder
uint64 = \FieldNumber
num Word64
i -> MessageBoundedPrim 20 -> MessageBuilder
forall (w :: Nat).
KnownNat w =>
MessageBoundedPrim w -> MessageBuilder
liftBoundedPrim (MessageBoundedPrim 20 -> MessageBuilder)
-> MessageBoundedPrim 20 -> MessageBuilder
forall a b. (a -> b) -> a -> b
$
    FieldNumber -> WireType -> MessageBoundedPrim 10
fieldHeader FieldNumber
num WireType
Varint PNullary MessageBoundedPrim 10
-> PNullary MessageBoundedPrim 10 -> PNullary MessageBoundedPrim 20
forall k (n :: k -> *) (t :: k) (u :: k) (v :: k).
PSemigroup n t u v =>
PNullary n t -> PNullary n u -> PNullary n v
&<> Word64 -> MessageBoundedPrim 10
base128Varint64 Word64
i
{-# INLINE uint64 #-}

-- | Encode a 32-bit signed integer
--
-- For example:
--
-- >>> 1 `sint32` (-42)
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\bS"
-- >>> 1 `sint32` maxBound
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\b\254\255\255\255\SI"
-- >>> 1 `sint32` minBound
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\b\255\255\255\255\SI"
sint32 :: FieldNumber -> Int32 -> MessageBuilder
sint32 :: FieldNumber -> Int32 -> MessageBuilder
sint32 = \FieldNumber
num Int32
i ->
  FieldNumber -> Word32 -> MessageBuilder
uint32 FieldNumber
num (Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Int32
forall a. (Num a, FiniteBits a) => a -> a
zigZagEncode Int32
i))
{-# INLINE sint32 #-}

-- | Encode a 64-bit signed integer
--
-- For example:
--
-- >>> 1 `sint64` (-42)
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\bS"
-- >>> 1 `sint64` maxBound
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\b\254\255\255\255\255\255\255\255\255\SOH"
-- >>> 1 `sint64` minBound
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\b\255\255\255\255\255\255\255\255\255\SOH"
sint64 :: FieldNumber -> Int64 -> MessageBuilder
sint64 :: FieldNumber -> Int64 -> MessageBuilder
sint64 = \FieldNumber
num Int64
i ->
  FieldNumber -> Word64 -> MessageBuilder
uint64 FieldNumber
num (Int64 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64 -> Int64
forall a. (Num a, FiniteBits a) => a -> a
zigZagEncode Int64
i))
{-# INLINE sint64 #-}

-- | Encode a fixed-width 32-bit integer
--
-- For example:
--
-- >>> 1 `fixed32` 42
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\r*\NUL\NUL\NUL"
fixed32 :: FieldNumber -> Word32 -> MessageBuilder
fixed32 :: FieldNumber -> Word32 -> MessageBuilder
fixed32 = \FieldNumber
num Word32
i -> MessageBoundedPrim 14 -> MessageBuilder
forall (w :: Nat).
KnownNat w =>
MessageBoundedPrim w -> MessageBuilder
liftBoundedPrim (MessageBoundedPrim 14 -> MessageBuilder)
-> MessageBoundedPrim 14 -> MessageBuilder
forall a b. (a -> b) -> a -> b
$
    FieldNumber -> WireType -> MessageBoundedPrim 10
fieldHeader FieldNumber
num WireType
Fixed32 PNullary MessageBoundedPrim 10
-> PNullary MessageBoundedPrim 4 -> PNullary MessageBoundedPrim 14
forall k (n :: k -> *) (t :: k) (u :: k) (v :: k).
PSemigroup n t u v =>
PNullary n t -> PNullary n u -> PNullary n v
&<>
    BoundedPrim 4 -> MessageBoundedPrim 4
forall (w :: Nat). BoundedPrim w -> MessageBoundedPrim w
MessageBoundedPrim (FixedPrim 4 -> BoundedPrim 4
forall (w :: Nat). KnownNat w => FixedPrim w -> BoundedPrim w
Prim.liftFixedPrim (Word32 -> FixedPrim 4
Prim.word32LE Word32
i))
{-# INLINE fixed32 #-}

-- | Encode a fixed-width 64-bit integer
--
-- For example:
--
-- >>> 1 `fixed64` 42
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\t*\NUL\NUL\NUL\NUL\NUL\NUL\NUL"
fixed64 :: FieldNumber -> Word64 -> MessageBuilder
fixed64 :: FieldNumber -> Word64 -> MessageBuilder
fixed64 = \FieldNumber
num Word64
i -> MessageBoundedPrim 18 -> MessageBuilder
forall (w :: Nat).
KnownNat w =>
MessageBoundedPrim w -> MessageBuilder
liftBoundedPrim (MessageBoundedPrim 18 -> MessageBuilder)
-> MessageBoundedPrim 18 -> MessageBuilder
forall a b. (a -> b) -> a -> b
$
    FieldNumber -> WireType -> MessageBoundedPrim 10
fieldHeader FieldNumber
num WireType
Fixed64 PNullary MessageBoundedPrim 10
-> PNullary MessageBoundedPrim 8 -> PNullary MessageBoundedPrim 18
forall k (n :: k -> *) (t :: k) (u :: k) (v :: k).
PSemigroup n t u v =>
PNullary n t -> PNullary n u -> PNullary n v
&<>
    BoundedPrim 8 -> MessageBoundedPrim 8
forall (w :: Nat). BoundedPrim w -> MessageBoundedPrim w
MessageBoundedPrim (FixedPrim 8 -> BoundedPrim 8
forall (w :: Nat). KnownNat w => FixedPrim w -> BoundedPrim w
Prim.liftFixedPrim (Word64 -> FixedPrim 8
Prim.word64LE Word64
i))
{-# INLINE fixed64 #-}

-- | Encode a fixed-width signed 32-bit integer
--
-- For example:
--
-- > 1 `sfixed32` (-42)
sfixed32 :: FieldNumber -> Int32 -> MessageBuilder
sfixed32 :: FieldNumber -> Int32 -> MessageBuilder
sfixed32 = \FieldNumber
num Int32
i -> MessageBoundedPrim 14 -> MessageBuilder
forall (w :: Nat).
KnownNat w =>
MessageBoundedPrim w -> MessageBuilder
liftBoundedPrim (MessageBoundedPrim 14 -> MessageBuilder)
-> MessageBoundedPrim 14 -> MessageBuilder
forall a b. (a -> b) -> a -> b
$
    FieldNumber -> WireType -> MessageBoundedPrim 10
fieldHeader FieldNumber
num WireType
Fixed32 PNullary MessageBoundedPrim 10
-> PNullary MessageBoundedPrim 4 -> PNullary MessageBoundedPrim 14
forall k (n :: k -> *) (t :: k) (u :: k) (v :: k).
PSemigroup n t u v =>
PNullary n t -> PNullary n u -> PNullary n v
&<>
    BoundedPrim 4 -> MessageBoundedPrim 4
forall (w :: Nat). BoundedPrim w -> MessageBoundedPrim w
MessageBoundedPrim (FixedPrim 4 -> BoundedPrim 4
forall (w :: Nat). KnownNat w => FixedPrim w -> BoundedPrim w
Prim.liftFixedPrim (Int32 -> FixedPrim 4
Prim.int32LE Int32
i))
{-# INLINE sfixed32 #-}

-- | Encode a fixed-width signed 64-bit integer
--
-- For example:
--
-- >>> 1 `sfixed64` (-42)
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\t\214\255\255\255\255\255\255\255"
sfixed64 :: FieldNumber -> Int64 -> MessageBuilder
sfixed64 :: FieldNumber -> Int64 -> MessageBuilder
sfixed64 = \FieldNumber
num Int64
i -> MessageBoundedPrim 18 -> MessageBuilder
forall (w :: Nat).
KnownNat w =>
MessageBoundedPrim w -> MessageBuilder
liftBoundedPrim (MessageBoundedPrim 18 -> MessageBuilder)
-> MessageBoundedPrim 18 -> MessageBuilder
forall a b. (a -> b) -> a -> b
$
    FieldNumber -> WireType -> MessageBoundedPrim 10
fieldHeader FieldNumber
num WireType
Fixed64 PNullary MessageBoundedPrim 10
-> PNullary MessageBoundedPrim 8 -> PNullary MessageBoundedPrim 18
forall k (n :: k -> *) (t :: k) (u :: k) (v :: k).
PSemigroup n t u v =>
PNullary n t -> PNullary n u -> PNullary n v
&<>
    BoundedPrim 8 -> MessageBoundedPrim 8
forall (w :: Nat). BoundedPrim w -> MessageBoundedPrim w
MessageBoundedPrim (FixedPrim 8 -> BoundedPrim 8
forall (w :: Nat). KnownNat w => FixedPrim w -> BoundedPrim w
Prim.liftFixedPrim (Int64 -> FixedPrim 8
Prim.int64LE Int64
i))
{-# INLINE sfixed64 #-}

-- | Encode a floating point number
--
-- For example:
--
-- >>> 1 `float` 3.14
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\r\195\245H@"
float :: FieldNumber -> Float -> MessageBuilder
float :: FieldNumber -> Float -> MessageBuilder
float = \FieldNumber
num Float
f -> MessageBoundedPrim 14 -> MessageBuilder
forall (w :: Nat).
KnownNat w =>
MessageBoundedPrim w -> MessageBuilder
liftBoundedPrim (MessageBoundedPrim 14 -> MessageBuilder)
-> MessageBoundedPrim 14 -> MessageBuilder
forall a b. (a -> b) -> a -> b
$
    FieldNumber -> WireType -> MessageBoundedPrim 10
fieldHeader FieldNumber
num WireType
Fixed32 PNullary MessageBoundedPrim 10
-> PNullary MessageBoundedPrim 4 -> PNullary MessageBoundedPrim 14
forall k (n :: k -> *) (t :: k) (u :: k) (v :: k).
PSemigroup n t u v =>
PNullary n t -> PNullary n u -> PNullary n v
&<>
    BoundedPrim 4 -> MessageBoundedPrim 4
forall (w :: Nat). BoundedPrim w -> MessageBoundedPrim w
MessageBoundedPrim (FixedPrim 4 -> BoundedPrim 4
forall (w :: Nat). KnownNat w => FixedPrim w -> BoundedPrim w
Prim.liftFixedPrim (Float -> FixedPrim 4
Prim.floatLE Float
f))
{-# INLINE float #-}

-- | Encode a double-precision number
--
-- For example:
--
-- >>> 1 `double` 3.14
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\t\US\133\235Q\184\RS\t@"
double :: FieldNumber -> Double -> MessageBuilder
double :: FieldNumber -> Double -> MessageBuilder
double = \FieldNumber
num Double
d -> MessageBoundedPrim 18 -> MessageBuilder
forall (w :: Nat).
KnownNat w =>
MessageBoundedPrim w -> MessageBuilder
liftBoundedPrim (MessageBoundedPrim 18 -> MessageBuilder)
-> MessageBoundedPrim 18 -> MessageBuilder
forall a b. (a -> b) -> a -> b
$
    FieldNumber -> WireType -> MessageBoundedPrim 10
fieldHeader FieldNumber
num WireType
Fixed64 PNullary MessageBoundedPrim 10
-> PNullary MessageBoundedPrim 8 -> PNullary MessageBoundedPrim 18
forall k (n :: k -> *) (t :: k) (u :: k) (v :: k).
PSemigroup n t u v =>
PNullary n t -> PNullary n u -> PNullary n v
&<>
    BoundedPrim 8 -> MessageBoundedPrim 8
forall (w :: Nat). BoundedPrim w -> MessageBoundedPrim w
MessageBoundedPrim (FixedPrim 8 -> BoundedPrim 8
forall (w :: Nat). KnownNat w => FixedPrim w -> BoundedPrim w
Prim.liftFixedPrim (Double -> FixedPrim 8
Prim.doubleLE Double
d))
{-# INLINE double #-}

-- | Encode a value with an enumerable type.
--
-- You should instantiate 'ProtoEnum' for a type in
-- order to emulate enums appearing in .proto files.
--
-- For example:
--
-- >>> :{
--     data Shape = Circle | Square | Triangle deriving (Bounded, Enum)
--     instance ProtoEnum Shape
--     data Gap = Gap0 | Gap3
--     instance ProtoEnum Gap where
--       toProtoEnumMay i = case i of
--         0 -> Just Gap0
--         3 -> Just Gap3
--         _ -> Nothing
--       fromProtoEnum g = case g of
--         Gap0 -> 0
--         Gap3 -> 3
-- :}
--
-- >>> 1 `enum` Triangle <> 2 `enum` Gap3
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\b\STX\DLE\ETX"
enum :: ProtoEnum e => FieldNumber -> e -> MessageBuilder
enum :: forall e. ProtoEnum e => FieldNumber -> e -> MessageBuilder
enum = \FieldNumber
num e
e -> MessageBoundedPrim 15 -> MessageBuilder
forall (w :: Nat).
KnownNat w =>
MessageBoundedPrim w -> MessageBuilder
liftBoundedPrim (MessageBoundedPrim 15 -> MessageBuilder)
-> MessageBoundedPrim 15 -> MessageBuilder
forall a b. (a -> b) -> a -> b
$
    FieldNumber -> WireType -> MessageBoundedPrim 10
fieldHeader FieldNumber
num WireType
Varint PNullary MessageBoundedPrim 10
-> PNullary MessageBoundedPrim 5 -> PNullary MessageBoundedPrim 15
forall k (n :: k -> *) (t :: k) (u :: k) (v :: k).
PSemigroup n t u v =>
PNullary n t -> PNullary n u -> PNullary n v
&<>
    Word32 -> MessageBoundedPrim 5
base128Varint32 (forall a b. (Integral a, Num b) => a -> b
fromIntegral @Int32 @Word32 (e -> Int32
forall a. ProtoEnum a => a -> Int32
fromProtoEnum e
e))
{-# INLINE enum #-}

-- | Encode a boolean value
--
-- For example:
--
-- >>> 1 `bool` True
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\b\SOH"
bool :: FieldNumber -> Bool -> MessageBuilder
bool :: FieldNumber -> Bool -> MessageBuilder
bool = \FieldNumber
num Bool
b -> MessageBoundedPrim 11 -> MessageBuilder
forall (w :: Nat).
KnownNat w =>
MessageBoundedPrim w -> MessageBuilder
liftBoundedPrim (MessageBoundedPrim 11 -> MessageBuilder)
-> MessageBoundedPrim 11 -> MessageBuilder
forall a b. (a -> b) -> a -> b
$
    FieldNumber -> WireType -> MessageBoundedPrim 10
fieldHeader FieldNumber
num WireType
Varint PNullary MessageBoundedPrim 10
-> PNullary MessageBoundedPrim 1 -> PNullary MessageBoundedPrim 11
forall k (n :: k -> *) (t :: k) (u :: k) (v :: k).
PSemigroup n t u v =>
PNullary n t -> PNullary n u -> PNullary n v
&<>
    BoundedPrim 1 -> MessageBoundedPrim 1
forall (w :: Nat). BoundedPrim w -> MessageBoundedPrim w
MessageBoundedPrim
      (FixedPrim 1 -> BoundedPrim 1
forall (w :: Nat). KnownNat w => FixedPrim w -> BoundedPrim w
Prim.liftFixedPrim (Word8 -> FixedPrim 1
Prim.word8 (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Bool -> Int
forall a. Enum a => a -> Int
fromEnum Bool
b))))
      -- Using word8 instead of a varint encoder shrinks the width bound.
{-# INLINE bool #-}

-- | Encode a sequence of octets as a field of type 'bytes'.
--
-- >>> 1 `bytes` (Proto3.Wire.Reverse.stringUtf8 "testing")
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\n\atesting"
bytes :: FieldNumber -> RB.BuildR -> MessageBuilder
bytes :: FieldNumber -> BuildR -> MessageBuilder
bytes FieldNumber
num = FieldNumber -> MessageBuilder -> MessageBuilder
embedded FieldNumber
num (MessageBuilder -> MessageBuilder)
-> (BuildR -> MessageBuilder) -> BuildR -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuildR -> MessageBuilder
MessageBuilder
{-# INLINE bytes #-}

-- | Encode a UTF-8 string.
--
-- For example:
--
-- >>> 1 `string` "testing"
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\n\atesting"
string :: FieldNumber -> String -> MessageBuilder
string :: FieldNumber -> String -> MessageBuilder
string FieldNumber
num = FieldNumber -> MessageBuilder -> MessageBuilder
embedded FieldNumber
num (MessageBuilder -> MessageBuilder)
-> (String -> MessageBuilder) -> String -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuildR -> MessageBuilder
MessageBuilder (BuildR -> MessageBuilder)
-> (String -> BuildR) -> String -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> BuildR
RB.stringUtf8
{-# INLINE string #-}

-- | Encode lazy `Text` as UTF-8
--
-- For example:
--
-- >>> 1 `text` "testing"
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\n\atesting"
text :: FieldNumber -> Text.Lazy.Text -> MessageBuilder
text :: FieldNumber -> Text -> MessageBuilder
text FieldNumber
num = FieldNumber -> MessageBuilder -> MessageBuilder
embedded FieldNumber
num (MessageBuilder -> MessageBuilder)
-> (Text -> MessageBuilder) -> Text -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuildR -> MessageBuilder
MessageBuilder (BuildR -> MessageBuilder)
-> (Text -> BuildR) -> Text -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> BuildR
RB.lazyTextUtf8
{-# INLINE text #-}

-- | Encode a `Text.Short.ShortText` as UTF-8.
--
-- For example:
--
-- >>> 1 `shortText` "testing"
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\n\atesting"
shortText :: FieldNumber -> Text.Short.ShortText -> MessageBuilder
shortText :: FieldNumber -> ShortText -> MessageBuilder
shortText FieldNumber
num = FieldNumber -> MessageBuilder -> MessageBuilder
embedded FieldNumber
num (MessageBuilder -> MessageBuilder)
-> (ShortText -> MessageBuilder) -> ShortText -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuildR -> MessageBuilder
MessageBuilder (BuildR -> MessageBuilder)
-> (ShortText -> BuildR) -> ShortText -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortText -> BuildR
RB.shortTextUtf8
{-# INLINE shortText #-}

-- | Encode a collection of bytes in the form of a strict 'B.ByteString'.
--
-- For example:
--
-- >>> 1 `byteString` "testing"
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\n\atesting"
byteString :: FieldNumber -> B.ByteString -> MessageBuilder
byteString :: FieldNumber -> ByteString -> MessageBuilder
byteString FieldNumber
num = FieldNumber -> MessageBuilder -> MessageBuilder
embedded FieldNumber
num (MessageBuilder -> MessageBuilder)
-> (ByteString -> MessageBuilder) -> ByteString -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuildR -> MessageBuilder
MessageBuilder (BuildR -> MessageBuilder)
-> (ByteString -> BuildR) -> ByteString -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> BuildR
RB.byteString
{-# INLINE byteString #-}

-- | Encode a lazy bytestring.
--
-- For example:
--
-- >>> 1 `lazyByteString` "testing"
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\n\atesting"
lazyByteString :: FieldNumber -> BL.ByteString -> MessageBuilder
lazyByteString :: FieldNumber -> ByteString -> MessageBuilder
lazyByteString FieldNumber
num = FieldNumber -> MessageBuilder -> MessageBuilder
embedded FieldNumber
num (MessageBuilder -> MessageBuilder)
-> (ByteString -> MessageBuilder) -> ByteString -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuildR -> MessageBuilder
MessageBuilder (BuildR -> MessageBuilder)
-> (ByteString -> BuildR) -> ByteString -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> BuildR
RB.lazyByteString
{-# INLINE lazyByteString #-}

-- | Encode a `BS.ShortByteString`.
--
-- For example:
--
-- >>> 1 `shortByteString` "testing"
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\n\atesting"
shortByteString :: FieldNumber -> BS.ShortByteString -> MessageBuilder
shortByteString :: FieldNumber -> ShortByteString -> MessageBuilder
shortByteString FieldNumber
num = FieldNumber -> MessageBuilder -> MessageBuilder
embedded FieldNumber
num (MessageBuilder -> MessageBuilder)
-> (ShortByteString -> MessageBuilder)
-> ShortByteString
-> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuildR -> MessageBuilder
MessageBuilder (BuildR -> MessageBuilder)
-> (ShortByteString -> BuildR) -> ShortByteString -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> BuildR
RB.shortByteString
{-# INLINE shortByteString #-}

-- | Encode varints in the space-efficient packed format.
-- But consider 'packedVarintsV', which may be faster.
--
-- The values to be encoded are specified by mapping the elements of a vector.
--
-- >>> packedVarints 1 [1, 2, 3]
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\n\ETX\SOH\STX\ETX"
packedVarints :: Foldable f => FieldNumber -> f Word64 -> MessageBuilder
packedVarints :: forall (f :: * -> *).
Foldable f =>
FieldNumber -> f Word64 -> MessageBuilder
packedVarints FieldNumber
num = (f Word64 -> MessageBuilder) -> f Word64 -> MessageBuilder
forall a. (a -> MessageBuilder) -> a -> MessageBuilder
etaMessageBuilder (FieldNumber -> MessageBuilder -> MessageBuilder
embedded FieldNumber
num (MessageBuilder -> MessageBuilder)
-> (f Word64 -> MessageBuilder) -> f Word64 -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f Word64 -> MessageBuilder
payload)
  where
    payload :: f Word64 -> MessageBuilder
payload = (Word64 -> MessageBuilder) -> f Word64 -> MessageBuilder
forall m a. Monoid m => (a -> m) -> f a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (MessageBoundedPrim 10 -> MessageBuilder
forall (w :: Nat).
KnownNat w =>
MessageBoundedPrim w -> MessageBuilder
liftBoundedPrim (MessageBoundedPrim 10 -> MessageBuilder)
-> (Word64 -> MessageBoundedPrim 10) -> Word64 -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> MessageBoundedPrim 10
base128Varint64)
{-# INLINE packedVarints #-}

-- | A faster but more specialized variant of:
--
-- > \f num -> packedVarints num . fmap f
--
-- >>> packedVarintsV (subtract 10) 1 ([11, 12, 13] :: Data.Vector.Vector Word64)
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\n\ETX\SOH\STX\ETX"
packedVarintsV ::
  Vector v a => (a -> Word64) -> FieldNumber -> v a -> MessageBuilder
packedVarintsV :: forall (v :: * -> *) a.
Vector v a =>
(a -> Word64) -> FieldNumber -> v a -> MessageBuilder
packedVarintsV a -> Word64
f FieldNumber
num = FieldNumber -> MessageBuilder -> MessageBuilder
embedded FieldNumber
num (MessageBuilder -> MessageBuilder)
-> (v a -> MessageBuilder) -> v a -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> MessageBuilder
payload
  where
    payload :: v a -> MessageBuilder
payload = (a -> MessageBuilder) -> v a -> MessageBuilder
forall (v :: * -> *) a.
Vector v a =>
(a -> MessageBuilder) -> v a -> MessageBuilder
vectorMessageBuilder (MessageBoundedPrim 10 -> MessageBuilder
forall (w :: Nat).
KnownNat w =>
MessageBoundedPrim w -> MessageBuilder
liftBoundedPrim (MessageBoundedPrim 10 -> MessageBuilder)
-> (a -> MessageBoundedPrim 10) -> a -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> MessageBoundedPrim 10
base128Varint64 (Word64 -> MessageBoundedPrim 10)
-> (a -> Word64) -> a -> MessageBoundedPrim 10
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Word64
f)
{-# INLINE packedVarintsV #-}

-- | A faster but more specialized variant of:
--
-- > packedVarintsV (fromIntegral . fromEnum) num
--
-- >>> packedBoolsV not 1 ([False, True] :: Data.Vector.Vector Bool)
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\n\STX\SOH\NUL"
packedBoolsV ::
  Vector v a => (a -> Bool) -> FieldNumber -> v a -> MessageBuilder
packedBoolsV :: forall (v :: * -> *) a.
Vector v a =>
(a -> Bool) -> FieldNumber -> v a -> MessageBuilder
packedBoolsV a -> Bool
f FieldNumber
num = FieldNumber -> MessageBuilder -> MessageBuilder
embedded FieldNumber
num (MessageBuilder -> MessageBuilder)
-> (v a -> MessageBuilder) -> v a -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuildR -> MessageBuilder
MessageBuilder (BuildR -> MessageBuilder)
-> (v a -> BuildR) -> v a -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> BuildR
payload
  where
    payload :: v a -> BuildR
payload = (a -> FixedPrim 1) -> v a -> BuildR
forall (w :: Nat) (v :: * -> *) a.
(KnownNat w, Vector v a) =>
(a -> FixedPrim w) -> v a -> BuildR
Prim.vectorFixedPrim (Word8 -> FixedPrim 1
Prim.word8 (Word8 -> FixedPrim 1) -> (a -> Word8) -> a -> FixedPrim 1
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> (a -> Int) -> a -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Int
forall a. Enum a => a -> Int
fromEnum (Bool -> Int) -> (a -> Bool) -> a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)
{-# INLINE packedBoolsV #-}

-- | Encode fixed-width Word32s in the space-efficient packed format.
-- But consider 'packedFixed32V', which may be faster.
--
-- The values to be encoded are specified by mapping the elements of a vector.
--
-- >>> packedFixed32 1 [1, 2, 3]
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\n\f\SOH\NUL\NUL\NUL\STX\NUL\NUL\NUL\ETX\NUL\NUL\NUL"
packedFixed32 :: Foldable f => FieldNumber -> f Word32 -> MessageBuilder
packedFixed32 :: forall (f :: * -> *).
Foldable f =>
FieldNumber -> f Word32 -> MessageBuilder
packedFixed32 FieldNumber
num = (f Word32 -> MessageBuilder) -> f Word32 -> MessageBuilder
forall a. (a -> MessageBuilder) -> a -> MessageBuilder
etaMessageBuilder (FieldNumber -> MessageBuilder -> MessageBuilder
embedded FieldNumber
num (MessageBuilder -> MessageBuilder)
-> (f Word32 -> MessageBuilder) -> f Word32 -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f Word32 -> MessageBuilder
payload)
  where
    payload :: f Word32 -> MessageBuilder
payload = (Word32 -> MessageBuilder) -> f Word32 -> MessageBuilder
forall m a. Monoid m => (a -> m) -> f a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (BuildR -> MessageBuilder
MessageBuilder (BuildR -> MessageBuilder)
-> (Word32 -> BuildR) -> Word32 -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> BuildR
RB.word32LE)
{-# INLINE packedFixed32 #-}

-- | A faster but more specialized variant of:
--
-- > \f num -> packedFixed32 num . fmap f
--
-- >>> packedFixed32V (subtract 10) 1 ([11, 12, 13] :: Data.Vector.Vector Word32)
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\n\f\SOH\NUL\NUL\NUL\STX\NUL\NUL\NUL\ETX\NUL\NUL\NUL"
packedFixed32V ::
  Vector v a => (a -> Word32) -> FieldNumber -> v a -> MessageBuilder
packedFixed32V :: forall (v :: * -> *) a.
Vector v a =>
(a -> Word32) -> FieldNumber -> v a -> MessageBuilder
packedFixed32V a -> Word32
f FieldNumber
num = (v a -> MessageBuilder) -> v a -> MessageBuilder
forall a. (a -> MessageBuilder) -> a -> MessageBuilder
etaMessageBuilder (FieldNumber -> MessageBuilder -> MessageBuilder
embedded FieldNumber
num (MessageBuilder -> MessageBuilder)
-> (v a -> MessageBuilder) -> v a -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> MessageBuilder
payload)
  where
    payload :: v a -> MessageBuilder
payload = BuildR -> MessageBuilder
MessageBuilder (BuildR -> MessageBuilder)
-> (v a -> BuildR) -> v a -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> FixedPrim 4) -> v a -> BuildR
forall (w :: Nat) (v :: * -> *) a.
(KnownNat w, Vector v a) =>
(a -> FixedPrim w) -> v a -> BuildR
Prim.vectorFixedPrim (Word32 -> FixedPrim 4
Prim.word32LE (Word32 -> FixedPrim 4) -> (a -> Word32) -> a -> FixedPrim 4
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Word32
f)
{-# INLINE packedFixed32V #-}

-- | Encode fixed-width Word64s in the space-efficient packed format.
-- But consider 'packedFixed64V', which may be faster.
--
-- The values to be encoded are specified by mapping the elements of a vector.
--
-- >>> packedFixed64 1 [1, 2, 3]
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\n\CAN\SOH\NUL\NUL\NUL\NUL\NUL\NUL\NUL\STX\NUL\NUL\NUL\NUL\NUL\NUL\NUL\ETX\NUL\NUL\NUL\NUL\NUL\NUL\NUL"
packedFixed64 :: Foldable f => FieldNumber -> f Word64 -> MessageBuilder
packedFixed64 :: forall (f :: * -> *).
Foldable f =>
FieldNumber -> f Word64 -> MessageBuilder
packedFixed64 FieldNumber
num = (f Word64 -> MessageBuilder) -> f Word64 -> MessageBuilder
forall a. (a -> MessageBuilder) -> a -> MessageBuilder
etaMessageBuilder (FieldNumber -> MessageBuilder -> MessageBuilder
embedded FieldNumber
num (MessageBuilder -> MessageBuilder)
-> (f Word64 -> MessageBuilder) -> f Word64 -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f Word64 -> MessageBuilder
payload)
  where
    payload :: f Word64 -> MessageBuilder
payload = (Word64 -> MessageBuilder) -> f Word64 -> MessageBuilder
forall m a. Monoid m => (a -> m) -> f a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (BuildR -> MessageBuilder
MessageBuilder (BuildR -> MessageBuilder)
-> (Word64 -> BuildR) -> Word64 -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> BuildR
RB.word64LE)
{-# INLINE packedFixed64 #-}

-- | A faster but more specialized variant of:
--
-- > \f num -> packedFixed64 num . fmap f
--
-- >>> packedFixed64V (subtract 10) 1 ([11, 12, 13] :: Data.Vector.Vector Word64)
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\n\CAN\SOH\NUL\NUL\NUL\NUL\NUL\NUL\NUL\STX\NUL\NUL\NUL\NUL\NUL\NUL\NUL\ETX\NUL\NUL\NUL\NUL\NUL\NUL\NUL"
packedFixed64V ::
  Vector v a => (a -> Word64) -> FieldNumber -> v a -> MessageBuilder
packedFixed64V :: forall (v :: * -> *) a.
Vector v a =>
(a -> Word64) -> FieldNumber -> v a -> MessageBuilder
packedFixed64V a -> Word64
f FieldNumber
num = (v a -> MessageBuilder) -> v a -> MessageBuilder
forall a. (a -> MessageBuilder) -> a -> MessageBuilder
etaMessageBuilder (FieldNumber -> MessageBuilder -> MessageBuilder
embedded FieldNumber
num (MessageBuilder -> MessageBuilder)
-> (v a -> MessageBuilder) -> v a -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> MessageBuilder
payload)
  where
    payload :: v a -> MessageBuilder
payload = BuildR -> MessageBuilder
MessageBuilder (BuildR -> MessageBuilder)
-> (v a -> BuildR) -> v a -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> FixedPrim 8) -> v a -> BuildR
forall (w :: Nat) (v :: * -> *) a.
(KnownNat w, Vector v a) =>
(a -> FixedPrim w) -> v a -> BuildR
Prim.vectorFixedPrim (Word64 -> FixedPrim 8
Prim.word64LE (Word64 -> FixedPrim 8) -> (a -> Word64) -> a -> FixedPrim 8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Word64
f)
{-# INLINE packedFixed64V #-}

-- | Encode floats in the space-efficient packed format.
-- But consider 'packedFloatsV', which may be faster.
--
-- >>> 1 `packedFloats` [1, 2, 3]
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\n\f\NUL\NUL\128?\NUL\NUL\NUL@\NUL\NUL@@"
packedFloats :: Foldable f => FieldNumber -> f Float -> MessageBuilder
packedFloats :: forall (f :: * -> *).
Foldable f =>
FieldNumber -> f Float -> MessageBuilder
packedFloats FieldNumber
num = (f Float -> MessageBuilder) -> f Float -> MessageBuilder
forall a. (a -> MessageBuilder) -> a -> MessageBuilder
etaMessageBuilder (FieldNumber -> MessageBuilder -> MessageBuilder
embedded FieldNumber
num (MessageBuilder -> MessageBuilder)
-> (f Float -> MessageBuilder) -> f Float -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f Float -> MessageBuilder
payload)
  where
    payload :: f Float -> MessageBuilder
payload = (Float -> MessageBuilder) -> f Float -> MessageBuilder
forall m a. Monoid m => (a -> m) -> f a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (BuildR -> MessageBuilder
MessageBuilder (BuildR -> MessageBuilder)
-> (Float -> BuildR) -> Float -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> BuildR
RB.floatLE)
{-# INLINE packedFloats #-}

-- | A faster but more specialized variant of:
--
-- > \f num -> packedFloats num . fmap f
--
-- >>> packedFloatsV (subtract 10) 1 ([11, 12, 13] :: Data.Vector.Vector Float)
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\n\f\NUL\NUL\128?\NUL\NUL\NUL@\NUL\NUL@@"
packedFloatsV ::
  Vector v a => (a -> Float) -> FieldNumber -> v a -> MessageBuilder
packedFloatsV :: forall (v :: * -> *) a.
Vector v a =>
(a -> Float) -> FieldNumber -> v a -> MessageBuilder
packedFloatsV a -> Float
f FieldNumber
num = (v a -> MessageBuilder) -> v a -> MessageBuilder
forall a. (a -> MessageBuilder) -> a -> MessageBuilder
etaMessageBuilder (FieldNumber -> MessageBuilder -> MessageBuilder
embedded FieldNumber
num (MessageBuilder -> MessageBuilder)
-> (v a -> MessageBuilder) -> v a -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> MessageBuilder
payload)
  where
    payload :: v a -> MessageBuilder
payload = BuildR -> MessageBuilder
MessageBuilder (BuildR -> MessageBuilder)
-> (v a -> BuildR) -> v a -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> FixedPrim 4) -> v a -> BuildR
forall (w :: Nat) (v :: * -> *) a.
(KnownNat w, Vector v a) =>
(a -> FixedPrim w) -> v a -> BuildR
Prim.vectorFixedPrim (Float -> FixedPrim 4
Prim.floatLE (Float -> FixedPrim 4) -> (a -> Float) -> a -> FixedPrim 4
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Float
f)
{-# INLINE packedFloatsV #-}

-- | Encode doubles in the space-efficient packed format.
-- But consider 'packedDoublesV', which may be faster.
--
-- >>> 1 `packedDoubles` [1, 2, 3]
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\n\CAN\NUL\NUL\NUL\NUL\NUL\NUL\240?\NUL\NUL\NUL\NUL\NUL\NUL\NUL@\NUL\NUL\NUL\NUL\NUL\NUL\b@"
packedDoubles :: Foldable f => FieldNumber -> f Double -> MessageBuilder
packedDoubles :: forall (f :: * -> *).
Foldable f =>
FieldNumber -> f Double -> MessageBuilder
packedDoubles FieldNumber
num = (f Double -> MessageBuilder) -> f Double -> MessageBuilder
forall a. (a -> MessageBuilder) -> a -> MessageBuilder
etaMessageBuilder (FieldNumber -> MessageBuilder -> MessageBuilder
embedded FieldNumber
num (MessageBuilder -> MessageBuilder)
-> (f Double -> MessageBuilder) -> f Double -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f Double -> MessageBuilder
payload)
  where
    payload :: f Double -> MessageBuilder
payload = (Double -> MessageBuilder) -> f Double -> MessageBuilder
forall m a. Monoid m => (a -> m) -> f a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (BuildR -> MessageBuilder
MessageBuilder (BuildR -> MessageBuilder)
-> (Double -> BuildR) -> Double -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> BuildR
RB.doubleLE)
{-# INLINE packedDoubles #-}

-- | A faster but more specialized variant of:
--
-- > \f num -> packedDoubles num . fmap f
--
-- >>> packedDoublesV (subtract 10) 1 ([11, 12, 13] :: Data.Vector.Vector Double)
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\n\CAN\NUL\NUL\NUL\NUL\NUL\NUL\240?\NUL\NUL\NUL\NUL\NUL\NUL\NUL@\NUL\NUL\NUL\NUL\NUL\NUL\b@"
packedDoublesV ::
  Vector v a => (a -> Double) -> FieldNumber -> v a -> MessageBuilder
packedDoublesV :: forall (v :: * -> *) a.
Vector v a =>
(a -> Double) -> FieldNumber -> v a -> MessageBuilder
packedDoublesV a -> Double
f FieldNumber
num = (v a -> MessageBuilder) -> v a -> MessageBuilder
forall a. (a -> MessageBuilder) -> a -> MessageBuilder
etaMessageBuilder (FieldNumber -> MessageBuilder -> MessageBuilder
embedded FieldNumber
num (MessageBuilder -> MessageBuilder)
-> (v a -> MessageBuilder) -> v a -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v a -> MessageBuilder
payload)
  where
    payload :: v a -> MessageBuilder
payload = BuildR -> MessageBuilder
MessageBuilder (BuildR -> MessageBuilder)
-> (v a -> BuildR) -> v a -> MessageBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> FixedPrim 8) -> v a -> BuildR
forall (w :: Nat) (v :: * -> *) a.
(KnownNat w, Vector v a) =>
(a -> FixedPrim w) -> v a -> BuildR
Prim.vectorFixedPrim (Double -> FixedPrim 8
Prim.doubleLE (Double -> FixedPrim 8) -> (a -> Double) -> a -> FixedPrim 8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Double
f)
{-# INLINE packedDoublesV #-}

-- | Encode an embedded message.
--
-- The message is represented as a 'MessageBuilder', so it is possible to chain
-- encoding functions.
--
-- For example:
--
-- >>> 1 `embedded` (1 `string` "this message" <> 2 `string` " is embedded")
-- Proto3.Wire.Encode.unsafeFromLazyByteString "\n\FS\n\fthis message\DC2\f is embedded"
embedded :: FieldNumber -> MessageBuilder -> MessageBuilder
embedded :: FieldNumber -> MessageBuilder -> MessageBuilder
embedded = \FieldNumber
num (MessageBuilder BuildR
bb) ->
    BuildR -> MessageBuilder
MessageBuilder ((Int -> BuildR) -> BuildR -> BuildR
RB.withLengthOf (BoundedPrim 20 -> BuildR
forall (w :: Nat). KnownNat w => BoundedPrim w -> BuildR
Prim.liftBoundedPrim (BoundedPrim 20 -> BuildR)
-> (Int -> BoundedPrim 20) -> Int -> BuildR
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldNumber -> Int -> BoundedPrim 20
prefix FieldNumber
num) BuildR
bb)
  where
    prefix :: FieldNumber -> Int -> PNullary BoundedPrim 20
prefix FieldNumber
num Int
len =
      MessageBoundedPrim 10 -> BoundedPrim 10
forall (w :: Nat). MessageBoundedPrim w -> BoundedPrim w
unMessageBoundedPrim (FieldNumber -> WireType -> MessageBoundedPrim 10
fieldHeader FieldNumber
num WireType
LengthDelimited) PNullary BoundedPrim 10
-> PNullary BoundedPrim 10 -> PNullary BoundedPrim 20
forall k (n :: k -> *) (t :: k) (u :: k) (v :: k).
PSemigroup n t u v =>
PNullary n t -> PNullary n u -> PNullary n v
&<>
      Word -> BoundedPrim 10
Prim.wordBase128LEVar (forall a b. (Integral a, Num b) => a -> b
fromIntegral @Int @Word Int
len)
{-# INLINE embedded #-}