1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//! Basic cross-feature transmute support.

#![cfg_attr(feature = "no-core", feature(no_core))]

#![no_std]

#![deny(missing_docs)]

#[cfg(all(feature = "enable-std", feature = "no-core"))]
compile_error!("enable-std` with `#![no_core]` is not considered feasible.");

#[cfg(feature = "enable-std")]
extern crate std;

/// [Standard transmute](https://doc.rust-lang.org/std/mem/fn.transmute.html)
#[cfg(all(feature = "enable-std", not(feature = "rust-documentation")))]
#[macro_export]
macro_rules! wrapped_transmute {
    ($value:tt, $source_type:ty, $destination_type:ty) => {
        std::mem::transmute::<$source_type,$destination_type>($value)
    };
}

/// [Core transmute](https://doc.rust-lang.org/core/mem/fn.transmute.html)
#[cfg(all(not(feature = "enable-std"), not(feature = "no-core"), not(feature = "rust-documentation")))]
#[macro_export]
macro_rules! wrapped_transmute {
    ($value:tt, $source_type:ty, $destination_type:ty) => {
        core::mem::transmute::<$source_type,$destination_type>($value)
    };
}

/// Fallback transmute, created using the notion that sizes are byte
/// aligned and that [safety probably isn't high on your list] if you're 
/// transmuting at this level anyway -- you should know what you're doing,
/// and this is likely something you'll come back to if/when you're looking
/// at the code, so while this is *fairly* tested, be sure to come back!
/// 
/// This attempts to follow the same logic as the above as best it's able,
/// but since it's operating outside of standard intrinsics in most cases
/// it's going to be a bit of a mess.  I mean, that's the theory, but since
/// there's no memory checking size in no-core, well, the guard rails are
/// razor blades.
/// 
/// This is wildly, hilariously, unsafe.  Run.  Run screaming.
/// 
/// [safety probably isn't high on your list]: https://doc.rust-lang.org/nomicon/transmutes.html
#[cfg(all(feature = "no-core", not(feature = "rust-documentation")))]
#[macro_export]
macro_rules! wrapped_transmute {
    ($value:tt, $source_type:ty, $destination_type:ty) => {
        *((&$value as *const $source_type as *const $destination_type).as_ref().expect("failed conversion"))
    };
}

/// Wrapped transmute is a simple macro designed to take three arguments:
/// 
///   * `value`: the value to be transmuted;
///   * `source_type`: the type of the source (type of $value);
///   * `destination_type`: the type of the destination/output.
/// 
/// The implementation of the macro is determined by the features of the
/// crate, which are used to guard against langauge features (inclusion
/// or exclusion of std and core).
#[cfg(feature = "rust-documentation")]
#[macro_export]
macro_rules! wrapped_transmute {
    ($value:tt, $source_type:ty, $destination_type:ty) => {
        // This version exists solely for rust documentation generation.
    };
}

#[cfg(test)]
mod test {
    #[test]
    fn simple_test() {
        use super::wrapped_transmute;
        {
            let a: f32 = 1.0;
            let b: u32 = 0x3F800000;
            let c = unsafe { wrapped_transmute!(a, f32, u32) };
            assert_eq!(c, b);
        }
    }
}