From 78f9941c1d27d0cb1584516ee30c67fc689d0720 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sat, 2 Sep 2023 01:02:29 +0200 Subject: [PATCH] Use sendability attributes to implement Send + Sync --- crates/header-translator/src/stmt.rs | 47 +++++++++++++++++-- crates/icrate/CHANGELOG.md | 2 + .../icrate/src/additions/Foundation/error.rs | 4 -- .../icrate/src/additions/Foundation/number.rs | 5 -- .../icrate/src/additions/Foundation/uuid.rs | 4 -- crates/icrate/src/generated | 2 +- 6 files changed, 47 insertions(+), 17 deletions(-) diff --git a/crates/header-translator/src/stmt.rs b/crates/header-translator/src/stmt.rs index f7a18792d..7e79d700f 100644 --- a/crates/header-translator/src/stmt.rs +++ b/crates/header-translator/src/stmt.rs @@ -1319,7 +1319,7 @@ impl fmt::Display for Stmt { derives, mutability, skipped, - sendable: _, + sendable, mainthreadonly: _, } => { if *skipped { @@ -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, @@ -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 { @@ -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 { @@ -1667,6 +1690,14 @@ 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, @@ -1674,7 +1705,7 @@ impl fmt::Display for Stmt { ty, kind, variants, - sendable: _, + sendable, } => { let macro_name = match kind { None => "extern_enum", @@ -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, diff --git a/crates/icrate/CHANGELOG.md b/crates/icrate/CHANGELOG.md index 97636647d..dada78da5 100644 --- a/crates/icrate/CHANGELOG.md +++ b/crates/icrate/CHANGELOG.md @@ -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 diff --git a/crates/icrate/src/additions/Foundation/error.rs b/crates/icrate/src/additions/Foundation/error.rs index 48be36a90..54d615ca4 100644 --- a/crates/icrate/src/additions/Foundation/error.rs +++ b/crates/icrate/src/additions/Foundation/error.rs @@ -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 {} diff --git a/crates/icrate/src/additions/Foundation/number.rs b/crates/icrate/src/additions/Foundation/number.rs index 386239da4..8a997e5ea 100644 --- a/crates/icrate/src/additions/Foundation/number.rs +++ b/crates/icrate/src/additions/Foundation/number.rs @@ -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 {} diff --git a/crates/icrate/src/additions/Foundation/uuid.rs b/crates/icrate/src/additions/Foundation/uuid.rs index 05d4385ca..df6036670 100644 --- a/crates/icrate/src/additions/Foundation/uuid.rs +++ b/crates/icrate/src/additions/Foundation/uuid.rs @@ -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 {} diff --git a/crates/icrate/src/generated b/crates/icrate/src/generated index a8844894e..bfe4ba4a9 160000 --- a/crates/icrate/src/generated +++ b/crates/icrate/src/generated @@ -1 +1 @@ -Subproject commit a8844894e7c9dca271c325429a57c9b2dde76dee +Subproject commit bfe4ba4a9871a46d1f9374854501b0a32eb608a6