winit/platform_impl/linux/x11/util/
format.rs

1use std::{fmt::Debug, mem, os::raw::*};
2
3// This isn't actually the number of the bits in the format.
4// X11 does a match on this value to determine which type to call sizeof on.
5// Thus, we use 32 for c_long, since 32 maps to c_long which maps to 64.
6// ...if that sounds confusing, then you know why this enum is here.
7#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
8pub enum Format {
9    Char = 8,
10    Short = 16,
11    Long = 32,
12}
13
14impl Format {
15    pub fn from_format(format: usize) -> Option<Self> {
16        match format {
17            8 => Some(Format::Char),
18            16 => Some(Format::Short),
19            32 => Some(Format::Long),
20            _ => None,
21        }
22    }
23
24    pub fn get_actual_size(&self) -> usize {
25        match self {
26            &Format::Char => mem::size_of::<c_char>(),
27            &Format::Short => mem::size_of::<c_short>(),
28            &Format::Long => mem::size_of::<c_long>(),
29        }
30    }
31}
32
33pub trait Formattable: Debug + Clone + Copy + PartialEq + PartialOrd {
34    const FORMAT: Format;
35}
36
37// You might be surprised by the absence of c_int, but not as surprised as X11 would be by the presence of it.
38impl Formattable for c_schar {
39    const FORMAT: Format = Format::Char;
40}
41impl Formattable for c_uchar {
42    const FORMAT: Format = Format::Char;
43}
44impl Formattable for c_short {
45    const FORMAT: Format = Format::Short;
46}
47impl Formattable for c_ushort {
48    const FORMAT: Format = Format::Short;
49}
50impl Formattable for c_long {
51    const FORMAT: Format = Format::Long;
52}
53impl Formattable for c_ulong {
54    const FORMAT: Format = Format::Long;
55}