1 | // x11-rs: Rust bindings for X11 libraries |
2 | // The X11 libraries are available under the MIT license. |
3 | // These bindings are public domain. |
4 | |
5 | use std::cmp::min; |
6 | use std::mem::{size_of, zeroed}; |
7 | |
8 | // |
9 | // public functions |
10 | // |
11 | |
12 | pub unsafe fn mem_eq<T: Sized>(a: &T, b: &T) -> bool { |
13 | let a_addr: usize = a as *const T as usize; |
14 | let b_addr: usize = b as *const T as usize; |
15 | |
16 | for i: usize in 0..size_of::<T>() { |
17 | if *((a_addr + i) as *const u8) != *((b_addr + i) as *const u8) { |
18 | return false; |
19 | } |
20 | } |
21 | |
22 | true |
23 | } |
24 | |
25 | pub unsafe fn transmute_union<I, O>(input: &I) -> O |
26 | where |
27 | I: Sized, |
28 | O: Sized, |
29 | { |
30 | let mut output: O = zeroed(); |
31 | let copy_len: usize = min(v1:size_of::<I>(), v2:size_of::<O>()); |
32 | |
33 | for i: usize in 0..copy_len { |
34 | *((&mut output as *mut O as usize + i) as *mut u8) = |
35 | *((input as *const I as usize + i) as *const u8); |
36 | } |
37 | |
38 | output |
39 | } |
40 | |