Skip to content

Commit d8db2f1

Browse files
committed
Mark Metal protocols as IsRetainable
This may technically be a breaking change if the user implemented these protocols themselves on a `Mutable` class, but that'd be unsound anyhow, so I'll consider this a correctness fix. This is useful for wgpu, see gfx-rs/wgpu#5641, and the hack will become unnecessary after #563.
1 parent 6b30466 commit d8db2f1

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

crates/header-translator/src/stmt.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,6 +2018,22 @@ impl Stmt {
20182018
}
20192019
write!(f, "IsMainThreadOnly")?;
20202020
}
2021+
// HACK: Make all Metal protocols retainable (i.e. allow
2022+
// using `.retain()` on `&ProtocolObject<dyn MTLDevice>`).
2023+
//
2024+
// This is a problem generally, but especially useful in
2025+
// Metal, as most of the functionality is only exposed
2026+
// through protocols (instead of concrete classes).
2027+
//
2028+
// This should become unnecessary after #563.
2029+
if id.library_name() == "Metal" {
2030+
if protocols.is_empty() {
2031+
write!(f, ": ")?;
2032+
} else {
2033+
write!(f, "+ ")?;
2034+
}
2035+
write!(f, "IsRetainable")?;
2036+
}
20212037
writeln!(f, " {{")?;
20222038

20232039
let required_items = self.required_items();

crates/objc2/src/__framework_prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub use crate::encode::{Encode, Encoding, RefEncode};
1515
pub use crate::ffi::{NSInteger, NSIntegerMax, NSUInteger, NSUIntegerMax, IMP};
1616
pub use crate::mutability::{
1717
Immutable, ImmutableWithMutableSubclass, InteriorMutable, IsIdCloneable, IsMainThreadOnly,
18-
MainThreadOnly, Mutable, MutableWithImmutableSuperclass,
18+
IsRetainable, MainThreadOnly, Mutable, MutableWithImmutableSuperclass,
1919
};
2020
pub use crate::rc::{Allocated, DefaultId, Id};
2121
pub use crate::runtime::{

crates/objc2/src/topics/about_generated/CHANGELOG.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1717

1818
This means that they have the usual `ops::BitAnd`, `ops::BitOr`, `ops::Not`,
1919
etc. implementations that you would expect from an option enum.
20+
* Added optional support for a few methods depending on types from `libc`.
2021
* Added new framework crates:
2122
- `CoreBluetooth` / `objc2-core-bluetooth`.
2223
- `Virtualization` / `objc2-virtualization`.
@@ -40,21 +41,27 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
4041
- `PhotosUI` / `objc2-photos-ui`.
4142
- `FinderSync` / `objc2-finder-sync`.
4243
- `Social` / `objc2-social`.
43-
* Added `objc2_quartz_core::CAMetalDrawable` and
44-
`objc2_quartz_core::CAMetalLayer`.
45-
* Added methods to access `CALayer` from `objc2_app_kit::NSView`.
46-
* Added optional support for a few methods depending on types from `libc`.
47-
* Added `MTLCounterErrorValue`, `MTLCounterDontSample` and
48-
`MTLTextureSwizzleChannelsDefault` Metal constants.
44+
* `objc2-quartz-core`: Added `CAMetalDrawable` and `CAMetalLayer`.
45+
* `objc2-app-kit`: Added methods to access `CALayer` from `NSView`.
46+
* `objc2-metal`: Added `MTLCounterErrorValue`, `MTLCounterDontSample` and
47+
`MTLTextureSwizzleChannelsDefault` constants.
4948

5049
### Changed
5150
* Updated SDK from Xcode 15.3 to 15.4.
5251

5352
View the release notes to learn more details:
5453
- [15.4](https://developer.apple.com/documentation/xcode-release-notes/xcode-15_4-release-notes)
5554

55+
### Fixed
56+
* `objc2-metal`: All protocols are now marked as `IsRetainable`.
57+
58+
This may technically break your code if you had some custom object that
59+
implements the protocol, but was marked with `Mutability = Mutable`, but
60+
Metal protocols are assumed retainable by Objective-C code, so that'd have
61+
been unsound anyway.
62+
5663
### Removed
57-
* `objc2_metal`: Removed internal `__MTLPackedFloat3` and made `MTLPackedFloat3` public.
64+
* `objc2-metal`: Removed internal `__MTLPackedFloat3` and made `MTLPackedFloat3` public.
5865

5966

6067
## 0.2.0 - 2024-04-17

0 commit comments

Comments
 (0)