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
// repurpose.rs
// Aldaron's Memory Interface ( ami )
// Copyright 2017 (c) Aldaron's Tech
// Copyright 2017 (c) Jeron Lau
// Licensed under the MIT LICENSE

//! This module is for repurposing memory allocated by rust.

#[cfg(debug_assertions)]
use size_of::*;

#[cfg(debug_assertions)]
#[inline(always)]
fn is_safe<T, U>() {
	let sizet = size_of::<T>();
	let sizeu = size_of::<U>();
	if sizet != sizeu {
		panic!("size of u ({}) does not match size of t ({}).", sizeu,
			sizet);
	}
}

#[cfg(not(debug_assertions))]
#[inline(always)]
fn is_safe<T, U>() { }

/// Repurpose memory of type `T` as memory of type `U`.  `input` is the a
/// reference to the memory to repurpose.  Returns the repurposed memory.
#[inline(always)]
pub fn repurpose<T, U>(input: &mut T) -> &mut U {
	is_safe::<T, U>();
	// Actual repurpose.
	unsafe { &mut *(input as *mut T as *mut U) }
}