robot-rust 0.2.1

The robot-rust project, bindings for wpilib, ctre, and rev in rust.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

#pragma once

#include <stdint.h>

#include <concepts>
#include <memory>
#include <span>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>

#include <fmt/format.h>

#include "wpi/Endian.h"
#include "wpi/array.h"
#include "wpi/bit.h"
#include "wpi/ct_string.h"
#include "wpi/function_ref.h"
#include "wpi/mutex.h"
#include "wpi/type_traits.h"

namespace wpi {

/**
 * Struct serialization template. Unspecialized class has no members; only
 * specializations of this class are useful, and only if they meet the
 * StructSerializable concept.
 *
 * @tparam T type to serialize/deserialize
 * @tparam I optional struct type info
 */
template <typename T, typename... I>
struct Struct {};

/**
 * Specifies that a type is capable of raw struct serialization and
 * deserialization.
 *
 * This is designed for serializing small fixed-size data structures in the
 * fastest and most compact means possible. Serialization consists of writing
 * values into a mutable std::span and deserialization consists of reading
 * values from an immutable std::span.
 *
 * Implementations must define a template specialization for wpi::Struct with
 * T being the type that is being serialized/deserialized, with the following
 * static members (as enforced by this concept):
 * - std::string_view GetTypeName(): function that returns the type name
 * - size_t GetSize(): function that returns the structure size in bytes
 * - std::string_view GetSchema(): function that returns the struct schema
 * - T Unpack(std::span<const uint8_t>): function for deserialization
 * - void Pack(std::span<uint8_t>, T&& value): function for
 *   serialization
 *
 * If possible, the GetTypeName(), GetSize(), and GetSchema() functions should
 * be marked constexpr. GetTypeName() and GetSchema() may return types other
 * than std::string_view, as long as the return value is convertible to
 * std::string_view.
 *
 * If the struct has nested structs, implementations should also meet the
 * requirements of HasNestedStruct<T>.
 */
template <typename T, typename... I>
concept StructSerializable = requires(std::span<const uint8_t> in,
                                      std::span<uint8_t> out, T&& value,
                                      const I&... info) {
  typename Struct<typename std::remove_cvref_t<T>,
                  typename std::remove_cvref_t<I>...>;
  {
    Struct<typename std::remove_cvref_t<T>,
           typename std::remove_cvref_t<I>...>::GetTypeName(info...)
  } -> std::convertible_to<std::string_view>;
  {
    Struct<typename std::remove_cvref_t<T>,
           typename std::remove_cvref_t<I>...>::GetSize(info...)
  } -> std::convertible_to<size_t>;
  {
    Struct<typename std::remove_cvref_t<T>,
           typename std::remove_cvref_t<I>...>::GetSchema(info...)
  } -> std::convertible_to<std::string_view>;
  {
    Struct<typename std::remove_cvref_t<T>,
           typename std::remove_cvref_t<I>...>::Unpack(in, info...)
  } -> std::same_as<typename std::remove_cvref_t<T>>;
  Struct<typename std::remove_cvref_t<T>,
         typename std::remove_cvref_t<I>...>::Pack(out, std::forward<T>(value),
                                                   info...);
};

/**
 * Specifies that a type is capable of in-place raw struct deserialization.
 *
 * In addition to meeting StructSerializable, implementations must define a
 * wpi::Struct<T> static member `void UnpackInto(T*, std::span<const uint8_t>)`
 * to update the pointed-to T with the contents of the span.
 */
template <typename T, typename... I>
concept MutableStructSerializable =
    StructSerializable<T, I...> &&
    requires(T* out, std::span<const uint8_t> in, const I&... info) {
      Struct<typename std::remove_cvref_t<T>,
             typename std::remove_cvref_t<I>...>::UnpackInto(out, in, info...);
    };

/**
 * Specifies that a struct type has nested struct declarations.
 *
 * In addition to meeting StructSerializable, implementations must define a
 * wpi::Struct<T> static member
 * `void ForEachNested(std::invocable<std::string_view, std::string_view) auto
 * fn)` (or equivalent) and call ForEachStructSchema<Type> on each nested struct
 * type.
 */
template <typename T, typename... I>
concept HasNestedStruct =
    StructSerializable<T, I...> &&
    requires(function_ref<void(std::string_view, std::string_view)> fn,
             const I&... info) {
      Struct<typename std::remove_cvref_t<T>,
             typename std::remove_cvref_t<I>...>::ForEachNested(fn, info...);
    };

/**
 * Unpack a serialized struct.
 *
 * @tparam T object type
 * @param data raw struct data
 * @param info optional struct type info
 * @return Deserialized object
 */
template <typename T, typename... I>
  requires StructSerializable<T, I...>
inline T UnpackStruct(std::span<const uint8_t> data, const I&... info) {
  using S = Struct<T, typename std::remove_cvref_t<I>...>;
  return S::Unpack(data, info...);
}

/**
 * Unpack a serialized struct starting at a given offset within the data.
 * This is primarily useful in unpack implementations to unpack nested structs.
 *
 * @tparam T object type
 * @tparam Offset starting offset
 * @param data raw struct data
 * @param info optional struct type info
 * @return Deserialized object
 */
template <typename T, size_t Offset, typename... I>
  requires StructSerializable<T, I...>
inline T UnpackStruct(std::span<const uint8_t> data, const I&... info) {
  using S = Struct<T, typename std::remove_cvref_t<I>...>;
  return S::Unpack(data.subspan(Offset), info...);
}

/**
 * Unpack a serialized struct array starting at a given offset within the data.
 * This is primarily useful in unpack implementations to unpack nested struct
 * arrays.
 *
 * @tparam T object type
 * @tparam Offset starting offset
 * @tparam N number of objects
 * @param data raw struct data
 * @return Deserialized array
 */
template <StructSerializable T, size_t Offset, size_t N>
inline wpi::array<T, N> UnpackStructArray(std::span<const uint8_t> data) {
  if (is_constexpr([] { Struct<std::remove_cvref_t<T>>::GetSize(); })) {
    constexpr auto StructSize = Struct<std::remove_cvref_t<T>>::GetSize();
    wpi::array<T, N> arr(wpi::empty_array);
    [&]<size_t... Is>(std::index_sequence<Is...>) {
      ((arr[Is] = UnpackStruct<T, Offset + Is * StructSize>(data)), ...);
    }(std::make_index_sequence<N>{});
    return arr;
  } else {
    auto size = Struct<std::remove_cvref_t<T>>::GetSize();
    wpi::array<T, N> arr(wpi::empty_array);
    for (size_t i = 0; i < N; i++) {
      arr[i] = UnpackStruct<T>(data);
      data = data.subspan(size);
    }
    return arr;
  }
}

/**
 * Pack a serialized struct.
 *
 * @param data struct storage (mutable, output)
 * @param value object
 * @param info optional struct type info
 */
template <typename T, typename... I>
  requires StructSerializable<T, I...>
inline void PackStruct(std::span<uint8_t> data, T&& value, const I&... info) {
  using S = Struct<typename std::remove_cvref_t<T>,
                   typename std::remove_cvref_t<I>...>;
  S::Pack(data, std::forward<T>(value), info...);
}

/**
 * Pack a serialized struct starting at a given offset within the data. This is
 * primarily useful in pack implementations to pack nested structs.
 *
 * @tparam Offset starting offset
 * @param data struct storage (mutable, output)
 * @param value object
 * @param info optional struct type info
 */
template <size_t Offset, typename T, typename... I>
  requires StructSerializable<T, I...>
inline void PackStruct(std::span<uint8_t> data, T&& value, const I&... info) {
  using S = Struct<typename std::remove_cvref_t<T>,
                   typename std::remove_cvref_t<I>...>;
  S::Pack(data.subspan(Offset), std::forward<T>(value), info...);
}

/**
 * Pack a serialized struct array starting at a given offset within the data.
 * This is primarily useful in pack implementations to pack nested struct
 * arrays.
 *
 * @tparam Offset starting offset
 * @tparam N number of objects
 * @param data struct storage (mutable, output)
 * @param arr array of object
 */
template <size_t Offset, size_t N, StructSerializable T>
inline void PackStructArray(std::span<uint8_t> data,
                            const wpi::array<T, N>& arr) {
  if (is_constexpr([] { Struct<std::remove_cvref_t<T>>::GetSize(); })) {
    constexpr auto StructSize = Struct<std::remove_cvref_t<T>>::GetSize();
    [&]<size_t... Is>(std::index_sequence<Is...>) {
      (PackStruct<Offset + Is * StructSize>(data, arr[Is]), ...);
    }(std::make_index_sequence<N>{});
  } else {
    auto size = Struct<std::remove_cvref_t<T>>::GetSize();
    for (auto&& val : arr) {
      PackStruct(data, val);
      data = data.subspan(size);
    }
  }
}

/**
 * Unpack a serialized struct into an existing object, overwriting its contents.
 *
 * @param out object (output)
 * @param data raw struct data
 * @param info optional struct type info
 */
template <typename T, typename... I>
  requires StructSerializable<T, I...>
inline void UnpackStructInto(T* out, std::span<const uint8_t> data,
                             const I&... info) {
  using S = Struct<T, typename std::remove_cvref_t<I>...>;
  if constexpr (MutableStructSerializable<T, I...>) {
    S::UnpackInto(out, data, info...);
  } else {
    *out = UnpackStruct<T>(data, info...);
  }
}

/**
 * Unpack a serialized struct into an existing object, overwriting its contents,
 * and starting at a given offset within the data.
 * This is primarily useful in unpack implementations to unpack nested structs.
 *
 * @tparam Offset starting offset
 * @param out object (output)
 * @param data raw struct data
 * @param info optional struct type info
 */
template <size_t Offset, typename T, typename... I>
  requires StructSerializable<T, I...>
inline void UnpackStructInto(T* out, std::span<const uint8_t> data,
                             const I&... info) {
  using S = Struct<T, typename std::remove_cvref_t<I>...>;
  if constexpr (MutableStructSerializable<T, I...>) {
    S::UnpackInto(out, data.subspan(Offset), info...);
  } else {
    *out = UnpackStruct<T, Offset>(data, info...);
  }
}

/**
 * Get the type name for a raw struct serializable type
 *
 * @tparam T serializable type
 * @param info optional struct type info
 * @return type name
 */
template <typename T, typename... I>
  requires StructSerializable<T, I...>
constexpr auto GetStructTypeName(const I&... info) {
  using S = Struct<T, typename std::remove_cvref_t<I>...>;
  return S::GetTypeName(info...);
}

/**
 * Get the type string for a raw struct serializable type
 *
 * @tparam T serializable type
 * @param info optional struct type info
 * @return type string (struct: followed by type name)
 */
template <typename T, typename... I>
  requires StructSerializable<T, I...>
constexpr auto GetStructTypeString(const I&... info) {
  using S = Struct<T, typename std::remove_cvref_t<I>...>;
  if constexpr (sizeof...(I) == 0 &&
                is_constexpr([&] { S::GetTypeName(info...); })) {
    constexpr auto typeName = S::GetTypeName(info...);
    using namespace literals;
    return Concat(
        "struct:"_ct_string,
        ct_string<char, std::char_traits<char>, typeName.size()>{typeName});
  } else {
    return fmt::format("struct:{}", S::GetTypeName(info...));
  }
}

/**
 * Get the size for a raw struct serializable type
 *
 * @tparam T serializable type
 * @param info optional struct type info
 * @return size
 */
template <typename T, typename... I>
  requires StructSerializable<T, I...>
constexpr size_t GetStructSize(const I&... info) {
  using S = Struct<T, typename std::remove_cvref_t<I>...>;
  return S::GetSize(info...);
}

template <typename T, size_t N, typename... I>
  requires StructSerializable<T, I...>
constexpr auto MakeStructArrayTypeName(const I&... info) {
  using S = Struct<T, typename std::remove_cvref_t<I>...>;
  if constexpr (sizeof...(I) == 0 &&
                is_constexpr([&] { S::GetTypeName(info...); })) {
    constexpr auto typeName = S::GetTypeName(info...);
    using namespace literals;
    if constexpr (N == std::dynamic_extent) {
      return Concat(
          ct_string<char, std::char_traits<char>, typeName.size()>{typeName},
          "[]"_ct_string);
    } else {
      return Concat(
          ct_string<char, std::char_traits<char>, typeName.size()>{typeName},
          "["_ct_string, NumToCtString<N>(), "]"_ct_string);
    }
  } else {
    if constexpr (N == std::dynamic_extent) {
      return fmt::format("{}[]", S::GetTypeName(info...));
    } else {
      return fmt::format("{}[{}]", S::GetTypeName(info...), N);
    }
  }
}

template <typename T, size_t N, typename... I>
  requires StructSerializable<T, I...>
constexpr auto MakeStructArrayTypeString(const I&... info) {
  using S = Struct<T, typename std::remove_cvref_t<I>...>;
  if constexpr (sizeof...(I) == 0 &&
                is_constexpr([&] { S::GetTypeName(info...); })) {
    using namespace literals;
    return Concat("struct:"_ct_string, MakeStructArrayTypeName<T, N>(info...));
  } else {
    return fmt::format("struct:{}", MakeStructArrayTypeName<T, N>(info...));
  }
}

template <typename T, size_t N, typename... I>
  requires StructSerializable<T, I...>
constexpr auto MakeStructArraySchema(const I&... info) {
  using S = Struct<T, typename std::remove_cvref_t<I>...>;
  if constexpr (sizeof...(I) == 0 &&
                is_constexpr([&] { S::GetSchema(info...); })) {
    constexpr auto schema = S::GetSchema(info...);
    using namespace literals;
    if constexpr (N == std::dynamic_extent) {
      return Concat(
          ct_string<char, std::char_traits<char>, schema.size()>{schema},
          "[]"_ct_string);
    } else {
      return Concat(
          ct_string<char, std::char_traits<char>, schema.size()>{schema},
          "["_ct_string, NumToCtString<N>(), "]"_ct_string);
    }
  } else {
    if constexpr (N == std::dynamic_extent) {
      return fmt::format("{}[]", S::GetSchema(info...));
    } else {
      return fmt::format("{}[{}]", S::GetSchema(info...), N);
    }
  }
}

template <typename T, typename... I>
  requires StructSerializable<T, I...>
constexpr std::string_view GetStructSchema(const I&... info) {
  using S = Struct<T, typename std::remove_cvref_t<I>...>;
  return S::GetSchema(info...);
}

template <typename T, typename... I>
  requires StructSerializable<T, I...>
constexpr std::span<const uint8_t> GetStructSchemaBytes(const I&... info) {
  using S = Struct<T, typename std::remove_cvref_t<I>...>;
  auto schema = S::GetSchema(info...);
  return {reinterpret_cast<const uint8_t*>(schema.data()), schema.size()};
}

template <typename T, typename... I>
  requires StructSerializable<T, I...>
void ForEachStructSchema(
    std::invocable<std::string_view, std::string_view> auto fn,
    const I&... info) {
  using S = Struct<typename std::remove_cvref_t<T>,
                   typename std::remove_cvref_t<I>...>;
  if constexpr (HasNestedStruct<T, I...>) {
    S::ForEachNested(fn, info...);
  }
  fn(GetStructTypeString<T>(info...), S::GetSchema(info...));
}

template <typename T, typename... I>
  requires StructSerializable<T, I...>
class StructArrayBuffer {
  using S = Struct<T, I...>;

