Skip to content

Small fixes - #336

Merged
martin-g merged 5 commits into
mainfrom
small_fixes
Nov 13, 2025
Merged

martin-g merged 5 commits into
mainfrom
small_fixes

Conversation

@Kriskras99

Copy link
Copy Markdown
Contributor

These are some small things I ran into when working on async support.

… match

This makes it a compiler error if we forget to compare a type.

`rustfmt` has been disabled for this function as it does a poor job
of formatting this code.
@martin-g martin-g added this to the 0.22.0 milestone Nov 12, 2025
Comment thread avro/src/util.rs Outdated

fn encode_variable<W: Write>(mut z: u64, mut writer: W) -> AvroResult<usize> {
fn encode_variable<W: Write>(n: i64, mut writer: W) -> AvroResult<usize> {
let mut z = (((n << 1) ^ (n >> 63)) as u64).to_le();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to be complaint with a double is written as 8 bytes. The double is converted into a 64-bit integer using a method equivalent to Java’s [doubleToRawLongBits](https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#doubleToRawLongBits-double-) and then encoded in little-endian format. from the specification, right ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, encode_variable is purely used for zigzag encoding int and long types.
The bit twiddling is to do the zigzag, while the remainder of the function is for doing the varint encoding.
I've moved the bit twiddling back to zig_i64, and documented why they are two different functions

Comment thread avro/src/util.rs Resolved
Comment thread avro/src/schema_equality.rs Resolved
Comment thread avro/src/schema_equality.rs Resolved
Comment thread avro/src/schema_equality.rs Resolved
Changes suggested by `martin-g`

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR contains several small improvements and fixes encountered while working on async support, including documentation additions, code refactoring, and bug fixes in schema comparison logic.

  • Added documentation comments to varint encoding/decoding functions
  • Refactored schema equality comparison from macros to exhaustive match statements
  • Fixed bugs in schema comparison (Decimal inner schema, Array/Map recursion, Record field names)
  • Improved API flexibility with generic iterators
  • Fixed documentation comments for schema structs

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
avro/src/util.rs Added documentation for varint encoding functions and refactored variable naming. Contains critical bugs with endianness conversion.
avro/src/schema_equality.rs Refactored from macro-based to match-based comparison, fixing several comparison bugs (Decimal inner, Array/Map recursion, field names) and removing unreachable error logging.
avro/src/schema.rs Fixed copy-paste errors in documentation comments and improved function signature to use generic iterators.
avro/src/reader.rs Updated function call to work with new generic iterator signature.
avro/src/encode.rs Added documentation for the encode_bytes function.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread avro/src/util.rs
Comment on lines +112 to +113
// Ensure the number is little endian for the varint encoding (no-op on LE systems)
zigzagged = zigzagged.to_le();

Copilot AI Nov 13, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The addition of to_le() here is incorrect and will break varint encoding on big-endian systems.

Varint encoding is endian-independent - it encodes numbers by extracting 7-bit chunks from least to most significant, which works correctly regardless of the system's endianness. The original code was correct.

On a big-endian system, calling to_le() would byte-swap the value before encoding, producing incorrect output. On little-endian systems, it's a no-op but adds unnecessary operations.

Recommendation: Remove the to_le() call and the associated comment on line 112.

Suggested change
// Ensure the number is little endian for the varint encoding (no-op on LE systems)
zigzagged = zigzagged.to_le();

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct. Encoding a varint on BE and then decoding that on LE should give the same result (but the specification doesn't actually specify which endianness to use).

Comment thread avro/src/util.rs
}

Ok(i)
Ok(u64::from_le(i))

Copilot AI Nov 13, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The addition of from_le() here is incorrect and will break varint decoding on big-endian systems.

The varint decoding algorithm already correctly reconstructs the value by shifting and ORing 7-bit chunks, which is endian-independent. Calling from_le() assumes the result is in little-endian format, which it isn't - it's just the correctly reconstructed value.

On a big-endian system, this would byte-swap the correctly decoded value, producing incorrect results. On little-endian systems, it's a no-op but adds unnecessary operations.

Recommendation: Remove the from_le() call and keep it as Ok(i).

Suggested change
Ok(u64::from_le(i))
Ok(i)

Copilot uses AI. Check for mistakes.
@martin-g
martin-g merged commit 7509847 into main Nov 13, 2025
@Kriskras99
Kriskras99 deleted the small_fixes branch November 22, 2025 15:35
@Kriskras99 Kriskras99 mentioned this pull request Jun 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants