Skip to content

Commit cd299d2

Browse files
committed
Prepare for using dispatch2 in framework crates
1 parent 210befc commit cd299d2

File tree

7 files changed

+91
-29
lines changed

7 files changed

+91
-29
lines changed

crates/dispatch2/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
//! # Apple's Dispatch (Grand Central Dispatch)
22
//!
3-
//! This crate allows interaction with the [Apple Dispatch](https://developer.apple.com/documentation/dispatch) library in a safe (``dispatch2`` module) and unsafe (``ffi`` module) way.
3+
//! This crate allows interaction with the Apple's Grand Central Dispatch
4+
//! library in a safe (``dispatch2`` module) and unsafe (``ffi`` module) way.
5+
//!
6+
//! See [Apple's documentation](https://developer.apple.com/documentation/dispatch)
7+
//! and [the source code for libdispatch](https://github.com/swiftlang/swift-corelibs-libdispatch)
8+
//! for more details.
49
//!
510
//! ## Example
611
//!
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
framework = "Dispatch"
2+
crate = "dispatch2"
3+
required-crates = []
4+
link = false
5+
skipped = true
6+
custom-lib-rs = true
7+
8+
macos = "10.0"
9+
maccatalyst = "13.0"
10+
ios = "8.0"
11+
tvos = "9.0"
12+
watchos = "2.0"
13+
visionos = "1.0"
14+
gnustep = true
15+
16+
typedef.dispatch_object_t.renamed = "DispatchObject"
17+
typedef.dispatch_queue_t.renamed = "Queue"
18+
typedef.dispatch_data_t.renamed = "Data"
19+
typedef.dispatch_group_t.renamed = "Group"
20+
typedef.dispatch_semaphore_t.renamed = "Semaphore"

crates/header-translator/src/config.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ impl Config {
6666
})
6767
}
6868

69+
pub fn replace_typedef_name(&self, id: ItemIdentifier) -> ItemIdentifier {
70+
let library_config = self.library(id.library_name());
71+
id.map_name(|name| {
72+
library_config
73+
.typedef_data
74+
.get(&name)
75+
.and_then(|data| data.renamed.clone())
76+
.unwrap_or(name)
77+
})
78+
}
79+
6980
pub fn to_parse(&self) -> impl Iterator<Item = (&str, &LibraryConfig)> + Clone {
7081
self.libraries
7182
.iter()
@@ -243,11 +254,11 @@ pub struct CategoryData {
243254
#[derive(Deserialize, Debug, Default, Clone, PartialEq, Eq)]
244255
#[serde(deny_unknown_fields)]
245256
pub struct ProtocolData {
246-
#[serde(default)]
247-
pub renamed: Option<String>,
248257
#[serde(default)]
249258
pub skipped: bool,
250259
#[serde(default)]
260+
pub renamed: Option<String>,
261+
#[serde(default)]
251262
#[serde(rename = "requires-mainthreadonly")]
252263
pub requires_mainthreadonly: Option<bool>,
253264
#[serde(default)]
@@ -273,6 +284,22 @@ pub struct EnumData {
273284
pub constants: HashMap<String, StructData>,
274285
}
275286

287+
#[derive(Deserialize, Debug, Default, Clone, PartialEq, Eq)]
288+
#[serde(deny_unknown_fields)]
289+
pub struct StaticData {
290+
#[serde(default)]
291+
pub skipped: bool,
292+
}
293+
294+
#[derive(Deserialize, Debug, Default, Clone, PartialEq, Eq)]
295+
#[serde(deny_unknown_fields)]
296+
pub struct TypedefData {
297+
#[serde(default)]
298+
pub skipped: bool,
299+
#[serde(default)]
300+
pub renamed: Option<String>,
301+
}
302+
276303
#[derive(Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
277304
#[serde(deny_unknown_fields)]
278305
pub struct MethodData {
@@ -312,10 +339,6 @@ impl Default for FnData {
312339
}
313340
}
314341

315-
// TODO
316-
pub type StaticData = StructData;
317-
pub type TypedefData = StructData;
318-
319342
fn unsafe_default() -> bool {
320343
true
321344
}

crates/header-translator/src/id.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,18 @@ impl ItemIdentifier {
369369
}
370370
}
371371

372+
pub fn copyhelper(mutable: bool) -> Self {
373+
let name = if mutable {
374+
"MutableCopyingHelper"
375+
} else {
376+
"CopyingHelper"
377+
};
378+
Self {
379+
name: name.into(),
380+
location: Location::new("Foundation.NSObject"),
381+
}
382+
}
383+
372384
pub fn block() -> Self {
373385
Self {
374386
name: "Block".into(),

crates/header-translator/src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,13 @@ fn load_config(workspace_dir: &Path) -> Result<Config, BoxError> {
183183
let objc = basic_toml::from_str(&fs::read_to_string(path)?)?;
184184
libraries.insert("ObjectiveC".to_string(), objc);
185185

186+
let path = workspace_dir
187+
.join("crates")
188+
.join("dispatch2")
189+
.join("translation-config.toml");
190+
let objc = basic_toml::from_str(&fs::read_to_string(path)?)?;
191+
libraries.insert("Dispatch".to_string(), objc);
192+
186193
Config::new(libraries)
187194
}
188195

crates/header-translator/src/rust_type.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -600,12 +600,8 @@ impl Ty {
600600
TypeKind::Double => Self::Primitive(Primitive::Double),
601601
TypeKind::Record => {
602602
let declaration = ty.get_declaration().expect("record declaration");
603-
let name = ty
604-
.get_display_name()
605-
.trim_start_matches("struct ")
606-
.to_string();
607603
Self::Struct {
608-
id: ItemIdentifier::with_name(name, &declaration, context),
604+
id: ItemIdentifier::new(&declaration, context),
609605
fields: ty
610606
.get_fields()
611607
.expect("struct fields")
@@ -622,12 +618,8 @@ impl Ty {
622618
}
623619
TypeKind::Enum => {
624620
let declaration = ty.get_declaration().expect("enum declaration");
625-
let name = ty
626-
.get_display_name()
627-
.trim_start_matches("enum ")
628-
.to_string();
629621
Self::Enum {
630-
id: ItemIdentifier::with_name(name, &declaration, context),
622+
id: ItemIdentifier::new(&declaration, context),
631623
ty: Box::new(Ty::parse(
632624
declaration
633625
.get_enum_underlying_type()
@@ -912,6 +904,10 @@ impl Ty {
912904
TypeKind::Typedef => {
913905
let typedef_name = ty.get_typedef_name().expect("typedef has name");
914906
let declaration = ty.get_declaration().expect("typedef declaration");
907+
assert_eq!(
908+
typedef_name,
909+
declaration.get_name().expect("typedef declaration name")
910+
);
915911
let to = declaration
916912
.get_typedef_underlying_type()
917913
.expect("typedef underlying type");
@@ -1024,8 +1020,11 @@ impl Ty {
10241020
};
10251021
}
10261022

1023+
let id = ItemIdentifier::new(&declaration, context);
1024+
let id = context.replace_typedef_name(id);
1025+
10271026
Self::TypeDef {
1028-
id: ItemIdentifier::with_name(typedef_name, &declaration, context),
1027+
id,
10291028
nullability,
10301029
lifetime,
10311030
to: Box::new(Self::parse(to, Lifetime::Unspecified, context)),

crates/header-translator/src/stmt.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,15 +1022,17 @@ impl Stmt {
10221022
}
10231023
EntityKind::TypedefDecl => {
10241024
let id = ItemIdentifier::new(entity, context);
1025+
let id = context.replace_typedef_name(id);
10251026
let availability = Availability::parse(entity, context);
10261027

1027-
if context
1028+
let data = context
10281029
.library(id.library_name())
10291030
.typedef_data
10301031
.get(&id.name)
1031-
.map(|data| data.skipped)
1032-
.unwrap_or_default()
1033-
{
1032+
.cloned()
1033+
.unwrap_or_default();
1034+
1035+
if data.skipped {
10341036
return vec![];
10351037
}
10361038

@@ -1983,13 +1985,7 @@ impl Stmt {
19831985
// need to emit `CopyingHelper` impls to tell Rust which
19841986
// return types they have.
19851987
if matches!(&*protocol.name, "NSCopying" | "NSMutableCopying") {
1986-
let copy_helper = if protocol.name == "NSCopying" {
1987-
protocol.clone().map_name(|_| "CopyingHelper".to_string())
1988-
} else {
1989-
protocol
1990-
.clone()
1991-
.map_name(|_| "MutableCopyingHelper".to_string())
1992-
};
1988+
let copy_helper = ItemIdentifier::copyhelper(protocol.name != "NSCopying");
19931989

19941990
let mut required_items = self.required_items();
19951991

0 commit comments

Comments
 (0)