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
// Copyright (c) 2014 by SiegeLord
//
// All rights reserved. Distributed under ZLib. For full terms see the file LICENSE.

extern crate libc;

use libc::c_char;
use std::ffi::CStr;

#[allow(non_camel_case_types)]
pub type c_bool = u8;

pub trait Flag
{
	fn zero() -> Self;
}

pub unsafe fn from_c_str(c_str: *const c_char) -> String
{
	String::from_utf8_lossy(CStr::from_ptr(c_str as *const _).to_bytes()).into_owned()
}

#[macro_export]
macro_rules! if_ok {
	($e: expr) => {
		if ($e).is_err()
		{
			return Err(());
		}
	};
}

#[macro_export]
macro_rules! opaque {
	($f: ident) => {
		/* Mimicking c_void */
		#[allow(missing_copy_implementations)]
		pub enum $f {}
	};
}

#[macro_export]
macro_rules! derive_copy_clone {
	($t: ty) => {
		impl Copy for $t {}
		impl Clone for $t
		{
			fn clone(&self) -> Self
			{
				*self
			}
		}
	};
}

#[macro_export]
macro_rules! flag_type
{
	($f: ident { $($n: ident = $v: expr),*}) =>
	{
		#[derive(Copy, Clone, Debug)]
		pub struct $f
		{
			bits: u32
		}

		impl $f
		{
			#[inline]
			pub fn get(&self) -> u32
			{
				self.bits
			}
		}

		impl Flag for $f
		{
			fn zero() -> $f
			{
				$f{bits: 0}
			}
		}

		impl ::std::ops::BitOr for $f
		{
			type Output = $f;
			fn bitor(self, e: $f) -> $f
			{
				$f{bits: self.bits | e.bits}
			}
		}

		impl ::std::ops::BitAnd for $f
		{
			type Output = bool;
			fn bitand(self, e: $f) -> bool
			{
				self.bits & e.bits != 0
			}
		}

		$(
			pub const $n: $f = $f{bits: $v};
		)+
	}
}

#[macro_export]
macro_rules! flags
{
	($f: ident { $($n: ident = $v: expr),*}) =>
	{
		$(
			pub const $n: $f = $f{bits: $v};
		)+
	}
}

#[macro_export]
macro_rules! cast_to_c {
	($p:ident, f32) => {
		$p as c_float
	};
	($p:ident, Color) => {
		*$p
	};
}