Skip to content

Commit

Permalink
Tune cf, dispatch and blocks features and api
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Dec 16, 2024
1 parent 9f22268 commit 8227b56
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cidre/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ ci = ["cf", "ns"]
cg = ["cf"] # optional io, dispatch, blocks
iio = ["cg", "blocks"]
objc = ["dep:cidre-macros"]
ns = ["objc"]
ns = ["objc", "cg"]
nl = ["ns"]
vt = ["cf", "cv", "cg", "cm"]
io = ["cf"]
Expand Down
14 changes: 14 additions & 0 deletions cidre/src/cf/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ impl Type {
}
}

impl std::cmp::PartialEq for Type {
fn eq(&self, other: &Self) -> bool {
self.equal(other)
}
}

impl std::cmp::Eq for Type {}

impl std::hash::Hash for Type {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
state.write_usize(self.hash());
}
}

impl Debug for Type {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let desc = self.desc();
Expand Down
23 changes: 23 additions & 0 deletions cidre/src/cf/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,18 @@ where
#[repr(transparent)]
pub struct DictionaryOfMut<K, V>(DictionaryMut, marker::PhantomData<(K, V)>);

impl<K, V> std::ops::Deref for DictionaryOfMut<K, V>
where
K: arc::Retain,
V: arc::Retain,
{
type Target = DictionaryOf<K, V>;

fn deref(&self) -> &Self::Target {
unsafe { transmute(self) }
}
}

impl<K, V> DictionaryOfMut<K, V>
where
K: arc::Retain,
Expand Down Expand Up @@ -581,6 +593,10 @@ where
transmute(dict)
}
}

pub fn copy_mut(&self) -> Option<arc::R<DictionaryOfMut<K, V>>> {
unsafe { transmute(CFDictionaryCreateMutableCopy(None, 0, &self.0)) }
}
}

impl<K, V> arc::Release for DictionaryOfMut<K, V>
Expand Down Expand Up @@ -614,6 +630,12 @@ extern "C-unwind" {
value_callbacks: Option<&ValueCbs>,
) -> Option<arc::R<DictionaryMut>>;

fn CFDictionaryCreateMutableCopy(
allocator: Option<&cf::Allocator>,
capacity: cf::Index,
the_dict: &Dictionary,
) -> Option<arc::R<DictionaryMut>>;

fn CFDictionaryAddValue(the_dict: &mut DictionaryMut, key: *const c_void, value: *const c_void);
fn CFDictionarySetValue(the_dict: &mut DictionaryMut, key: *const c_void, value: *const c_void);
fn CFDictionaryReplaceValue(
Expand All @@ -623,4 +645,5 @@ extern "C-unwind" {
);
fn CFDictionaryRemoveValue(the_dict: &mut DictionaryMut, key: *const c_void);
fn CFDictionaryRemoveAllValues(the_dict: &mut DictionaryMut);

}
30 changes: 29 additions & 1 deletion cidre/src/cf/string.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use core::fmt;
use std::{
borrow::Cow,
borrow::{Borrow, Cow},
ffi::{c_char, CStr},
hash::Hash,
str::from_utf8_unchecked,
};

Expand Down Expand Up @@ -529,6 +530,33 @@ impl AsRef<cf::String> for cf::StringMut {
}
}

impl Borrow<cf::Type> for &cf::String {
fn borrow(&self) -> &cf::Type {
self.as_type_ref()
}
}

impl Borrow<cf::Type> for cf::String {
fn borrow(&self) -> &cf::Type {
self.as_type_ref()
}
}

impl std::cmp::PartialEq for cf::String {
fn eq(&self, other: &Self) -> bool {
self.as_type_ref().eq(other)
}
}

impl std::cmp::Eq for cf::String {}

impl std::hash::Hash for cf::String {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let hash = self.as_type_ref().hash();
state.write_usize(hash)
}
}

