Skip to content

Commit

Permalink
Make reverse_endianness public
Browse files Browse the repository at this point in the history
  • Loading branch information
LDeakin committed Mar 16, 2024
1 parent 5310b02 commit 549df33
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Implement `TryFrom<&str>` for `{Array,Group}Metadata`

### Changed
- Make `array::codec::array_to_bytes::bytes::Endianness::{is_native(),NATIVE_ENDIAN}` public
- Make `array::codec::array_to_bytes::bytes::{Endianness::is_native(),NATIVE_ENDIAN,reverse_endianness}` public

## [0.12.4] - 2024-03-09

Expand Down
9 changes: 5 additions & 4 deletions src/array/codec/array_to_bytes/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,27 @@ pub const NATIVE_ENDIAN: Endianness = if cfg!(target_endian = "big") {
Endianness::Little
};

fn reverse_endianness(v: &mut [u8], data_type: &DataType) {
/// Reverse the endianness of bytes for a given data type.
pub fn reverse_endianness(v: &mut [u8], data_type: &DataType) {
match data_type {
DataType::Bool | DataType::Int8 | DataType::UInt8 | DataType::RawBits(_) => {}
DataType::Int16 | DataType::UInt16 | DataType::Float16 | DataType::BFloat16 => {
let swap = |chunk: &mut [u8]| {
let bytes = u16::from_ne_bytes(chunk.try_into().unwrap());
let bytes = u16::from_ne_bytes(unsafe { chunk.try_into().unwrap_unchecked() });
chunk.copy_from_slice(bytes.swap_bytes().to_ne_bytes().as_slice());
};
v.chunks_exact_mut(2).for_each(swap);
}
DataType::Int32 | DataType::UInt32 | DataType::Float32 | DataType::Complex64 => {
let swap = |chunk: &mut [u8]| {
let bytes = u32::from_ne_bytes(chunk.try_into().unwrap());
let bytes = u32::from_ne_bytes(unsafe { chunk.try_into().unwrap_unchecked() });
chunk.copy_from_slice(bytes.swap_bytes().to_ne_bytes().as_slice());
};
v.chunks_exact_mut(4).for_each(swap);
}
DataType::Int64 | DataType::UInt64 | DataType::Float64 | DataType::Complex128 => {
let swap = |chunk: &mut [u8]| {
let bytes = u64::from_ne_bytes(chunk.try_into().unwrap());
let bytes = u64::from_ne_bytes(unsafe { chunk.try_into().unwrap_unchecked() });
chunk.copy_from_slice(bytes.swap_bytes().to_ne_bytes().as_slice());
};
v.chunks_exact_mut(8).for_each(swap);
Expand Down

0 comments on commit 549df33

Please sign in to comment.