Skip to content

Commit 1c9c9d3

Browse files
authored
Merge pull request #143 from madsmtm/foundation-asref-asmut
Impl `AsRef` and `AsMut` for objects, to convert them to their superclasses
2 parents 13b2c8a + f3b99eb commit 1c9c9d3

File tree

6 files changed

+53
-18
lines changed

6 files changed

+53
-18
lines changed

objc2-foundation/src/array.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ unsafe impl<T: Send> Send for NSArray<T, Owned> {}
4343
object! {
4444
// TODO: Ensure that this deref to NSArray is safe!
4545
// This "inherits" NSArray, and has the same `Send`/`Sync` impls as that.
46-
unsafe pub struct NSMutableArray<T, O: Ownership>: NSArray<T, O> {}
46+
unsafe pub struct NSMutableArray<T, O: Ownership>: NSArray<T, O>, NSObject {}
4747
}
4848

4949
unsafe fn from_refs<T: Message + ?Sized>(cls: &Class, refs: &[&T]) -> *mut Object {

objc2-foundation/src/data.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ object! {
3232
/// See [Apple's documentation](https://developer.apple.com/documentation/foundation/nsmutabledata?language=objc).
3333
///
3434
/// [`Vec`]: std::vec::Vec
35-
unsafe pub struct NSMutableData: NSData;
35+
unsafe pub struct NSMutableData: NSData, NSObject;
3636
}
3737

3838
// TODO: SAFETY

objc2-foundation/src/macros.rs

+45-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
macro_rules! __impl_as_ref_as_mut {
2+
($name:ident<$($t:ident $(: $b:ident)?),*>,) => {};
3+
($name:ident<$($t:ident $(: $b:ident)?),*>, $item:ty, $($tail:ty,)*) => {
4+
impl<$($t $(: $b)?),*> AsRef<$item> for $name<$($t),*> {
5+
#[inline]
6+
fn as_ref(&self) -> &$item {
7+
&**self
8+
}
9+
}
10+
11+
impl<$($t $(: $b)?),*> AsMut<$item> for $name<$($t),*> {
12+
#[inline]
13+
fn as_mut(&mut self) -> &mut $item {
14+
&mut **self
15+
}
16+
}
17+
18+
__impl_as_ref_as_mut!($name<$($t $(: $b)?),*>, $($tail,)*);
19+
};
20+
}
21+
122
/// TODO
223
///
324
/// # Safety
@@ -9,13 +30,32 @@
930
macro_rules! object {
1031
(
1132
$(#[$m:meta])*
12-
unsafe $v:vis struct $name:ident: $inherits:ty $(;)?
33+
unsafe $v:vis struct $name:ident: $($inheritance_chain:ty),+ $(;)?
1334
) => {
14-
object!($(#[$m])* unsafe $v struct $name<>: $inherits {});
35+
object! {
36+
@__inner
37+
$(#[$m])*
38+
unsafe $v struct $name<>: $($inheritance_chain,)+ ::objc2::runtime::Object {}
39+
}
1540
};
1641
(
1742
$(#[$m:meta])*
18-
unsafe $v:vis struct $name:ident<$($t:ident $(: $b:ident)?),*>: $inherits:ty {
43+
unsafe $v:vis struct $name:ident<$($t:ident $(: $b:ident)?),*>: $($inheritance_chain:ty),+ {
44+
$($p:ident: $pty:ty,)*
45+
}
46+
) => {
47+
object! {
48+
@__inner
49+
$(#[$m])*
50+
unsafe $v struct $name<$($t $(: $b)?),*>: $($inheritance_chain,)+ ::objc2::runtime::Object {
51+
$($p: $pty,)*
52+
}
53+
}
54+
};
55+
(
56+
@__inner
57+
$(#[$m:meta])*
58+
unsafe $v:vis struct $name:ident<$($t:ident $(: $b:ident)?),*>: $inherits:ty $(, $inheritance_rest:ty)* {
1959
$($p:ident: $pty:ty,)*
2060
}
2161
) => {
@@ -85,7 +125,7 @@ macro_rules! object {
85125
}
86126
}
87127

88-
// TODO: AsRef and AsMut
128+
__impl_as_ref_as_mut!($name<$($t $(: $b)?),*>, $inherits, $($inheritance_rest,)*);
89129

90130
// Objective-C equality has approximately the same semantics as Rust
91131
// equality (although less aptly specified).
@@ -100,13 +140,7 @@ macro_rules! object {
100140
impl<$($t: ::core::cmp::PartialEq $(+ $b)?),*> ::core::cmp::PartialEq for $name<$($t),*> {
101141
#[inline]
102142
fn eq(&self, other: &Self) -> bool {
103-
use ::objc2::MessageReceiver;
104-
use $crate::NSObject;
105-
// "downgrading" to NSObject to work around generic
106-
// downgrading not having been set up yet.
107-
// TODO: Fix this.
108-
let other: &NSObject = unsafe { &*other.as_raw_receiver().cast() };
109-
self.is_equal(other)
143+
self.is_equal(&*other)
110144
}
111145
}
112146

objc2-foundation/src/mutable_attributed_string.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use objc2::msg_send;
22
use objc2::rc::{DefaultId, Id, Owned, Shared};
33

4-
use crate::{NSAttributedString, NSCopying, NSMutableCopying, NSString};
4+
use crate::{NSAttributedString, NSCopying, NSMutableCopying, NSObject, NSString};
55

66
object! {
77
/// A mutable string that has associated attributes.
88
///
99
/// See [Apple's documentation](https://developer.apple.com/documentation/foundation/nsmutableattributedstring?language=objc).
10-
unsafe pub struct NSMutableAttributedString: NSAttributedString;
10+
unsafe pub struct NSMutableAttributedString: NSAttributedString, NSObject;
1111
}
1212

1313
// TODO: SAFETY

objc2-foundation/src/mutable_string.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use core::str;
66
use objc2::msg_send;
77
use objc2::rc::{DefaultId, Id, Owned, Shared};
88

9-
use crate::{NSCopying, NSMutableCopying, NSString};
9+
use crate::{NSCopying, NSMutableCopying, NSObject, NSString};
1010

1111
object! {
1212
/// A dynamic plain-text Unicode string object.
1313
///
1414
/// See [Apple's documentation](https://developer.apple.com/documentation/foundation/nsmutablestring?language=objc).
15-
unsafe pub struct NSMutableString: NSString;
15+
unsafe pub struct NSMutableString: NSString, NSObject;
1616
}
1717

1818
// TODO: SAFETY

objc2-foundation/src/object.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use objc2::runtime::{Bool, Class, Object};
55
use super::NSString;
66

77
object! {
8-
unsafe pub struct NSObject: Object;
8+
@__inner
9+
unsafe pub struct NSObject<>: Object {}
910
}
1011

1112
impl NSObject {

0 commit comments

Comments
 (0)