Skip to content

Commit 1090ca2

Browse files
committed
Automatically generate DefaultId implementations
1 parent 11a1d50 commit 1090ca2

19 files changed

+36
-110
lines changed

crates/header-translator/src/method.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ impl Method {
246246
(self.is_class, self.selector.clone())
247247
}
248248

249+
pub(crate) fn usable_in_default_id(&self) -> bool {
250+
self.selector == "new" && self.is_class && self.arguments.is_empty() && self.safe
251+
}
252+
249253
fn parent_type_data(entity: &Entity<'_>, context: &Context<'_>) -> (bool, bool) {
250254
let parent = entity.get_semantic_parent().expect("method parent");
251255
let (parent, is_protocol) = match parent.get_kind() {

crates/header-translator/src/stmt.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,25 @@ impl fmt::Display for Stmt {
14281428
}
14291429
writeln!(f, " }}")?;
14301430
writeln!(f, ");")?;
1431+
1432+
if let Some(method) = methods.iter().find(|method| method.usable_in_default_id()) {
1433+
if let Some(feature) = cls.feature() {
1434+
// Assume new methods require no extra features
1435+
writeln!(f, " #[cfg(feature = \"{feature}\")]")?;
1436+
}
1437+
writeln!(
1438+
f,
1439+
"impl{} DefaultId for {}{} {{",
1440+
GenericParamsHelper(generics, "Message"),
1441+
cls.path_in_relation_to(category),
1442+
GenericTyHelper(generics),
1443+
)?;
1444+
writeln!(f, " #[inline]")?;
1445+
writeln!(f, " fn default_id() -> Id<Self> {{")?;
1446+
writeln!(f, " Self::{}()", method.fn_name)?;
1447+
writeln!(f, " }}")?;
1448+
writeln!(f, "}}")?;
1449+
}
14311450
}
14321451
Self::ProtocolImpl {
14331452
cls,

crates/icrate/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
4343
* **BREAKING**: Removed ownership parameter from generic types, since the
4444
ownership/mutability information is now stored in `ClassType::Mutability`.
4545
* **BREAKING**: Renamed `NSMutableCopying::mutable_copy` to `::mutableCopy`.
46+
* **BREAKING**: The default value for `NSUUID` was changed from a nil UUID to
47+
a new random UUID.
4648

4749
### Removed
4850
* **BREAKING**: Removed various redundant `NSProxy` methods.

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use core::panic::{RefUnwindSafe, UnwindSafe};
77

88
use objc2::msg_send;
99
use objc2::mutability::{IsMutable, IsRetainable};
10-
use objc2::rc::DefaultId;
1110

1211
use super::util;
1312
use crate::common::*;
@@ -234,13 +233,6 @@ impl<T: IsMutable> IndexMut<usize> for NSArray<T> {
234233
}
235234
}
236235

237-
impl<T: Message> DefaultId for NSArray<T> {
238-
#[inline]
239-
fn default_id() -> Id<Self> {
240-
Self::new()
241-
}
242-
}
243-
244236
impl<T: fmt::Debug + Message> fmt::Debug for NSArray<T> {
245237
#[inline]
246238
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
use core::panic::{RefUnwindSafe, UnwindSafe};
33

44
use objc2::extern_methods;
5-
use objc2::rc::DefaultId;
65

76
use crate::common::*;
87
use crate::Foundation::{self, NSAttributedString, NSAttributedStringKey};
@@ -45,10 +44,3 @@ extern_methods!(
4544
}
4645
}
4746
);
48-
49-
impl DefaultId for NSAttributedString {
50-
#[inline]
51-
fn default_id() -> Id<Self> {
52-
Self::new()
53-
}
54-
}

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use core::ops::Index;
66
use core::panic::{RefUnwindSafe, UnwindSafe};
77
use core::slice::{self, SliceIndex};
88

9-
use objc2::rc::DefaultId;
10-
119
use crate::common::*;
1210
use crate::Foundation::{self, NSData};
1311

@@ -85,13 +83,6 @@ impl<I: SliceIndex<[u8]>> Index<I> for NSData {
8583
}
8684
}
8785

88-
impl DefaultId for NSData {
89-
#[inline]
90-
fn default_id() -> Id<Self> {
91-
Self::new()
92-
}
93-
}
94-
9586
impl fmt::Debug for NSData {
9687
#[inline]
9788
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use core::ptr::{self, NonNull};
99

1010
use objc2::msg_send;
1111
use objc2::mutability::IsMutable;
12-
use objc2::rc::DefaultId;
1312
use objc2::runtime::Object;
1413

1514
use super::util;
@@ -211,13 +210,6 @@ extern_methods!(
211210
}
212211
);
213212

214-
impl<K: Message, V: Message> DefaultId for NSDictionary<K, V> {
215-
#[inline]
216-
fn default_id() -> Id<Self> {
217-
Self::new()
218-
}
219-
}
220-
221213
unsafe impl<K: Message, V: Message> Foundation::NSFastEnumeration2 for NSDictionary<K, V> {
222214
type Item = K;
223215
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use core::cmp::Ordering;
44
use core::ops::{Index, IndexMut};
55

66
use objc2::mutability::{IsMutable, IsRetainable};
7-
use objc2::rc::DefaultId;
87

98
use super::util;
109
use crate::common::*;
@@ -175,10 +174,3 @@ impl<T: IsMutable> IndexMut<usize> for NSMutableArray<T> {
175174
self.get_mut(index).unwrap()
176175
}
177176
}
178-
179-
impl<T: Message> DefaultId for NSMutableArray<T> {
180-
#[inline]
181-
fn default_id() -> Id<Self> {
182-
Self::new()
183-
}
184-
}

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#![cfg(feature = "Foundation_NSMutableAttributedString")]
2-
use objc2::rc::DefaultId;
3-
42
use crate::common::*;
53
use crate::Foundation::{self, NSAttributedString, NSMutableAttributedString};
64

@@ -19,10 +17,3 @@ impl NSMutableAttributedString {
1917
Self::initWithAttributedString(Self::alloc(), attributed_string)
2018
}
2119
}
22-
23-
impl DefaultId for NSMutableAttributedString {
24-
#[inline]
25-
fn default_id() -> Id<Self> {
26-
Self::new()
27-
}
28-
}

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use core::ops::{Index, IndexMut, Range};
55
use core::slice::{self, SliceIndex};
66
use std::io;
77

8-
use objc2::rc::DefaultId;
9-
108
use crate::common::*;
119
use crate::Foundation::{NSMutableData, NSRange};
1210

@@ -136,13 +134,6 @@ impl io::Write for NSMutableData {
136134
}
137135
}
138136

139-
impl DefaultId for NSMutableData {
140-
#[inline]
141-
fn default_id() -> Id<Self> {
142-
Self::new()
143-
}
144-
}
145-
146137
impl<'a> IntoIterator for &'a NSMutableData {
147138
type Item = &'a u8;
148139
type IntoIter = core::slice::Iter<'a, u8>;

0 commit comments

Comments
 (0)