Skip to content

Conversation

@madsmtm
Copy link
Owner

@madsmtm madsmtm commented Apr 8, 2025

Refactor dispatch2 to use the DispatchRetained<T> pattern instead of types owning the retain count internally. This should allow us to use Dispatch types in the auto-generated headers. See #681 (comment) for why we don't use a more general approach to these *Retained types.

I've also decided to prefix types with "Dispatch", to better match Swift, and to allow easily glob-importing from dispatch2 without fear that the symbols will clash with something else. This isn't that "rusty", but it plays much nicer with the rest of the objc2 ecosystem, and matches what Swift does.

It's also clearer at the usage-site. Compare:

// Hmm, what queue are we referring to?
Queue::main().exec_sync(|| ...);

// Ah, it's a Dispatch queue.
DispatchQueue::main().exec_sync(|| ...);

See #681 for further discussion about this refactor.

Part of #77. Unblocks #710.

madsmtm added 9 commits April 8, 2025 22:47
Remove `is_activated` state and `TargetQueueError`, this kind of
information is best left for libdispatch to keep track of.
Remove `is_workloop` state, the assertion it was used for in `exec_sync`
is unnecessary, `dispatch_sync*` functions are safe to use on workloops,
they're just less efficient.
This replaces the previous approach in dispatch2 of having
memory-management done internally by the types themselves.
Prefix them with "Dispatch", to better match Swift, and to allow easily
glob-importing from dispatch2 without fear that the symbols will clash
with something else.

This isn't really "rusty", but it plays much nicer with the rest of the
objc2 ecosystem, and matches what Swift does.

It's also clearer at the usage-site. Compare:

   DispatchQueue::main().exec_sync(|| ...)

And:

   Queue::main().exec_sync(|| ...)
Instead, this is delegated to DispatchRetained.
Avoid the extra boolean value by using ManuallyDrop.
@madsmtm madsmtm added enhancement New feature or request A-dispatch2 Affects the `dispatch2` crate labels Apr 8, 2025
@madsmtm madsmtm merged commit e4b72df into master Apr 8, 2025
20 checks passed
@madsmtm madsmtm deleted the dispatch-refactor branch April 8, 2025 23:00
This was referenced Apr 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-dispatch2 Affects the `dispatch2` crate enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant