1111//
1212// [1]: https://msgpack.org/index.html
1313
14+ #include < climits>
1415#include < cstddef>
1516#include < cstdint>
1617#include < string>
@@ -30,6 +31,8 @@ void pack_integer(std::string& buffer, std::int32_t value);
3031void pack_double (std::string& buffer, double value);
3132
3233Expected<void > pack_string (std::string& buffer, StringView value);
34+ Expected<void > pack_string (std::string& buffer, const char * begin,
35+ std::size_t size);
3336
3437Expected<void > pack_array (std::string& buffer, std::size_t size);
3538
@@ -65,17 +68,16 @@ Expected<void> pack_map(std::string& buffer, const PairIterable& pairs,
6568// an even number of arguments. First in each pair of arguments is `key`, the
6669// key name of the corresponding map item. Second in each pair of arguments is
6770// `pack_value`, a function that encodes the corresponding value. `pack_value`
68- // is invoked with two arguments: the first is a reference to `buffer`, and the
69- // second is a reference to the current value. `pack_value` returns an
70- // `Expected<void>`. If the return value is an error, then iteration is halted
71- // and the error is returned. If some other error occurs, then an error is
72- // returned. Otherwise, the non-error value is returned.
73- template <typename Key, typename PackValue, typename ... Rest>
74- Expected<void > pack_map (std::string& buffer, Key&& key, PackValue&& pack_value,
75- Rest&&... rest);
76-
77- template <typename Key, typename PackValue, typename ... Rest>
78- Expected<void > pack_map_suffix (std::string& buffer, Key&& key,
71+ // is invoked with one argument: a reference to `buffer`. `pack_value` returns
72+ // an `Expected<void>`. If the return value is an error, then iteration is
73+ // halted and the error is returned. If some other error occurs, then an error
74+ // is returned. Otherwise, the non-error value is returned.
75+ template <typename PackValue, typename ... Rest>
76+ Expected<void > pack_map (std::string& buffer, StringView key,
77+ PackValue&& pack_value, Rest&&... rest);
78+
79+ template <typename PackValue, typename ... Rest>
80+ Expected<void > pack_map_suffix (std::string& buffer, StringView key,
7981 PackValue&& pack_value, Rest&&... rest);
8082Expected<void > pack_map_suffix (std::string& buffer);
8183
@@ -117,25 +119,23 @@ Expected<void> pack_map(std::string& buffer, const PairIterable& pairs,
117119 return result;
118120}
119121
120- template <typename Key, typename PackValue, typename ... Rest>
121- Expected<void > pack_map (std::string& buffer, Key&& key, PackValue&& pack_value,
122- Rest&&... rest) {
123- Expected<void > result;
122+ template <typename PackValue, typename ... Rest>
123+ Expected<void > pack_map (std::string& buffer, StringView key,
124+ PackValue&& pack_value, Rest&&... rest) {
124125 static_assert (
125126 sizeof ...(rest) % 2 == 0 ,
126127 " pack_map must receive an even number of arguments after the first." );
127- result = pack_map (buffer, 1 + sizeof ...(rest) / 2 );
128- if (!result) {
129- return result;
130- }
131- result = pack_map_suffix (buffer, std::forward<Key>(key),
132- std::forward<PackValue>(pack_value),
133- std::forward<Rest>(rest)...);
134- return result;
128+ static_assert (
129+ sizeof ...(rest) / 2 <= UINT32_MAX,
130+ " You're passing more than eight billion arguments to a function." );
131+ (void )pack_map (buffer, 1 + sizeof ...(rest) / 2 );
132+
133+ return pack_map_suffix (buffer, key, std::forward<PackValue>(pack_value),
134+ std::forward<Rest>(rest)...);
135135}
136136
137- template <typename Key, typename PackValue, typename ... Rest>
138- Expected<void > pack_map_suffix (std::string& buffer, Key&& key,
137+ template <typename PackValue, typename ... Rest>
138+ Expected<void > pack_map_suffix (std::string& buffer, StringView key,
139139 PackValue&& pack_value, Rest&&... rest) {
140140 Expected<void > result;
141141 result = pack_string (buffer, key);
@@ -159,6 +159,10 @@ inline void pack_integer(std::string& buffer, std::int32_t value) {
159159 pack_integer (buffer, std::int64_t (value));
160160}
161161
162+ inline Expected<void > pack_string (std::string& buffer, StringView value) {
163+ return pack_string (buffer, value.begin (), value.size ());
164+ }
165+
162166} // namespace msgpack
163167} // namespace tracing
164168} // namespace datadog
0 commit comments