use std::ffi::{c_void, CString};
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum DispatchQoS {
Background = 0,
Utility = 1,
#[default]
Default = 2,
UserInitiated = 3,
UserInteractive = 4,
}
pub struct DispatchQueue {
ptr: *const c_void,
}
unsafe impl Send for DispatchQueue {}
unsafe impl Sync for DispatchQueue {}
impl DispatchQueue {
#[must_use]
pub fn new(label: &str, qos: DispatchQoS) -> Self {
let c_label = CString::new(label).expect("Label contains null byte");
let ptr = unsafe { crate::ffi::dispatch_queue_create(c_label.as_ptr(), qos as i32) };
assert!(!ptr.is_null(), "Failed to create dispatch queue");
Self { ptr }
}
#[must_use]
pub const fn as_ptr(&self) -> *const c_void {
self.ptr
}
}
impl Clone for DispatchQueue {
fn clone(&self) -> Self {
unsafe {
Self {
ptr: crate::ffi::dispatch_queue_retain(self.ptr),
}
}
}
}
impl Drop for DispatchQueue {
fn drop(&mut self) {
unsafe {
crate::ffi::dispatch_queue_release(self.ptr);
}
}
}
impl fmt::Debug for DispatchQueue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DispatchQueue")
.field("ptr", &self.ptr)
.finish()
}
}
impl fmt::Display for DispatchQueue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "DispatchQueue")
}
}