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]),
           &[0x0100, 0x3412]);

View bytes as an f64:

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

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.