Read a varint from the reader. Note: this function does not do zigzag decoding, for that see [`zag_i32`] and [`zag_i64`].
(reader: &mut R)
| 134 | pub(crate) fn zig_i64<W: Write>(n: i64, writer: W) -> AvroResult<usize> { |
| 135 | let zigzagged = ((n << 1) ^ (n >> 63)) as u64; |
| 136 | encode_variable(zigzagged, writer) |
| 137 | } |
| 138 | |
| 139 | /// Decode a zigzagged varint from the reader. |
| 140 | pub(crate) fn zag_i32<R: Read>(reader: &mut R) -> AvroResult<i32> { |
| 141 | let i = zag_i64(reader)?; |
| 142 | i32::try_from(i).map_err(|e| Details::ZagI32(e, i).into()) |
| 143 | } |
| 144 | |
| 145 | /// Decode a zigzagged varint from the reader. |
| 146 | pub(crate) fn zag_i64<R: Read>(reader: &mut R) -> AvroResult<i64> { |
| 147 | let z = decode_variable(reader)?; |
| 148 | Ok(if z & 0x1 == 0 { |
| 149 | (z >> 1) as i64 |
| 150 | } else { |
| 151 | !(z >> 1) as i64 |
| 152 | }) |
| 153 | } |
| 154 | |
| 155 | /// Write the number as a varint to the writer. |
| 156 | /// |
| 157 | /// Note: this function does not do zigzag encoding, for that see [`zig_i32`] and [`zig_i64`]. |
| 158 | fn encode_variable<W: Write>(mut zigzagged: u64, mut writer: W) -> AvroResult<usize> { |
| 159 | // Ensure the number is little endian for the varint encoding (no-op on LE systems) |
| 160 | zigzagged = zigzagged.to_le(); |
| 161 | // Encode the number as a varint |