Skip to content

Commit 78f9941

Browse files
committed
Use sendability attributes to implement Send + Sync
1 parent aa4f3fd commit 78f9941

File tree

6 files changed

+47
-17
lines changed

6 files changed

+47
-17
lines changed

crates/header-translator/src/stmt.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ impl fmt::Display for Stmt {
13191319
derives,
13201320
mutability,
13211321
skipped,
1322-
sendable: _,
1322+
sendable,
13231323
mainthreadonly: _,
13241324
} => {
13251325
if *skipped {
@@ -1429,6 +1429,20 @@ impl fmt::Display for Stmt {
14291429
}
14301430
writeln!(f, " }}")?;
14311431
writeln!(f, ");")?;
1432+
1433+
if *sendable && generics.is_empty() {
1434+
writeln!(f)?;
1435+
if let Some(feature) = &main_feature_gate {
1436+
writeln!(f, " #[cfg(feature = \"{feature}\")]")?;
1437+
}
1438+
writeln!(f, "unsafe impl Send for {} {{}}", id.name)?;
1439+
1440+
writeln!(f)?;
1441+
if let Some(feature) = &main_feature_gate {
1442+
writeln!(f, " #[cfg(feature = \"{feature}\")]")?;
1443+
}
1444+
writeln!(f, "unsafe impl Sync for {} {{}}", id.name)?;
1445+
}
14321446
}
14331447
Self::Methods {
14341448
cls,
@@ -1608,6 +1622,15 @@ impl fmt::Display for Stmt {
16081622
write!(f, "{}", protocol.path())?;
16091623
}
16101624
}
1625+
// TODO
1626+
// if *required_sendable {
1627+
// if protocols.is_empty() {
1628+
// write!(f, ": ")?;
1629+
// } else {
1630+
// write!(f, "+ ")?;
1631+
// }
1632+
// write!(f, "Send + Sync")?;
1633+
// }
16111634
writeln!(f, " {{")?;
16121635

16131636
for method in methods {
@@ -1649,7 +1672,7 @@ impl fmt::Display for Stmt {
16491672
availability,
16501673
boxable: _,
16511674
fields,
1652-
sendable: _,
1675+
sendable,
16531676
} => {
16541677
writeln!(f, "extern_struct!(")?;
16551678
if let Some(encoding_name) = encoding_name {
@@ -1667,14 +1690,22 @@ impl fmt::Display for Stmt {
16671690
}
16681691
writeln!(f, " }}")?;
16691692
writeln!(f, ");")?;
1693+
1694+
if let Some(true) = sendable {
1695+
writeln!(f)?;
1696+
writeln!(f, "unsafe impl Send for {} {{}}", id.name)?;
1697+
1698+
writeln!(f)?;
1699+
writeln!(f, "unsafe impl Sync for {} {{}}", id.name)?;
1700+
}
16701701
}
16711702
Self::EnumDecl {
16721703
id,
16731704
availability,
16741705
ty,
16751706
kind,
16761707
variants,
1677-
sendable: _,
1708+
sendable,
16781709
} => {
16791710
let macro_name = match kind {
16801711
None => "extern_enum",
@@ -1698,6 +1729,16 @@ impl fmt::Display for Stmt {
16981729
}
16991730
writeln!(f, " }}")?;
17001731
writeln!(f, ");")?;
1732+
1733+
if let Some(true) = sendable {
1734+
if let Some(name) = &id.name {
1735+
writeln!(f)?;
1736+
writeln!(f, "unsafe impl Send for {name} {{}}")?;
1737+
1738+
writeln!(f)?;
1739+
writeln!(f, "unsafe impl Sync for {name} {{}}")?;
1740+
}
1741+
}
17011742
}
17021743
Self::VarDecl {
17031744
id,

crates/icrate/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1212

1313
### Added
1414
* Added `MainThreadMarker` `From` implementation for `MainThreadOnly` types.
15+
* Added `Send` and `Sync` implementations for a bunch more types (same as the
16+
ones Swift marks as `@Sendable`).
1517

1618
### Changed
1719
* Moved the `ns_string!` macro to `icrate::Foundation::ns_string`. The old

crates/icrate/src/additions/Foundation/error.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ use crate::Foundation::{
99
self, NSError, NSErrorDomain, NSErrorUserInfoKey, NSInteger, NSLocalizedDescriptionKey,
1010
};
1111

12-
// SAFETY: Error objects are immutable data containers.
13-
unsafe impl Sync for NSError {}
14-
unsafe impl Send for NSError {}
15-
1612
impl UnwindSafe for NSError {}
1713
impl RefUnwindSafe for NSError {}
1814

crates/icrate/src/additions/Foundation/number.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ use objc2::encode::Encoding;
2020
use crate::common::*;
2121
use crate::Foundation::{CGFloat, NSNumber};
2222

23-
// SAFETY: `NSNumber` is a wrapper around an integer/float/bool, and it is
24-
// immutable.
25-
unsafe impl Sync for NSNumber {}
26-
unsafe impl Send for NSNumber {}
27-
2823
impl UnwindSafe for NSNumber {}
2924
impl RefUnwindSafe for NSNumber {}
3025

crates/icrate/src/additions/Foundation/uuid.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ use core::panic::{RefUnwindSafe, UnwindSafe};
55
use crate::common::*;
66
use crate::Foundation::{self, UuidBytes, NSUUID};
77

8-
// SAFETY: `NSUUID` is immutable.
9-
unsafe impl Sync for NSUUID {}
10-
unsafe impl Send for NSUUID {}
11-
128
impl UnwindSafe for NSUUID {}
139
impl RefUnwindSafe for NSUUID {}
1410

crates/icrate/src/generated

0 commit comments

Comments
 (0)