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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use super::Value;
use crate::error::{JlrsError, JlrsResult};
use crate::traits::{Align, BitsUnion as BU, Cast, Flag};
use crate::{impl_julia_type, impl_julia_typecheck, impl_valid_layout};
use jl_sys::{jl_islayout_inline, jl_uniontype_t, jl_uniontype_type};
use std::marker::PhantomData;
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
#[repr(transparent)]
pub struct Union<'frame>(*mut jl_uniontype_t, PhantomData<&'frame ()>);
impl<'frame> Union<'frame> {
pub(crate) unsafe fn wrap(union: *mut jl_uniontype_t) -> Self {
Union(union, PhantomData)
}
#[doc(hidden)]
pub unsafe fn ptr(self) -> *mut jl_uniontype_t {
self.0
}
pub fn isbitsunion(self) -> bool {
unsafe {
let v: Value = self.into();
jl_islayout_inline(v.ptr(), &mut 0, &mut 0) != 0
}
}
pub fn isbits_size_align(self, size: &mut usize, align: &mut usize) -> bool {
unsafe {
let v: Value = self.into();
jl_islayout_inline(v.ptr(), size, align) != 0
}
}
pub fn size(self) -> usize {
let mut sz = 0;
if !self.isbits_size_align(&mut sz, &mut 0) {
return std::mem::size_of::<usize>();
}
sz
}
pub fn a(self) -> Value<'frame, 'static> {
unsafe { Value::wrap((&*self.ptr()).a) }
}
pub fn b(self) -> Value<'frame, 'static> {
unsafe { Value::wrap((&*self.ptr()).b) }
}
pub fn as_value(self) -> Value<'frame, 'static> {
self.into()
}
}
impl<'frame> Into<Value<'frame, 'static>> for Union<'frame> {
fn into(self) -> Value<'frame, 'static> {
unsafe { Value::wrap(self.ptr().cast()) }
}
}
unsafe impl<'frame, 'data> Cast<'frame, 'data> for Union<'frame> {
type Output = Self;
fn cast(value: Value<'frame, 'data>) -> JlrsResult<Self::Output> {
if value.is::<Self::Output>() {
return unsafe { Ok(Self::cast_unchecked(value)) };
}
Err(JlrsError::NotAUnion)?
}
unsafe fn cast_unchecked(value: Value<'frame, 'data>) -> Self::Output {
Self::wrap(value.ptr().cast())
}
}
impl_julia_typecheck!(Union<'frame>, jl_uniontype_type, 'frame);
impl_julia_type!(Union<'frame>, jl_uniontype_type, 'frame);
impl_valid_layout!(Union<'frame>, 'frame);
#[repr(C, align(1))]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Align1;
unsafe impl Align for Align1 {
const ALIGNMENT: usize = 1;
}
#[repr(C, align(2))]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Align2;
unsafe impl Align for Align2 {
const ALIGNMENT: usize = 2;
}
#[repr(C, align(4))]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Align4;
unsafe impl Align for Align4 {
const ALIGNMENT: usize = 4;
}
#[repr(C, align(8))]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Align8;
unsafe impl Align for Align8 {
const ALIGNMENT: usize = 8;
}
#[repr(C, align(16))]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Align16;
unsafe impl Align for Align16 {
const ALIGNMENT: usize = 16;
}
#[repr(transparent)]
#[derive(Copy, Clone, Debug)]
pub struct BitsUnion<T>(T);
unsafe impl<T> BU for BitsUnion<T> {}
pub unsafe fn correct_layout_for<A: Align, B: BU, F: Flag>(u: Union) -> bool {
let mut jl_sz = 0;
let mut jl_align = 0;
if !u.isbits_size_align(&mut jl_sz, &mut jl_align) {
return false;
}
A::ALIGNMENT == jl_align && std::mem::size_of::<B>() == jl_sz
}