From 6cdf3d8d220e791ac60b64e1bfc67033ef156c1d Mon Sep 17 00:00:00 2001 From: "A. Molzer" <5550310+197g@users.noreply.github.com> Date: Tue, 4 Nov 2025 23:17:05 +0100 Subject: [PATCH] Naming pass for byte order conversion --- src/decoder/ifd.rs | 4 ++-- src/decoder/mod.rs | 10 +++++----- src/tags.rs | 28 ++++++++++++++++------------ 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/decoder/ifd.rs b/src/decoder/ifd.rs index 6e336a2..46547b9 100644 --- a/src/decoder/ifd.rs +++ b/src/decoder/ifd.rs @@ -766,7 +766,7 @@ impl Entry { let buf_unit = usize::from(type_.byte_len()); let mul_of_ty = buffer.len() / buf_unit * buf_unit; - let cls = type_.byte_order_class(); + let cls = type_.endian_bytes(); let native = ByteOrder::native(); while total_bytes > 0 { @@ -777,7 +777,7 @@ impl Entry { let buffer = &mut buffer[..now as usize]; reader.inner().read_exact(buffer)?; - reader.byte_order.convert_class(cls, buffer, native); + reader.byte_order.convert_endian_bytes(cls, buffer, native); collect(buffer); } diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index bad9a39..70df36c 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -538,13 +538,13 @@ fn fix_endianness(buf: &mut [u8], byte_order: ByteOrder, bit_depth: u8) { let host = ByteOrder::native(); let class = match bit_depth { - 0..=8 => crate::tags::ByteOrderClass::One, - 9..=16 => crate::tags::ByteOrderClass::Two, - 17..=32 => crate::tags::ByteOrderClass::Four, - _ => crate::tags::ByteOrderClass::Eight, + 0..=8 => crate::tags::EndianBytes::One, + 9..=16 => crate::tags::EndianBytes::Two, + 17..=32 => crate::tags::EndianBytes::Four, + _ => crate::tags::EndianBytes::Eight, }; - host.convert_class(class, buf, byte_order); + host.convert_endian_bytes(class, buf, byte_order); } impl Decoder { diff --git a/src/tags.rs b/src/tags.rs index 0c62b85..2cc1344 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -217,17 +217,17 @@ impl Type { } } - pub(crate) fn byte_order_class(self) -> ByteOrderClass { + pub(crate) fn endian_bytes(self) -> EndianBytes { match self { - Type::BYTE | Type::SBYTE | Type::ASCII | Type::UNDEFINED => ByteOrderClass::One, - Type::SHORT | Type::SSHORT => ByteOrderClass::Two, + Type::BYTE | Type::SBYTE | Type::ASCII | Type::UNDEFINED => EndianBytes::One, + Type::SHORT | Type::SSHORT => EndianBytes::Two, Type::LONG | Type::SLONG | Type::FLOAT | Type::IFD | Type::RATIONAL - | Type::SRATIONAL => ByteOrderClass::Four, - Type::LONG8 | Type::SLONG8 | Type::DOUBLE | Type::IFD8 => ByteOrderClass::Eight, + | Type::SRATIONAL => EndianBytes::Four, + Type::LONG8 | Type::SLONG8 | Type::DOUBLE | Type::IFD8 => EndianBytes::Eight, } } } @@ -338,33 +338,37 @@ impl ByteOrder { } /// Given a typed buffer, convert its contents to the specified byte order in-place. + /// + /// The buffer is assumed to represent an array of the given type. If the length of the buffer + /// is not divisible into an integer number of values, the behavior for the remaining bytes it + /// not specified. pub fn convert(self, ty: Type, buffer: &mut [u8], to: ByteOrder) { - self.convert_class(ty.byte_order_class(), buffer, to) + self.convert_endian_bytes(ty.endian_bytes(), buffer, to) } - pub(crate) fn convert_class(self, cls: ByteOrderClass, buffer: &mut [u8], to: ByteOrder) { + pub(crate) fn convert_endian_bytes(self, cls: EndianBytes, buffer: &mut [u8], to: ByteOrder) { if self == to { return; } // FIXME: at MSRV 1.89 or higher use `slice::as_chunks_mut`. match cls { - ByteOrderClass::One => { + EndianBytes::One => { // No change needed } - ByteOrderClass::Two => { + EndianBytes::Two => { for chunk in buffer.chunks_exact_mut(2) { let chunk: &mut [u8; 2] = chunk.try_into().unwrap(); *chunk = u16::from_be_bytes(*chunk).to_le_bytes(); } } - ByteOrderClass::Four => { + EndianBytes::Four => { for chunk in buffer.chunks_exact_mut(4) { let chunk: &mut [u8; 4] = chunk.try_into().unwrap(); *chunk = u32::from_be_bytes(*chunk).to_le_bytes(); } } - ByteOrderClass::Eight => { + EndianBytes::Eight => { for chunk in buffer.chunks_exact_mut(8) { let chunk: &mut [u8; 8] = chunk.try_into().unwrap(); *chunk = u64::from_be_bytes(*chunk).to_le_bytes(); @@ -376,7 +380,7 @@ impl ByteOrder { /// The size of individual byte-order corrected elements. #[derive(Clone, Copy)] -pub(crate) enum ByteOrderClass { +pub(crate) enum EndianBytes { One, Two, Four,