Skip to content

Commit

Permalink
to/from_u64: explicitly convert to little-endian
Browse files Browse the repository at this point in the history
  • Loading branch information
SIGSTACKFAULT committed May 21, 2024
1 parent 335b710 commit 5bff326
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/hex/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ impl From<IVec2> for Hex {
impl Hex {
/// Unpack from a [`u64`].
/// [x][`Hex::x`] is read from the most signifigant 32 bits; [y][`Hex::y`]
/// is read from the least signifigant 32 bits. Intended to be used with
/// [`Hex::as_u64`].
/// is read from the least signifigant 32 bits. The fields are
/// little-endian. Intended to be used with [`Hex::as_u64`].
///
/// # Example
///
Expand All @@ -74,16 +74,16 @@ impl Hex {
#[must_use]
#[doc(alias = "unpack")]
pub const fn from_u64(value: u64) -> Self {
let x = (value >> 32) as i32;
let y = (value & 0xFFFF_FFFF) as i32;
let x = i32::from_le((value >> 32) as i32);
let y = i32::from_le((value & 0xFFFF_FFFF) as i32);
Self::new(x, y)
}

/// Pack into a [`u64`].
/// [x][`Hex::x`] is placed in the most signifigant 32 bits; [y][`Hex::y`]
/// is placed in the least signifigant 32 bits. Can be used as a sort
/// key, or for saving in a binary format. Intended to be used with
/// [`Hex::from_u64`].
/// key, or for saving in a binary format. The fields are little-endian.
/// Intended to be used with [`Hex::from_u64`].
///
/// # Example
///
Expand All @@ -97,8 +97,8 @@ impl Hex {
#[allow(clippy::cast_sign_loss)]
#[doc(alias = "pack")]
pub const fn as_u64(self) -> u64 {
let high = (self.x as u32 as u64) << 32;
let low = self.y as u32 as u64;
let high = (self.x.to_le() as u32 as u64) << 32;
let low = self.y.to_le() as u32 as u64;
high | low
}
}

0 comments on commit 5bff326

Please sign in to comment.