Skip to content

Commit 9600773

Browse files
committed
WIP on trivial transmute / same repr
1 parent 23efec0 commit 9600773

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

src/impl_expand.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,89 @@
99
use crate::imp_prelude::*;
1010

1111
use crate::data_traits::RawDataSubst;
12+
use crate::math_cell::MathCell;
1213

14+
use std::cell::Cell;
1315
use std::mem;
16+
use std::num::Wrapping;
17+
use std::num;
18+
use std::ptr::NonNull;
1419

1520
use num_complex::Complex;
1621

1722

23+
pub unsafe trait EqualRepresentation<To> { }
24+
25+
unsafe impl<T> EqualRepresentation<T> for T { }
26+
27+
macro_rules! unsafe_impl_trivial_transmute_t {
28+
($($from:ty => $to:ty,)+) => {
29+
$(
30+
unsafe impl<T> EqualRepresentation<$to> for $from { }
31+
)+
32+
33+
}
34+
}
35+
36+
macro_rules! unsafe_impl_trivial_transmute_bidir {
37+
($(($from:ty) <=> $to:ty,)+) => {
38+
$(
39+
unsafe impl EqualRepresentation<$to> for $from { }
40+
unsafe impl EqualRepresentation<$from> for $to { }
41+
)+
42+
43+
}
44+
}
45+
46+
macro_rules! unsafe_impl_trivial_transmute {
47+
($($from:ty => $to:ty,)+) => {
48+
$(
49+
unsafe impl EqualRepresentation<$to> for $from { }
50+
)+
51+
52+
}
53+
}
54+
55+
unsafe_impl_trivial_transmute_t! {
56+
T => [T; 1],
57+
[T; 1] => T,
58+
// transmute from cell to T, but not the other way around
59+
Cell<T> => T,
60+
MathCell<T> => T,
61+
*mut T => *const T,
62+
*const T => *mut T,
63+
NonNull<T> => *mut T,
64+
NonNull<T> => *const T,
65+
Wrapping<T> => T,
66+
T => Wrapping<T>,
67+
}
68+
69+
unsafe_impl_trivial_transmute_bidir! {
70+
(usize) <=> isize,
71+
(u128) <=> i128,
72+
(u64) <=> i64,
73+
(u32) <=> i32,
74+
(u16) <=> i16,
75+
(u8) <=> i8,
76+
}
77+
78+
unsafe_impl_trivial_transmute! {
79+
// only from nonzero, not to
80+
num::NonZeroU8 => u8,
81+
num::NonZeroI8 => i8,
82+
num::NonZeroU16 => u16,
83+
num::NonZeroI16 => i16,
84+
num::NonZeroU32 => u32,
85+
num::NonZeroI32 => i32,
86+
num::NonZeroU64 => u64,
87+
num::NonZeroI64 => i64,
88+
num::NonZeroU128 => u128,
89+
num::NonZeroI128 => i128,
90+
num::NonZeroUsize => usize,
91+
num::NonZeroIsize => isize,
92+
}
93+
94+
1895
pub unsafe trait MultiElement : MultiElementExtended<<Self as MultiElement>::Elem> {
1996
type Elem;
2097
}

0 commit comments

Comments
 (0)