dispatch2/
workloop.rs

1use alloc::ffi::CString;
2use core::{borrow::Borrow, ops::Deref};
3
4use crate::{DispatchQueue, DispatchRetained};
5
6dispatch_object!(
7    /// Dispatch workloop queue.
8    #[doc(alias = "dispatch_workloop_t")]
9    #[doc(alias = "dispatch_workloop_s")]
10    pub struct DispatchWorkloop;
11);
12
13dispatch_object_not_data!(unsafe DispatchWorkloop);
14
15impl DispatchWorkloop {
16    /// Create a new [`DispatchWorkloop`].
17    pub fn new(label: &str, inactive: bool) -> DispatchRetained<Self> {
18        let label = CString::new(label).expect("Invalid label!");
19
20        // Safety: label can only be valid.
21        unsafe {
22            if inactive {
23                DispatchWorkloop::__new_inactive(label.as_ptr())
24            } else {
25                DispatchWorkloop::__new(label.as_ptr())
26            }
27        }
28    }
29}
30
31impl Deref for DispatchWorkloop {
32    type Target = DispatchQueue;
33
34    /// Access the workloop as a [`DispatchQueue`].
35    #[inline]
36    fn deref(&self) -> &Self::Target {
37        let ptr: *const DispatchWorkloop = self;
38        let ptr: *const DispatchQueue = ptr.cast();
39        // SAFETY: Workloop queues are "subclasses" of queues (they can be
40        // used in all the same places that normal queues can).
41        unsafe { &*ptr }
42    }
43}
44
45impl AsRef<DispatchQueue> for DispatchWorkloop {
46    #[inline]
47    fn as_ref(&self) -> &DispatchQueue {
48        self
49    }
50}
51
52// PartialEq, Eq and Hash work the same for workloops and queues.
53impl Borrow<DispatchQueue> for DispatchWorkloop {
54    #[inline]
55    fn borrow(&self) -> &DispatchQueue {
56        self
57    }
58}