Skip to content

Commit

Permalink
Allow &'static AnyClass in function return
Browse files Browse the repository at this point in the history
Also delegate to `fn_return` in `method_return`.
  • Loading branch information
madsmtm committed Nov 25, 2024
1 parent 11c9655 commit 93de19f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 48 deletions.
85 changes: 39 additions & 46 deletions crates/header-translator/src/rust_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1454,54 +1454,34 @@ impl Ty {
}

pub(crate) fn method_return(&self) -> impl fmt::Display + '_ {
FormatterFn(move |f| {
if let Self::Primitive(Primitive::Void) = self {
// Don't output anything
return Ok(());
}

write!(f, " -> ")?;

match self {
Self::Pointer {
nullability,
pointee,
..
} if pointee.is_static_object() => {
if *nullability == Nullability::NonNull {
write!(f, "&'static {}", pointee.behind_pointer())
} else {
write!(f, "Option<&'static {}>", pointee.behind_pointer())
}
}
Self::Pointer {
nullability,
lifetime: Lifetime::Unspecified,
pointee,
..
} if pointee.is_object_like() => {
if *nullability == Nullability::NonNull {
write!(f, "Retained<{}>", pointee.behind_pointer())
} else {
write!(f, "Option<Retained<{}>>", pointee.behind_pointer())
}
}
Self::TypeDef {
id, nullability, ..
} if self.is_object_like() => {
if *nullability == Nullability::NonNull {
write!(f, "Retained<{}>", id.path())
} else {
write!(f, "Option<Retained<{}>>", id.path())
}
FormatterFn(move |f| match self {
Self::Pointer {
nullability,
lifetime: Lifetime::Unspecified,
pointee,
..
} if pointee.is_object_like() && !pointee.is_static_object() => {
if *nullability == Nullability::NonNull {
write!(f, " -> Retained<{}>", pointee.behind_pointer())
} else {
write!(f, " -> Option<Retained<{}>>", pointee.behind_pointer())
}
Self::Primitive(Primitive::C99Bool) => {
warn!("C99's bool as Objective-C method return is ill supported");
write!(f, "bool")
}
Self::TypeDef {
id, nullability, ..
} if self.is_object_like() && !self.is_static_object() => {
if *nullability == Nullability::NonNull {
write!(f, " -> Retained<{}>", id.path())
} else {
write!(f, " -> Option<Retained<{}>>", id.path())
}
Self::Primitive(Primitive::ObjcBool) => write!(f, "bool"),
_ => write!(f, "{}", self.plain()),
}
Self::Primitive(Primitive::C99Bool) => {
warn!("C99's bool as Objective-C method return is ill supported");
write!(f, " -> bool")
}
Self::Primitive(Primitive::ObjcBool) => write!(f, " -> bool"),
_ => write!(f, "{}", self.fn_return()),
})
}

Expand Down Expand Up @@ -1573,7 +1553,20 @@ impl Ty {
return Ok(());
}

write!(f, " -> {}", self.plain())
match self {
Self::Pointer {
nullability,
pointee,
..
} if pointee.is_static_object() => {
if *nullability == Nullability::NonNull {
write!(f, " -> &'static {}", pointee.behind_pointer())
} else {
write!(f, " -> Option<&'static {}>", pointee.behind_pointer())
}
}
_ => write!(f, " -> {}", self.plain()),
}
})
}

Expand Down
3 changes: 2 additions & 1 deletion crates/objc2/src/topics/about_generated/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- A bunch of things in `WebKit`.
* **BREAKING**: Marked methods on the `NSObjectUIAccessibility` category as
`MainThreadOnly`.
* Fixed `AnyClass` and `AnyProtocol` mapping. For example
* **BREAKING**: Fixed `AnyClass` and `AnyProtocol` mapping. For example
`NSStringFromProtocol` now takes `&AnyProtocol`,
`NSPasteboard::readObjectsForClasses_options` now takes
`&NSArray<AnyClass>`, and `UITrait` is now exposed more correctly.
* **BREAKING**: Fixed the return type of `NSClassFromString`.


## 0.2.2 - 2024-05-21
Expand Down
2 changes: 1 addition & 1 deletion generated

0 comments on commit 93de19f

Please sign in to comment.