Skip to content

Commit e245f34

Browse files
committed
Explicitly mark objc2-foundation Send and Sync-ness
1 parent 90ed6a7 commit e245f34

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

objc2-foundation/src/array.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ object!(
174174
}
175175
);
176176

177+
// SAFETY: Same as Id<T, O> (which is what NSArray effectively stores).
178+
//
179+
// The `PhantomData` can't get these impls to display in the docs.
180+
//
181+
// TODO: Properly verify this
182+
unsafe impl<T: Sync + Send> Sync for NSArray<T, Shared> {}
183+
unsafe impl<T: Sync + Send> Send for NSArray<T, Shared> {}
184+
unsafe impl<T: Sync> Sync for NSArray<T, Owned> {}
185+
unsafe impl<T: Send> Send for NSArray<T, Owned> {}
186+
177187
unsafe impl<T: INSObject, O: Ownership> INSArray for NSArray<T, O> {
178188
/// The `NSArray` itself (length and number of items) is always immutable,
179189
/// but we would like to know when we're the only owner of the array, to
@@ -319,6 +329,14 @@ object!(
319329
}
320330
);
321331

332+
// SAFETY: Same as NSArray.
333+
//
334+
// TODO: Properly verify this
335+
unsafe impl<T: Sync + Send> Sync for NSMutableArray<T, Shared> {}
336+
unsafe impl<T: Sync + Send> Send for NSMutableArray<T, Shared> {}
337+
unsafe impl<T: Sync> Sync for NSMutableArray<T, Owned> {}
338+
unsafe impl<T: Send> Send for NSMutableArray<T, Owned> {}
339+
322340
unsafe impl<T: INSObject, O: Ownership> INSArray for NSMutableArray<T, O> {
323341
type Ownership = Owned;
324342
type Item = T;
@@ -540,4 +558,14 @@ mod tests {
540558
assert_eq!(strings[1].as_str(pool), "hello");
541559
});
542560
}
561+
562+
#[test]
563+
fn test_send_sync() {
564+
fn assert_send_sync<T: Send + Sync>() {}
565+
566+
assert_send_sync::<NSArray<NSString, Shared>>();
567+
assert_send_sync::<NSMutableArray<NSString, Shared>>();
568+
assert_send_sync::<Id<NSArray<NSString, Shared>, Shared>>();
569+
assert_send_sync::<Id<NSMutableArray<NSString, Shared>, Owned>>();
570+
}
543571
}

objc2-foundation/src/data.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ pub unsafe trait INSData: INSObject {
9494

9595
object!(unsafe pub struct NSData);
9696

97+
// TODO: SAFETY
98+
unsafe impl Sync for NSData {}
99+
unsafe impl Send for NSData {}
100+
97101
unsafe impl INSData for NSData {
98102
type Ownership = Shared;
99103
}
@@ -157,6 +161,10 @@ pub unsafe trait INSMutableData: INSData {
157161

158162
object!(unsafe pub struct NSMutableData);
159163

164+
// TODO: SAFETY
165+
unsafe impl Sync for NSMutableData {}
166+
unsafe impl Send for NSMutableData {}
167+
160168
unsafe impl INSData for NSMutableData {
161169
type Ownership = Owned;
162170
}

objc2-foundation/src/dictionary.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ object!(
145145
}
146146
);
147147

148+
// TODO: SAFETY
149+
unsafe impl<K: Sync + Send, V: Sync> Sync for NSDictionary<K, V> {}
150+
unsafe impl<K: Sync + Send, V: Send> Send for NSDictionary<K, V> {}
151+
148152
impl<K: INSObject, V: INSObject> NSDictionary<K, V> {
149153
unsafe_def_fn!(pub fn new -> Shared);
150154
}

objc2-foundation/src/string.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ pub unsafe trait INSString: INSObject {
102102

103103
object!(unsafe pub struct NSString);
104104

105+
// TODO: SAFETY
106+
unsafe impl Sync for NSString {}
107+
unsafe impl Send for NSString {}
108+
105109
impl NSString {
106110
unsafe_def_fn!(pub fn new -> Shared);
107111
}

objc2-foundation/src/value.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ object!(
7575
}
7676
);
7777

78+
// TODO: SAFETY
79+
unsafe impl<T: Sync> Sync for NSValue<T> {}
80+
unsafe impl<T: Send> Send for NSValue<T> {}
81+
7882
unsafe impl<T: 'static + Copy + Encode> INSValue for NSValue<T> {
7983
type Value = T;
8084
}

0 commit comments

Comments
 (0)