Skip to content

Commit 0f56bd7

Browse files
Now everything compiles once again
1 parent efb6a51 commit 0f56bd7

File tree

10 files changed

+363
-272
lines changed

10 files changed

+363
-272
lines changed

src/avfoundation.rs

Lines changed: 112 additions & 113 deletions
Large diffs are not rendered by default.

src/base/core_foundation.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,33 @@ extern "C" {
4545

4646
pub trait CFTypeInterface
4747
where
48-
// to be able to have default implementations of methods returning Self
48+
Self: ptr::AsRaw,
4949
Self: Sized,
50-
// all objects should be clonable (here cloning just means increasing the reference count)
51-
Self: Clone,
5250
{
53-
fn as_raw(&self) -> ptr::cf::RawRef;
54-
5551
fn equal(&self, other: &impl CFTypeInterface) -> bool {
56-
let self_raw = self.as_raw();
57-
let other_raw = other.as_raw();
52+
let self_raw = self.as_raw_ref();
53+
let other_raw = other.as_raw_ref();
5854
let ret = unsafe { CFEqual(self_raw, other_raw) };
5955
ret.into()
6056
}
6157

6258
fn show(&self) {
63-
let self_raw = self.as_raw();
59+
let self_raw = self.as_raw_ref();
6460
unsafe { CFShow(self_raw) };
6561
}
6662

6763
fn retain_count(&self) -> isize {
68-
let self_raw = self.as_raw();
64+
let self_raw = self.as_raw_ref();
6965
unsafe { CFGetRetainCount(self_raw) }
7066
}
7167

7268
fn hash(&self) -> usize {
73-
let self_raw = self.as_raw();
69+
let self_raw = self.as_raw_ref();
7470
unsafe { CFHash(self_raw) }
7571
}
7672

7773
fn type_id(&self) -> CFTypeID {
78-
let self_raw = self.as_raw();
74+
let self_raw = self.as_raw_ref();
7975
unsafe { CFGetTypeID(self_raw) }
8076
}
8177
}

src/base/objc.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::base::ptr;
1+
use crate::base::ptr::{self, FromOwned};
22
use std::ptr::NonNull;
33

44
pub type NSInteger = isize;
@@ -44,62 +44,57 @@ extern "C" {
4444
) -> ptr::objc::NullableRawPtr;
4545
}
4646

47-
// TODO: Move to ptr module
48-
pub trait FromOwnedPtr {
49-
unsafe fn from_owned_ptr_unchecked(owned_ptr: ptr::objc::OwnedPtr) -> Self;
50-
}
51-
5247
// TODO: Is that really safe?
5348
impl<T> ptr::Retain for T
5449
where
55-
T: ptr::objc::AsRawPtr + FromOwnedPtr,
50+
T: ptr::AsRaw + ptr::FromOwned,
5651
{
5752
type Owned = Self;
5853

5954
fn retain(&self) -> Self::Owned {
60-
unsafe { Self::from_owned_ptr_unchecked(self.as_raw().retain()) }
55+
unsafe { Self::from_owned_ptr_unchecked(self.as_raw_ptr().retain()) }
6156
}
6257
}
6358

6459
/// Indicates that the type can be used as type parameter for Objective-C classes like NSArray.
6560
/// That does not include special types like StaticNSString or ImmutableNSString.
66-
pub trait ValidObjCGeneric: ptr::objc::AsRawPtr + FromOwnedPtr {}
61+
pub trait ValidObjCGeneric: ptr::AsRaw + ptr::FromOwned {}
6762

68-
// TODO: IsKinfOf should maybe be unsafe
63+
// TODO: IsKindOf should maybe be unsafe
6964
/// Marker trait used for handling of type parameters in NSArray and NSDictionary.
70-
pub trait IsKindOf<T: ValidObjCGeneric>: ptr::objc::AsRawPtr {}
65+
pub trait IsKindOf<T: ValidObjCGeneric>: ptr::AsRaw {}
7166
impl<T: ValidObjCGeneric> IsKindOf<T> for T {}
7267

