Skip to content

Commit

Permalink
Use sendability attributes to implement Send + Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Sep 3, 2023
1 parent aa4f3fd commit 78f9941
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 17 deletions.
47 changes: 44 additions & 3 deletions crates/header-translator/src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ impl fmt::Display for Stmt {
derives,
mutability,
skipped,
sendable: _,
sendable,
mainthreadonly: _,
} => {
if *skipped {
Expand Down Expand Up @@ -1429,6 +1429,20 @@ impl fmt::Display for Stmt {
}
writeln!(f, " }}")?;
writeln!(f, ");")?;

if *sendable && generics.is_empty() {
writeln!(f)?;
if let Some(feature) = &main_feature_gate {
writeln!(f, " #[cfg(feature = \"{feature}\")]")?;
}
writeln!(f, "unsafe impl Send for {} {{}}", id.name)?;

writeln!(f)?;
if let Some(feature) = &main_feature_gate {
writeln!(f, " #[cfg(feature = \"{feature}\")]")?;
}
writeln!(f, "unsafe impl Sync for {} {{}}", id.name)?;
}
}
Self::Methods {
cls,
Expand Down Expand Up @@ -1608,6 +1622,15 @@ impl fmt::Display for Stmt {
write!(f, "{}", protocol.path())?;
}
}
// TODO
// if *required_sendable {
// if protocols.is_empty() {
// write!(f, ": ")?;
// } else {
// write!(f, "+ ")?;
// }
// write!(f, "Send + Sync")?;
// }
writeln!(f, " {{")?;

for method in methods {
Expand Down Expand Up @@ -1649,7 +1672,7 @@ impl fmt::Display for Stmt {
availability,
boxable: _,
fields,
sendable: _,
sendable,
} => {
writeln!(f, "extern_struct!(")?;
if let Some(encoding_name) = encoding_name {
Expand All @@ -1667,14 +1690,22 @@ impl fmt::Display for Stmt {
}
writeln!(f, " }}")?;
writeln!(f, ");")?;

if let Some(true) = sendable {
writeln!(f)?;
writeln!(f, "unsafe impl Send for {} {{}}", id.name)?;

writeln!(f)?;
writeln!(f, "unsafe impl Sync for {} {{}}", id.name)?;
}
}
Self::EnumDecl {
id,
availability,
ty,
kind,
variants,
sendable: _,
sendable,
} => {
let macro_name = match kind {
None => "extern_enum",
Expand All @@ -1698,6 +1729,16 @@ impl fmt::Display for Stmt {
}
writeln!(f, " }}")?;
writeln!(f, ");")?;

if let Some(true) = sendable {
if let Some(name) = &id.name {
writeln!(f)?;
writeln!(f, "unsafe impl Send for {name} {{}}")?;

writeln!(f)?;
writeln!(f, "unsafe impl Sync for {name} {{}}")?;
}
}
}
Self::VarDecl {
id,
Expand Down
2 changes: 2 additions & 0 deletions crates/icrate/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

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

### Changed
* Moved the `ns_string!` macro to `icrate::Foundation::ns_string`. The old
Expand Down
4 changes: 0 additions & 4 deletions crates/icrate/src/additions/Foundation/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ use crate::Foundation::{
self, NSError, NSErrorDomain, NSErrorUserInfoKey, NSInteger, NSLocalizedDescriptionKey,
};

// SAFETY: Error objects are immutable data containers.
unsafe impl Sync for NSError {}
unsafe impl Send for NSError {}

impl UnwindSafe for NSError {}
impl RefUnwindSafe for NSError {}

Expand Down
5 changes: 0 additions & 5 deletions crates/icrate/src/additions/Foundation/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ use objc2::encode::Encoding;
use crate::common::*;
use crate::Foundation::{CGFloat, NSNumber};

// SAFETY: `NSNumber` is a wrapper around an integer/float/bool, and it is
// immutable.
unsafe impl Sync for NSNumber {}
unsafe impl Send for NSNumber {}

impl UnwindSafe for NSNumber {}
impl RefUnwindSafe for NSNumber {}

Expand Down
4 changes: 0 additions & 4 deletions crates/icrate/src/additions/Foundation/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ use core::panic::{RefUnwindSafe, UnwindSafe};
use crate::common::*;
use crate::Foundation::{self, UuidBytes, NSUUID};

// SAFETY: `NSUUID` is immutable.
unsafe impl Sync for NSUUID {}
unsafe impl Send for NSUUID {}

impl UnwindSafe for NSUUID {}
impl RefUnwindSafe for NSUUID {}

Expand Down
2 changes: 1 addition & 1 deletion crates/icrate/src/generated

0 comments on commit 78f9941

Please sign in to comment.