[][src]Crate safe_transmute

This crate contains checked implementations of transmutation procedures, some of which ensure memory safety.

Crate outline

The following modules are available:

This crate can be used in a no-std environment by disabling the std feature through specifying default-features = false on import.

Examples

View bytes as a series of u16s, with a single-many boundary guard (at least one value, extraneous bytes are allowed):

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

View all bytes as a series of u16s:

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

View a byte slice as a single f64:

assert_eq!(safe_transmute_one::<f64>(
    &[0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x40])?,
    2.0);

View a series of u16s as bytes:

assert_eq!(safe_transmute_to_bytes(&[0x0001u16,
                                     0x1234u16]),
           &[0x01, 0x00, 0x34, 0x12].le_to_native::<u16>());

Re-exports

pub use self::guard::SingleValueGuard;
pub use self::guard::PermissiveGuard;
pub use self::guard::SingleManyGuard;
pub use self::guard::PedanticGuard;
pub use self::guard::Guard;
pub use self::error::ErrorReason;
pub use self::error::GuardError;
pub use self::error::Error;
pub use self::pod::PodTransmutable;
pub use self::to_bytes::safe_transmute_one_to_bytes;
pub use self::to_bytes::safe_transmute_to_bytes;
pub use self::to_bytes::safe_transmute_to_bytes_vec;
pub use self::bool::safe_transmute_bool_vec_permissive;
pub use self::bool::safe_transmute_bool_vec_pedantic;
pub use self::bool::safe_transmute_bool_permissive;
pub use self::bool::safe_transmute_bool_pedantic;
pub use self::to_bytes::guarded_transmute_to_bytes_pod_many;
pub use self::to_bytes::guarded_transmute_to_bytes_pod;
pub use self::to_bytes::guarded_transmute_to_bytes_pod_vec;
pub use self::bool::guarded_transmute_bool_permissive;
pub use self::bool::guarded_transmute_bool_pedantic;

Modules

align

Alignment checking primitives.

base

Primitives for object and array transmutation.

bool

Functions for safe transmutation to bool.

error

Detectable and recoverable-from transmutation precondition errors.

guard

The guard module exposes an API for memory boundary checking.

pod

Plain data object safe transmute

to_bytes

Functions for transmutation from a concrete type to bytes.

util

Module containing various utility functions.

Functions

safe_transmute_many

Transmute a byte slice into a sequence of values of the given type.

safe_transmute_many_pedantic

Transmute a byte slice into a sequence of values of the given type.

safe_transmute_many_permissive

Transmute a byte slice into a sequence of values of the given type.

safe_transmute_one

Transmute a byte slice into a single instance of a POD.

safe_transmute_one_pedantic

Transmute a byte slice into a single instance of a POD.

safe_transmute_vec

Transform a byte vector into a vector of values.

safe_transmute_vec_pedantic

Transform a byte vector into a vector of values.

safe_transmute_vec_permissive

Transform a byte vector into a vector of values.