#transmute #bytes #casting

no-std bytemuck

A crate for mucking around with piles of bytes

11 stable releases

1.7.3 Dec 16, 2021
1.7.2 Jul 23, 2021
1.7.0 Jun 13, 2021
1.5.1 Feb 18, 2021
0.1.2 Nov 11, 2019

#25 in Encoding

Download history 90540/week @ 2021-09-27 116705/week @ 2021-10-04 106861/week @ 2021-10-11 100929/week @ 2021-10-18 94800/week @ 2021-10-25 91810/week @ 2021-11-01 93768/week @ 2021-11-08 91599/week @ 2021-11-15 88431/week @ 2021-11-22 97148/week @ 2021-11-29 94983/week @ 2021-12-06 99616/week @ 2021-12-13 73550/week @ 2021-12-20 71598/week @ 2021-12-27 99746/week @ 2022-01-03 112210/week @ 2022-01-10

366,684 downloads per month
Used in 1,783 crates (228 directly)

Zlib OR Apache-2.0 OR MIT

58KB
757 lines

License:Zlib Minimum Rust Version crates.io docs.rs

bytemuck

A crate for mucking around with piles of bytes.

Stability

The goal is to stay at 1.y.z until at least the next edition of Rust.

I consider any increase of the Minimum Rust Version to be a semver breaking change, so rustc-1.34 will continue to be supported for at least the rest of the bytemuck-1.y.z series of the crate.

(The secret goal is to get all of this functionality into the standard library some day so that we don't even need to import a crate to do all this fun stuff.)


lib.rs:

This crate gives small utilities for casting between plain data types.

Basics

Data comes in five basic forms in Rust, so we have five basic casting functions:

  • T uses [cast]
  • &T uses [cast_ref]
  • &mut T uses [cast_mut]
  • &[T] uses [cast_slice]
  • &mut [T] uses [cast_slice_mut]

Some casts will never fail (eg: cast::<u32, f32> always works), other casts might fail (eg: cast_ref::<[u8; 4], u32> will fail if the reference isn't already aligned to 4). Each casting function has a "try" version which will return a Result, and the "normal" version which will simply panic on invalid input.

Using Your Own Types

All the functions here are guarded by the [Pod] trait, which is a sub-trait of the [Zeroable] trait.

If you're very sure that your type is eligible, you can implement those traits for your type and then they'll have full casting support. However, these traits are unsafe, and you should carefully read the requirements before adding the them to your own types.

Features

  • This crate is core only by default, but if you're using Rust 1.36 or later you can enable the extern_crate_alloc cargo feature for some additional methods related to Box and Vec. Note that the docs.rs documentation is always built with extern_crate_alloc cargo feature enabled.

Dependencies

~105KB