#[cfg(test)]
mod tests {

Expand Down
2 changes: 2 additions & 0 deletions cidre/src/cg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub use font::Glyph;
pub use font::Index as FontIndex;

mod path;

#[cfg(feature = "blocks")]
pub use path::ApplyBlock as PathApplyBlock;
pub use path::Element as PathElement;
pub use path::ElementType as PathElementType;
Expand Down
1 change: 1 addition & 0 deletions cidre/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub use queue::QosClass;
pub use queue::Queue;

pub mod data;
#[cfg(feature = "blocks")]
pub use data::Applier as DataApplier;
pub use data::Data;

Expand Down
9 changes: 9 additions & 0 deletions cidre/src/dispatch/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{arc, define_obj_type, dispatch, ns};
use crate::blocks;

#[doc(alias = "dispatch_data_applier_t")]
#[cfg(feature = "blocks")]
pub type Applier<Attr> = blocks::Block<fn(&dispatch::Data, usize, *const u8, usize) -> bool, Attr>;

define_obj_type!(
Expand Down Expand Up @@ -74,17 +75,20 @@ impl Data {
}

#[doc(alias = "dispatch_data_create")]
#[cfg(feature = "blocks")]
#[inline]
pub fn copy_from_slice(data: &[u8]) -> arc::R<Self> {
unsafe { dispatch_data_create(data.as_ptr(), data.len(), None, None) }
}

#[cfg(feature = "blocks")]
#[inline]
pub fn from_static(bytes: &'static [u8]) -> arc::R<Self> {
let mut b = blocks::StaticBlock::new0(destructor_noop);
unsafe { dispatch_data_create(bytes.as_ptr(), bytes.len(), None, Some(b.as_esc_mut())) }
}

#[cfg(feature = "blocks")]
#[inline]
pub fn with_bytes_no_copy(
bytes: *const u8,
Expand All @@ -107,18 +111,21 @@ impl Data {
}
extern "C" fn destructor_noop(_ctx: *const c_void) {}

#[cfg(feature = "blocks")]
impl From<&'static [u8]> for arc::R<Data> {
fn from(slice: &'static [u8]) -> arc::R<Data> {
Data::from_static(slice)
}
}

#[cfg(feature = "blocks")]
impl From<&'static str> for arc::R<Data> {
fn from(slice: &'static str) -> arc::R<Data> {
Data::from_static(slice.as_bytes())
}
}

#[cfg(feature = "blocks")]
impl From<Vec<u8>> for arc::R<Data> {
fn from(val: Vec<u8>) -> arc::R<Data> {
let len = val.len();
Expand All @@ -135,6 +142,7 @@ impl From<Vec<u8>> for arc::R<Data> {
}
}

#[cfg(feature = "blocks")]
impl From<Box<[u8]>> for arc::R<Data> {
fn from(val: Box<[u8]>) -> arc::R<Data> {
let len = val.len();
Expand All @@ -155,6 +163,7 @@ impl From<Box<[u8]>> for arc::R<Data> {
extern "C" {
static _dispatch_data_empty: Data;

#[cfg(feature = "blocks")]
fn dispatch_data_create(
buffer: *const u8,
size: usize,
Expand Down
1 change: 1 addition & 0 deletions cidre/src/dispatch/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ extern "C" {

#[cfg(feature = "blocks")]
fn dispatch_sync(queue: &Queue, block: &mut dispatch::Block<blocks::NoEsc>);
#[cfg(feature = "blocks")]
fn dispatch_async(queue: &Queue, block: &mut dispatch::Block);

fn dispatch_async_f(queue: &Queue, context: *mut c_void, work: dispatch::Fn<c_void>);
Expand Down
6 changes: 5 additions & 1 deletion cidre/src/ns/data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::ptr::{slice_from_raw_parts, slice_from_raw_parts_mut};

use crate::{arc, blocks, cf, define_obj_type, define_opts, ns, objc};
use crate::{arc, cf, define_obj_type, define_opts, ns, objc};

#[cfg(feature = "blocks")]
use crate::blocks;

define_opts!(
#[doc(alias = "NSDataReadingOptions")]
Expand Down Expand Up @@ -164,6 +167,7 @@ impl Data {
}

/// NSExtendedData
#[cfg(feature = "blocks")]
impl Data {
#[objc::msg_send(enumerateByteRangesUsingBlock:)]
pub fn enumerate_byte_ranges_using_block(
Expand Down
6 changes: 5 additions & 1 deletion cidre/src/ns/file_manager.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{arc, blocks, define_cls, define_obj_type, define_opts, ns, objc, os};
use crate::{arc, define_cls, define_obj_type, define_opts, ns, objc, os};

#[cfg(feature = "blocks")]
use crate::blocks;

define_obj_type!(
#[doc(alias = "NSFileAttributeKey")]
Expand Down Expand Up @@ -294,6 +297,7 @@ impl FileManager {
options: ns::VolumeEnumOpts,
) -> arc::R<ns::Array<ns::Url>>;

#[cfg(feature = "blocks")]
#[objc::msg_send(unmountVolumeAtURL:options:completionHandler:)]
pub fn unmount_volume_at_url_ch_block(
&self,
Expand Down
7 changes: 6 additions & 1 deletion cidre/src/ns/operation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{arc, define_cls, define_obj_type, dispatch, ns, objc};
use crate::{arc, define_cls, define_obj_type, ns, objc};

#[cfg(feature = "dispatch")]
use crate::dispatch;

#[cfg(feature = "blocks")]
use crate::blocks;
Expand Down Expand Up @@ -145,9 +148,11 @@ impl OpQueue {
#[objc::msg_send(cancelAllOperations)]
pub fn cancel_all_ops(&mut self);

#[cfg(feature = "dispatch")]
#[objc::msg_send(underlyingQueue)]
pub fn underlying_queue(&self) -> Option<arc::R<dispatch::Queue>>;

#[cfg(feature = "dispatch")]
#[objc::msg_send(setUnderlyingQueue:)]
pub fn set_underlying_queue(&mut self, val: Option<&dispatch::Queue>);

Expand Down

0 comments on commit 8227b56

Please sign in to comment.