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
//! Zigzag encoding for signed integers.
//!
//! Zigzag maps signed integers to unsigned ones so that small-magnitude negative
//! values produce small-magnitude varints. Pair these helpers with the
//! [`varint`][`crate::varint`] functions to encode signed values compactly.
//!
//! The mapping mirrors the one used by Protocol Buffers:
//!
//! | signed | unsigned |
//! | ------ | -------- |
//! | 0 | 0 |
//! | -1 | 1 |
//! | 1 | 2 |
//! | -2 | 3 |
//! | 2 | 4 |
/// Map a signed `i32` to its zigzag-encoded `u32`.
///
/// # Example
///
/// ```
/// use wire_codec::zigzag;
/// assert_eq!(zigzag::encode_i32(0), 0);
/// assert_eq!(zigzag::encode_i32(-1), 1);
/// assert_eq!(zigzag::encode_i32(1), 2);
/// assert_eq!(zigzag::encode_i32(i32::MIN), u32::MAX);
/// ```
pub const
/// Recover the signed `i32` from a zigzag-encoded `u32`.
///
/// # Example
///
/// ```
/// use wire_codec::zigzag;
/// assert_eq!(zigzag::decode_i32(0), 0);
/// assert_eq!(zigzag::decode_i32(1), -1);
/// assert_eq!(zigzag::decode_i32(u32::MAX), i32::MIN);
/// ```
pub const
/// Map a signed `i64` to its zigzag-encoded `u64`.
///
/// # Example
///
/// ```
/// use wire_codec::zigzag;
/// assert_eq!(zigzag::encode_i64(i64::MIN), u64::MAX);
/// ```
pub const
/// Recover the signed `i64` from a zigzag-encoded `u64`.
pub const