#no-std

no-std byte-slice-cast

Safely cast bytes slices from/to slices of built-in fundamental numeric types

11 releases (3 stable)

1.2.0 Oct 19, 2021
1.0.0 Oct 13, 2020
0.3.5 Dec 22, 2019
0.3.4 Nov 11, 2019
0.1.0 Aug 14, 2017

#134 in Rust patterns

Download history 28396/week @ 2021-08-13 30716/week @ 2021-08-20 32852/week @ 2021-08-27 32846/week @ 2021-09-03 38275/week @ 2021-09-10 39377/week @ 2021-09-17 46460/week @ 2021-09-24 43233/week @ 2021-10-01 48045/week @ 2021-10-08 51311/week @ 2021-10-15 37313/week @ 2021-10-22 39989/week @ 2021-10-29 43527/week @ 2021-11-05 46120/week @ 2021-11-12 46225/week @ 2021-11-19 46752/week @ 2021-11-26

190,384 downloads per month
Used in 619 crates (26 directly)

MIT license

33KB
716 lines

byte-slice-cast crates.io Actions Status docs.rs

Safely cast between byte slices and slices of another built-in fundamental number type.

LICENSE

byte-slice-cast is licensed under the MIT license (LICENSE or http://opensource.org/licenses/MIT).

Contribution

Any kinds of contributions are welcome as a pull request.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in byte-slice-cast by you shall be licensed under the MIT license as above, without any additional terms or conditions.


lib.rs:

Safely cast bytes slices from/to slices of built-in fundamental numeric types.

The provided traits here allow safe casting between byte slices and slices of fundamental numeric types, like integers and floating point numbers. During casting, checks are performed to ensure that the output slice is safe to use: the input slice must be properly aligned for the output type and contain an integer number of values.

Instead of working only on slices, the traits work on AsRef<[T]> in the immutable case and on AsMut<[T]> for the mutable case. As such, it is possible to directly work on e.g. Vec<T> and Box<[T]> too.

The content of the output slice will be bitwise equivalent to the input slice, as such extra care has to be taken with regard to endianness.

Example with slices

# extern crate byte_slice_cast;
# fn main() {
use byte_slice_cast::*;

let slice = [0x0102u16, 0x0304u16, 0x0506u16];

let converted_slice = slice.as_byte_slice();

if cfg!(target_endian = "big") {
    assert_eq!(converted_slice, &[1, 2, 3, 4, 5, 6]);
} else {
    assert_eq!(converted_slice, &[2, 1, 4, 3, 6, 5]);
}

let converted_back_slice = converted_slice.as_slice_of::<u16>().unwrap();

assert_eq!(converted_back_slice, &slice);
# }

Example with mutable slices

# extern crate byte_slice_cast;
# fn main() {
use byte_slice_cast::*;

let mut slice = [0u32; 1];
let mut converted_slice = slice.as_mut_byte_slice();
converted_slice.copy_from_slice(&[0x12, 0x34, 0x56, 0x78]);

let mut converted_slice = converted_slice.as_mut_slice_of::<u16>().unwrap();
converted_slice[0] = 0xffff;

if cfg!(target_endian = "big") {
    assert_eq!(&slice, &[0xffff5678]);
} else {
    assert_eq!(&slice, &[0x7856ffff]);
}

# }

No runtime deps

Features

  • std