glium/
field.rs

1//! Field utils
2
3use std::marker::PhantomData;
4
5/// Represents the field of a struct
6///
7/// Created with the `field!` macro.
8pub struct Field<T> {
9    offs: usize,
10    size: usize,
11    phantom: PhantomData<T>,
12}
13
14impl<T> Field<T> {
15    /// Gets the offset of the field
16    pub const fn offs(&self) -> usize {
17        self.offs
18    }
19    /// Gets the size of the field
20    pub const fn size(&self) -> usize {
21        self.size
22    }
23}
24
25#[doc(hidden)]
26pub fn _hidden_field<T>(offs: usize, _: Option<&T>) -> Field<T> {
27    Field {
28        offs,
29        size: std::mem::size_of::<T>(),
30        phantom: PhantomData,
31    }
32}
33
34/// A macro to create a `Field`.
35#[macro_export]
36macro_rules! field {
37    ($struct_name:ident, $field_name:ident) => {{
38        let opt = None::<&$struct_name>.map(|v| &v.$field_name);
39        let offs = $crate::__glium_offset_of!($struct_name, $field_name);
40        $crate::field::_hidden_field(offs, opt)
41    }};
42}