-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.rs
More file actions
69 lines (59 loc) · 1.37 KB
/
Copy pathmain.rs
File metadata and controls
69 lines (59 loc) · 1.37 KB
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
#[derive(Copy, Clone)]
#[repr(C)]
struct TwoGroupsOfOneByte {
a: u8,
b: u8,
}
#[derive(Copy, Clone)]
#[repr(C)]
struct OneGroupOfTwoBytes {
c: u16,
}
#[derive(Copy, Clone)]
#[repr(C)]
struct FourBytes {
a: u8,
b: u8,
c: u8,
d: u8,
}
union Bytes {
group1: TwoGroupsOfOneByte,
group2: OneGroupOfTwoBytes,
}
union ScalarBytes {
bytes: FourBytes,
unsigned: u32,
signed: i32,
float: f32,
}
fn main() {
let bytes = Bytes {
group1: TwoGroupsOfOneByte { a: 0x01, b: 0x02 },
};
unsafe {
assert!(bytes.group1.a == 0x01);
assert!(bytes.group1.b == 0x02);
assert!(bytes.group2.c == 0x0201);
let word = ScalarBytes {
unsigned: 0x44332211,
};
assert!(word.bytes.a == 0x11);
assert!(word.bytes.b == 0x22);
assert!(word.bytes.c == 0x33);
assert!(word.bytes.d == 0x44);
assert!(word.signed == 0x44332211);
let negative = ScalarBytes { signed: -2 };
assert!(negative.unsigned == 0xffff_fffe);
let one = ScalarBytes { float: 1.0 };
assert!(one.unsigned == 0x3f80_0000);
let mut mutable = ScalarBytes { unsigned: 0 };
mutable.bytes = FourBytes {
a: 0xaa,
b: 0xbb,
c: 0xcc,
d: 0x7d,
};
assert!(mutable.unsigned == 0x7dcc_bbaa);
}
}