 public:
  StructArrayBuffer() = default;
  StructArrayBuffer(const StructArrayBuffer&) = delete;
  StructArrayBuffer& operator=(const StructArrayBuffer&) = delete;
  StructArrayBuffer(StructArrayBuffer&& rhs) : m_buf{std::move(rhs.m_buf)} {}
  StructArrayBuffer& operator=(StructArrayBuffer&& rhs) {
    m_buf = std::move(rhs.m_buf);
    return *this;
  }

  template <typename U, typename F>
    requires
#if __cpp_lib_ranges >= 201911L
      std::ranges::range<U> &&
      std::convertible_to<std::ranges::range_value_t<U>, T> &&
#endif
      std::invocable<F, std::span<const uint8_t>>
    void Write(U&& data, F&& func, const I&... info) {
    auto size = S::GetSize(info...);
    if ((std::size(data) * size) < 256) {
      // use the stack
      uint8_t buf[256];
      auto out = buf;
      for (auto&& val : data) {
        S::Pack(std::span<uint8_t>{std::to_address(out), size},
                std::forward<decltype(val)>(val), info...);
        out += size;
      }
      func(std::span<uint8_t>{buf, out});
    } else {
      std::scoped_lock lock{m_mutex};
      m_buf.resize(std::size(data) * size);
      auto out = m_buf.begin();
      for (auto&& val : data) {
        S::Pack(std::span<uint8_t>{std::to_address(out), size},
                std::forward<decltype(val)>(val), info...);
        out += size;
      }
      func(m_buf);
    }
  }

