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
use std::intrinsics::transmute;
use opencl_sys::{CL_QUEUED, CL_SUBMITTED, CL_RUNNING, CL_COMPLETE};
use crate::core::Error;

/// An event's status
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(i32)]
pub enum EventStatus {
    /// Command has been enqueued in the command-queue.
    Queued = CL_QUEUED,
    /// Enqueued command has been submitted by the host to the device associated with the command-queue.
    Submitted = CL_SUBMITTED,
    /// Device is currently executing this command.
    Running = CL_RUNNING,
    /// The command has completed.
    Complete = CL_COMPLETE
}

impl EventStatus {
    #[inline(always)]
    pub const fn has_completed (&self) -> bool {
        match self {
            Self::Complete => true,
            _ => false
        }
    }

    #[inline(always)]
    pub const fn is_running (&self) -> bool {
        match self {
            Self::Running => true,
            _ => false
        }
    }

    #[inline(always)]
    pub const fn is_submitted (&self) -> bool {
        match self {
            Self::Submitted => true,
            _ => false
        }
    }

    #[inline(always)]
    pub const fn is_queued (&self) -> bool {
        match self {
            Self::Queued => true,
            _ => false
        }
    }

    #[inline(always)]
    pub const fn has_started_running (&self) -> bool {
        (*self as i32) <= CL_RUNNING
    }

    #[inline(always)]
    pub const fn has_submitted (&self) -> bool {
        (*self as i32) <= CL_SUBMITTED
    }
}

impl TryFrom<i32> for EventStatus {
    type Error = Error;

    #[inline(always)]
    fn try_from(value: i32) -> Result<Self, Self::Error> {
        if value < 0 {
            return Err(Error::try_from(value).unwrap())
        }

        return unsafe { Ok(transmute(value)) }
    }
}

impl Into<i32> for EventStatus {
    #[inline(always)]
    fn into(self) -> i32 {
        self as i32
    }
}