Crate safe_transmute [] [src]

This crate contains checked implementations of transmute().

The functions in this crate are not inherently safe, but just guarded against common simple mistakes (like trying to create an 8-byte type from 7 bytes).

Those functions are exactly as safe as the data passed to them - creating a null pointer, for example, is not unsafe in and of itself, but dereferencing it certainly is, but they don't do that (see here for extended discussion).

Examples

View bytes as a series of u16s:

assert_eq!(guarded_transmute_many::<u16>(&[0x00, 0x01,
                                           0x12, 0x34,
                                           // Spare byte, unused
                                           0x00]).unwrap(),
           &[0x0100, 0x3412]);

View all bytes as a series of u16s:

assert_eq!(guarded_transmute_many_pedantic::<u16>(&[0x00, 0x01,
                                                    0x12, 0x34]).unwrap(),
           &[0x0100, 0x3412]);

View bytes as an f64:

assert_eq!(guarded_transmute::<f64>(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]).unwrap(),
           0.0);

Structs

Error

A transmutation error.

Enums

ErrorReason

How the type's size compares to the received byte count and the transmutation function's characteristic.

Functions

guarded_transmute

Transmute a byte slice into a single instance of a Copyable type

guarded_transmute_many

View a byte slice as a slice of an arbitrary type.

guarded_transmute_many_pedantic

View a byte slice as a slice of an arbitrary type.

guarded_transmute_many_permissive

View a byte slice as a slice of an arbitrary type.

guarded_transmute_pedantic

Transmute a byte slice into a single instance of a Copyable type