 private:
  wpi::mutex m_mutex;
  std::vector<uint8_t> m_buf;
};

/**
 * Raw struct support for fixed-size arrays of other structs.
 */
template <typename T, size_t N, typename... I>
  requires StructSerializable<T, I...>
struct Struct<std::array<T, N>, I...> {
  static constexpr auto GetTypeName(const I&... info) {
    return MakeStructArrayTypeName<T, N>(info...);
  }
  static constexpr size_t GetSize(const I&... info) {
    return N * GetStructSize<T>(info...);
  }
  static constexpr auto GetSchema(const I&... info) {
    return MakeStructArraySchema<T, N>(info...);
  }
  static std::array<T, N> Unpack(std::span<const uint8_t> data,
                                 const I&... info) {
    auto size = GetStructSize<T>(info...);
    std::array<T, N> result;
    for (size_t i = 0; i < N; ++i) {
      result[i] = UnpackStruct<T, 0>(data, info...);
      data = data.subspan(size);
    }
    return result;
  }
  static void Pack(std::span<uint8_t> data, std::span<const T, N> values,
                   const I&... info) {
    auto size = GetStructSize<T>(info...);
    std::span<uint8_t> unsizedData = data;
    for (auto&& val : values) {
      PackStruct(unsizedData, val, info...);
      unsizedData = unsizedData.subspan(size);
    }
  }
  static void UnpackInto(std::array<T, N>* out, std::span<const uint8_t> data,
                         const I&... info) {
    UnpackInto(std::span{*out}, data, info...);
  }
  // alternate span-based function
  static void UnpackInto(std::span<T, N> out, std::span<const uint8_t> data,
                         const I&... info) {
    auto size = GetStructSize<T>(info...);
    std::span<const uint8_t> unsizedData = data;
    for (size_t i = 0; i < N; ++i) {
      UnpackStructInto(&out[i], unsizedData, info...);
      unsizedData = unsizedData.subspan(size);
    }
  }
};

/**
 * Raw struct support for boolean values.
 * Primarily useful for higher level struct implementations.
 */
template <>
struct Struct<bool> {
  static constexpr std::string_view GetTypeName() { return "bool"; }
  static constexpr size_t GetSize() { return 1; }
  static constexpr std::string_view GetSchema() { return "bool value"; }
  static bool Unpack(std::span<const uint8_t> data) { return data[0]; }
  static void Pack(std::span<uint8_t> data, bool value) {
    data[0] = static_cast<char>(value ? 1 : 0);
  }
};

/**
 * Raw struct support for uint8_t values.
 * Primarily useful for higher level struct implementations.
 */
template <>
struct Struct<uint8_t> {
  static constexpr std::string_view GetTypeName() { return "uint8"; }
  static constexpr size_t GetSize() { return 1; }
  static constexpr std::string_view GetSchema() { return "uint8 value"; }
  static uint8_t Unpack(std::span<const uint8_t> data) { return data[0]; }
  static void Pack(std::span<uint8_t> data, uint8_t value) { data[0] = value; }
};

/**
 * Raw struct support for int8_t values.
 * Primarily useful for higher level struct implementations.
 */
template <>
struct Struct<int8_t> {
  static constexpr std::string_view GetTypeName() { return "int8"; }
  static constexpr size_t GetSize() { return 1; }
  static constexpr std::string_view GetSchema() { return "int8 value"; }
  static int8_t Unpack(std::span<const uint8_t> data) { return data[0]; }
  static void Pack(std::span<uint8_t> data, int8_t value) { data[0] = value; }
};

/**
 * Raw struct support for uint16_t values.
 * Primarily useful for higher level struct implementations.
 */
template <>
struct Struct<uint16_t> {
  static constexpr std::string_view GetTypeName() { return "uint16"; }
  static constexpr size_t GetSize() { return 2; }
  static constexpr std::string_view GetSchema() { return "uint16 value"; }
  static uint16_t Unpack(std::span<const uint8_t> data) {
    return support::endian::read16le(data.data());
  }
  static void Pack(std::span<uint8_t> data, uint16_t value) {
    support::endian::write16le(data.data(), value);
  }
};

/**
 * Raw struct support for int16_t values.
 * Primarily useful for higher level struct implementations.
 */
template <>
struct Struct<int16_t> {
  static constexpr std::string_view GetTypeName() { return "int16"; }
  static constexpr size_t GetSize() { return 2; }
  static constexpr std::string_view GetSchema() { return "int16 value"; }
  static int16_t Unpack(std::span<const uint8_t> data) {
    return support::endian::read16le(data.data());
  }
  static void Pack(std::span<uint8_t> data, int16_t value) {
    support::endian::write16le(data.data(), value);
  }
};

/**
 * Raw struct support for uint32_t values.
 * Primarily useful for higher level struct implementations.
 */
template <>
struct Struct<uint32_t> {
  static constexpr std::string_view GetTypeName() { return "uint32"; }
  static constexpr size_t GetSize() { return 4; }
  static constexpr std::string_view GetSchema() { return "uint32 value"; }
  static uint32_t Unpack(std::span<const uint8_t> data) {
    return support::endian::read32le(data.data());
  }
  static void Pack(std::span<uint8_t> data, uint32_t value) {
    support::endian::write32le(data.data(), value);
  }
};

/**
 * Raw struct support for int32_t values.
 * Primarily useful for higher level struct implementations.
 */
template <>
struct Struct<int32_t> {
  static constexpr std::string_view GetTypeName() { return "int32"; }
  static constexpr size_t GetSize() { return 4; }
  static constexpr std::string_view GetSchema() { return "int32 value"; }
  static int32_t Unpack(std::span<const uint8_t> data) {
    return support::endian::read32le(data.data());
  }
  static void Pack(std::span<uint8_t> data, int32_t value) {
    support::endian::write32le(data.data(), value);
  }
};

/**
 * Raw struct support for uint64_t values.
 * Primarily useful for higher level struct implementations.
 */
template <>
struct Struct<uint64_t> {
  static constexpr std::string_view GetTypeName() { return "uint64"; }
  static constexpr size_t GetSize() { return 8; }
  static constexpr std::string_view GetSchema() { return "uint64 value"; }
  static uint64_t Unpack(std::span<const uint8_t> data) {
    return support::endian::read64le(data.data());
  }
  static void Pack(std::span<uint8_t> data, uint64_t value) {
    support::endian::write64le(data.data(), value);
  }
};

/**
 * Raw struct support for int64_t values.
 * Primarily useful for higher level struct implementations.
 */
template <>
struct Struct<int64_t> {
  static constexpr std::string_view GetTypeName() { return "int64"; }
  static constexpr size_t GetSize() { return 8; }
  static constexpr std::string_view GetSchema() { return "int64 value"; }
  static int64_t Unpack(std::span<const uint8_t> data) {
    return support::endian::read64le(data.data());
  }
  static void Pack(std::span<uint8_t> data, int64_t value) {
    support::endian::write64le(data.data(), value);
  }
};

/**
 * Raw struct support for float values.
 * Primarily useful for higher level struct implementations.
 */
template <>
struct Struct<float> {
  static constexpr std::string_view GetTypeName() { return "float"; }
  static constexpr size_t GetSize() { return 4; }
  static constexpr std::string_view GetSchema() { return "float value"; }
  static float Unpack(std::span<const uint8_t> data) {
    return bit_cast<float>(support::endian::read32le(data.data()));
  }
  static void Pack(std::span<uint8_t> data, float value) {
    support::endian::write32le(data.data(), bit_cast<uint32_t>(value));
  }
};

/**
 * Raw struct support for double values.
 * Primarily useful for higher level struct implementations.
 */
template <>
struct Struct<double> {
  static constexpr std::string_view GetTypeName() { return "double"; }
  static constexpr size_t GetSize() { return 8; }
  static constexpr std::string_view GetSchema() { return "double value"; }
  static double Unpack(std::span<const uint8_t> data) {
    return bit_cast<double>(support::endian::read64le(data.data()));
  }
  static void Pack(std::span<uint8_t> data, double value) {
    support::endian::write64le(data.data(), bit_cast<uint64_t>(value));
  }
};

}  // namespace wpi