7368
pub trait NSObjectProtocol
7469
where
75-
Self: ptr::objc::AsRawPtr,
70+
Self: ptr::AsRaw,
7671
Self: Sized,
7772
{
78-
type Owned: FromOwnedPtr;
73+
type Owned: ptr::FromOwned;
7974

8075
/// Objective-C class represented by the struct implementing this trait..
8176
fn class() -> ptr::objc::ClassPtr;
8277

8378
fn hash(&self) -> usize {
84-
unsafe { choco_base_NSObjectProtocol_instance_hash(self.as_raw()) }
79+
unsafe { choco_base_NSObjectProtocol_instance_hash(self.as_raw_ptr()) }
8580
}
8681
// In Objective-C, the parameter to -[NSObject isEqual:] is nullable,
8782
// we consider it non-nullable to makes things simpler.
8883
fn is_equal(&self, obj: &impl NSObjectProtocol) -> bool {
89-
let self_raw = self.as_raw();
90-
let obj_raw = obj.as_raw();
84+
let self_raw = self.as_raw_ptr();
85+
let obj_raw = obj.as_raw_ptr();
9186
let ret = unsafe { choco_base_NSObjectProtocol_instance_isEqual(self_raw, obj_raw.into()) };
9287
ret.into()
9388
}
9489

9590
fn is_kind_of(&self, class: ptr::objc::ClassPtr) -> bool {
96-
let self_raw = self.as_raw();
91+
let self_raw = self.as_raw_ptr();
9792
let ret = unsafe { choco_base_NSObjectProtocol_instance_isKindOfClass(self_raw, class) };
9893
ret.into()
9994
}
10095

10196
fn description(&self) -> crate::foundation::NSString {
102-
let self_raw = self.as_raw();
97+
let self_raw = self.as_raw_ptr();
10398
unsafe {
10499
let owned_ptr = choco_base_NSObjectProtocol_instance_description(self_raw)
105100
.unwrap()
@@ -109,7 +104,7 @@ where
109104
}
110105

111106
fn debug_description(&self) -> crate::foundation::NSString {
112-
let self_raw = self.as_raw();
107+
let self_raw = self.as_raw_ptr();
113108
unsafe {
114109
let owned_ptr = choco_base_NSObjectProtocol_instance_debugDescription(self_raw)
115110
.unwrap()
@@ -134,13 +129,13 @@ pub struct NSObject {
134129
ptr: ptr::objc::OwnedPtr,
135130
}
136131

137-
impl ptr::objc::AsRawPtr for NSObject {
138-
fn as_raw(&self) -> ptr::objc::RawPtr {
139-
self.ptr.as_raw()
132+
impl ptr::AsRaw for NSObject {
133+
fn as_raw_ptr(&self) -> ptr::objc::RawPtr {
134+
self.ptr.as_raw_ptr()
140135
}
141136
}
142137

143-
impl FromOwnedPtr for NSObject {
138+
impl ptr::FromOwned for NSObject {
144139
unsafe fn from_owned_ptr_unchecked(ptr: ptr::objc::OwnedPtr) -> Self {
145140
Self { ptr }
146141
}

src/base/ptr.rs

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,32 @@ pub trait Retain {
33
fn retain(&self) -> Self::Owned;
44
}
55

6+
/// At least one of the two methods must be implemented (or else you'll end up with infinite recursion).
7+
pub trait AsRaw {
8+
fn as_raw_ptr(&self) -> objc::RawPtr {
9+
objc::RawPtr {
10+
ptr: self.as_raw_ref().ptr.cast(),
11+
}
12+
}
13+
14+
fn as_raw_ref(&self) -> cf::RawRef {
15+
cf::RawRef {
16+
ptr: self.as_raw_ptr().ptr.cast(),
17+
}
18+
}
19+
}
20+
21+
/// At least one of the two methods must be implemented (or else you'll end up with infinite recursion).
22+
pub trait FromOwned: Sized {
23+
unsafe fn from_owned_ptr_unchecked(owned_ptr: objc::OwnedPtr) -> Self {
24+
Self::from_owned_ref_unchecked(owned_ptr.into())
25+
}
26+
27+
unsafe fn from_owned_ref_unchecked(owned_ref: cf::OwnedRef) -> Self {
28+
Self::from_owned_ptr_unchecked(owned_ref.into())
29+
}
30+
}
31+
632
pub(crate) mod objc {
733
use std::ptr::NonNull;
834

@@ -62,10 +88,6 @@ pub(crate) mod objc {
6288
}
6389
}
6490

65-
pub trait AsRawPtr {
66-
fn as_raw(&self) -> RawPtr;
67-
}
68-
6991
pub struct OwnedPtr {
7092
raw: RawPtr,
7193
}
@@ -93,19 +115,20 @@ pub(crate) mod objc {
93115
}
94116
}
95117

96-
impl AsRawPtr for OwnedPtr {
97-
fn as_raw(&self) -> RawPtr {
118+
impl super::AsRaw for OwnedPtr {
119+
fn as_raw_ptr(&self) -> RawPtr {
98120
self.raw
99121
}
100122
}
101123

102124
#[derive(Copy, Clone)]
125+
#[repr(transparent)]
103126
pub struct StaticPtr {
104127
raw: RawPtr,
105128
}
106129

107-
impl AsRawPtr for StaticPtr {
108-
fn as_raw(&self) -> RawPtr {
130+
impl super::AsRaw for StaticPtr {
131+
fn as_raw_ptr(&self) -> RawPtr {
109132
self.raw
110133
}
111134
}
@@ -209,6 +232,12 @@ pub(crate) mod cf {
209232
}
210233
}
211234

235+
impl super::AsRaw for OwnedRef {
236+
fn as_raw_ref(&self) -> RawRef {
237+
self.raw
238+
}
239+
}
240+
212241
impl super::Retain for OwnedRef {
213242
type Owned = Self;
214243
fn retain(&self) -> Self::Owned {
@@ -247,10 +276,18 @@ pub(crate) mod cf {
247276
}
248277
}
249278

250-
impl objc::AsRawPtr for cf::OwnedRef {
251-
fn as_raw(&self) -> objc::RawPtr {
252-
objc::RawPtr {
253-
ptr: self.raw.ptr.cast(),
254-
}
279+
impl From<cf::OwnedRef> for objc::OwnedPtr {
280+
fn from(ptr: cf::OwnedRef) -> Self {
281+
let raw = ptr.as_raw_ptr();
282+
std::mem::forget(ptr);
283+
unsafe { Self::from_owned_raw(raw) }
284+
}
285+
}
286+
287+
impl From<objc::OwnedPtr> for cf::OwnedRef {
288+
fn from(ptr: objc::OwnedPtr) -> Self {
289+
let raw = ptr.as_raw_ref();
290+
std::mem::forget(ptr);
291+
unsafe { Self::from_owned_raw(raw) }
255292
}
256293
}

src/core_media.rs

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ pub trait CMFormatDescriptionInterface: CFTypeInterface {
147147
}
148148

149149
fn media_type(&self) -> CMMediaType {
150-
let self_raw = self.as_raw();
150+
let self_raw = self.as_raw_ref();
151151
unsafe { CMFormatDescriptionGetMediaType(self_raw) }
152152
}
153153
}
@@ -156,6 +156,13 @@ pub struct CMFormatDescription {
156156
ptr: ptr::cf::OwnedRef,
157157
}
158158

159+
impl ptr::AsRaw for CMFormatDescription {
160+
fn as_raw_ref(&self) -> ptr::cf::RawRef {
161+
self.ptr.as_raw_ref()
162+
}
163+
}
164+
165+
impl CFTypeInterface for CMFormatDescription {}
159166
impl CMFormatDescriptionInterface for CMFormatDescription {}
160167
pub trait CMAudioFormatDescriptionInterface: CMFormatDescriptionInterface {}
161168
impl ValidObjCGeneric for CMFormatDescription {}
@@ -164,6 +171,19 @@ pub struct CMAudioFormatDescription {
164171
ptr: ptr::cf::OwnedRef,
165172
}
166173

174+
impl ptr::AsRaw for CMAudioFormatDescription {
175+
fn as_raw_ref(&self) -> ptr::cf::RawRef {
176+
self.ptr.as_raw_ref()
177+
}
178+
}
179+
180+
impl ptr::FromOwned for CMFormatDescription {
181+
unsafe fn from_owned_ref_unchecked(owned_ref: ptr::cf::OwnedRef) -> Self {
182+
Self { ptr: owned_ref }
183+
}
184+
}
185+
186+
impl CFTypeInterface for CMAudioFormatDescription {}
167187
impl CMFormatDescriptionInterface for CMAudioFormatDescription {}
168188
impl CMAudioFormatDescriptionInterface for CMAudioFormatDescription {}
169189

@@ -176,12 +196,12 @@ pub struct CMVideoDimensions {
176196

177197
pub trait CMVideoFormatDescriptionInterface: CMFormatDescriptionInterface {
178198
fn clean_aperture(&self, origin_is_at_top_left: bool) -> CGRect {
179-
let self_raw = self.as_raw();
199+
let self_raw = self.as_raw_ref();
180200
unsafe { CMVideoFormatDescriptionGetCleanAperture(self_raw, origin_is_at_top_left.into()) }
181201
}
182202

183203
fn dimensions(&self) -> CMVideoDimensions {
184-
let self_raw = self.as_raw();
204+
let self_raw = self.as_raw_ref();
185205
unsafe { CMVideoFormatDescriptionGetDimensions(self_raw) }
186206
}
187207
}
@@ -190,6 +210,13 @@ pub struct CMVideoFormatDescription {
190210
ptr: ptr::cf::OwnedRef,
191211
}
192212

213+
impl ptr::AsRaw for CMVideoFormatDescription {
214+
fn as_raw_ref(&self) -> ptr::cf::RawRef {
215+
self.ptr.as_raw_ref()
216+
}
217+
}
218+
219+
impl CFTypeInterface for CMVideoFormatDescription {}
193220
impl CMFormatDescriptionInterface for CMVideoFormatDescription {}
194221
impl CMVideoFormatDescriptionInterface for CMVideoFormatDescription {}
195222

@@ -199,6 +226,13 @@ pub struct CMMuxedFormatDescription {
199226
ptr: ptr::cf::OwnedRef,
200227
}
201228

229+
impl ptr::AsRaw for CMMuxedFormatDescription {
230+
fn as_raw_ref(&self) -> ptr::cf::RawRef {
231+
self.ptr.as_raw_ref()
232+
}
233+
}
234+
235+
impl CFTypeInterface for CMMuxedFormatDescription {}
202236
impl CMFormatDescriptionInterface for CMMuxedFormatDescription {}
203237
impl CMMuxedFormatDescriptionInterface for CMMuxedFormatDescription {}
204238

@@ -208,6 +242,13 @@ pub struct CMMetadataFormatDescription {
208242
ptr: ptr::cf::OwnedRef,
209243
}
210244

245+
impl ptr::AsRaw for CMMetadataFormatDescription {
246+
fn as_raw_ref(&self) -> ptr::cf::RawRef {
247+
self.ptr.as_raw_ref()
248+
}
249+
}
250+
251+
impl CFTypeInterface for CMMetadataFormatDescription {}
211252
impl CMFormatDescriptionInterface for CMMetadataFormatDescription {}
212253
impl CMMetadataFormatDescriptionInterface for CMMetadataFormatDescription {}
213254

@@ -217,6 +258,13 @@ pub struct CMTextFormatDescription {
217258
ptr: ptr::cf::OwnedRef,
218259
}
219260

261+
impl ptr::AsRaw for CMTextFormatDescription {
262+
fn as_raw_ref(&self) -> ptr::cf::RawRef {
263+
self.ptr.as_raw_ref()
264+
}
265+
}
266+
267+
impl CFTypeInterface for CMTextFormatDescription {}
220268
impl CMFormatDescriptionInterface for CMTextFormatDescription {}
221269
impl CMTextFormatDescriptionInterface for CMTextFormatDescription {}
222270

@@ -226,6 +274,13 @@ pub struct CMTimeCodeFormatDescription {
226274
ptr: ptr::cf::OwnedRef,
227275
}
228276

277+
impl ptr::AsRaw for CMTimeCodeFormatDescription {
278+
fn as_raw_ref(&self) -> ptr::cf::RawRef {
279+
self.ptr.as_raw_ref()
280+
}
281+
}
282+
283+
impl CFTypeInterface for CMTimeCodeFormatDescription {}
229284
impl CMFormatDescriptionInterface for CMTimeCodeFormatDescription {}
230285
impl CMTimeCodeFormatDescriptionInterface for CMTimeCodeFormatDescription {}
231286

@@ -235,5 +290,12 @@ pub struct CMClosedCaptionFormatDescription {
235290
ptr: ptr::cf::OwnedRef,
236291
}
237292

293+
impl ptr::AsRaw for CMClosedCaptionFormatDescription {
294+
fn as_raw_ref(&self) -> ptr::cf::RawRef {
295+
self.ptr.as_raw_ref()
296+
}
297+
}
298+
299+
impl CFTypeInterface for CMClosedCaptionFormatDescription {}
238300
impl CMFormatDescriptionInterface for CMClosedCaptionFormatDescription {}
239301
impl CMClosedCaptionFormatDescriptionInterface for CMClosedCaptionFormatDescription {}

0 commit comments

Comments
 (0)