cocoa_foundation/
foundation.rs

1// Copyright 2013 The Servo Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9#![deprecated = "use the objc2-foundation crate instead"]
10#![allow(non_upper_case_globals)]
11
12use crate::base::{id, nil, BOOL, NO, SEL};
13use bitflags::bitflags;
14use block::Block;
15use core::ffi::{c_char, c_double, c_ulong, c_ulonglong, c_void};
16use objc::{class, msg_send, sel, sel_impl};
17use std::ptr;
18
19#[cfg(target_pointer_width = "32")]
20pub type NSInteger = core::ffi::c_int;
21#[cfg(target_pointer_width = "32")]
22pub type NSUInteger = core::ffi::c_uint;
23
24#[cfg(target_pointer_width = "64")]
25pub type NSInteger = core::ffi::c_long;
26#[cfg(target_pointer_width = "64")]
27pub type NSUInteger = core::ffi::c_ulong;
28
29pub const NSIntegerMax: NSInteger = NSInteger::MAX;
30pub const NSNotFound: NSInteger = NSIntegerMax;
31
32const UTF8_ENCODING: usize = 4;
33
34#[cfg(target_os = "macos")]
35mod macos {
36    use crate::base::id;
37    use core_graphics_types::base::CGFloat;
38    use core_graphics_types::geometry::CGRect;
39    use objc::{self, class, msg_send, sel, sel_impl};
40    use std::mem;
41
42    #[repr(C)]
43    #[derive(Copy, Clone)]
44    pub struct NSPoint {
45        pub x: CGFloat,
46        pub y: CGFloat,
47    }
48
49    impl NSPoint {
50        #[inline]
51        pub fn new(x: CGFloat, y: CGFloat) -> NSPoint {
52            NSPoint { x, y }
53        }
54    }
55
56    unsafe impl objc::Encode for NSPoint {
57        fn encode() -> objc::Encoding {
58            let encoding = format!(
59                "{{CGPoint={}{}}}",
60                CGFloat::encode().as_str(),
61                CGFloat::encode().as_str()
62            );
63            unsafe { objc::Encoding::from_str(&encoding) }
64        }
65    }
66
67    #[repr(C)]
68    #[derive(Copy, Clone)]
69    pub struct NSSize {
70        pub width: CGFloat,
71        pub height: CGFloat,
72    }
73
74    impl NSSize {
75        #[inline]
76        pub fn new(width: CGFloat, height: CGFloat) -> NSSize {
77            NSSize { width, height }
78        }
79    }
80
81    unsafe impl objc::Encode for NSSize {
82        fn encode() -> objc::Encoding {
83            let encoding = format!(
84                "{{CGSize={}{}}}",
85                CGFloat::encode().as_str(),
86                CGFloat::encode().as_str()
87            );
88            unsafe { objc::Encoding::from_str(&encoding) }
89        }
90    }
91
92    #[repr(C)]
93    #[derive(Copy, Clone)]
94    pub struct NSRect {
95        pub origin: NSPoint,
96        pub size: NSSize,
97    }
98
99    impl NSRect {
100        #[inline]
101        pub fn new(origin: NSPoint, size: NSSize) -> NSRect {
102            NSRect { origin, size }
103        }
104
105        #[inline]
106        pub fn as_CGRect(&self) -> &CGRect {
107            unsafe { mem::transmute::<&NSRect, &CGRect>(self) }
108        }
109
110        #[inline]
111        pub fn inset(&self, x: CGFloat, y: CGFloat) -> NSRect {
112            unsafe { NSInsetRect(*self, x, y) }
113        }
114    }
115
116    unsafe impl objc::Encode for NSRect {
117        fn encode() -> objc::Encoding {
118            let encoding = format!(
119                "{{CGRect={}{}}}",
120                NSPoint::encode().as_str(),
121                NSSize::encode().as_str()
122            );
123            unsafe { objc::Encoding::from_str(&encoding) }
124        }
125    }
126
127    // Same as CGRectEdge
128    #[repr(u32)]
129    pub enum NSRectEdge {
130        NSRectMinXEdge,
131        NSRectMinYEdge,
132        NSRectMaxXEdge,
133        NSRectMaxYEdge,
134    }
135
136    #[cfg_attr(feature = "link", link(name = "Foundation", kind = "framework"))]
137    extern "C" {
138        fn NSInsetRect(rect: NSRect, x: CGFloat, y: CGFloat) -> NSRect;
139    }
140
141    pub trait NSValue: Sized {
142        unsafe fn valueWithPoint(_: Self, point: NSPoint) -> id {
143            msg_send![class!(NSValue), valueWithPoint: point]
144        }
145
146        unsafe fn valueWithSize(_: Self, size: NSSize) -> id {
147            msg_send![class!(NSValue), valueWithSize: size]
148        }
149    }
150
151    impl NSValue for id {}
152}
153
154#[cfg(target_os = "macos")]
155pub use self::macos::*;
156
157#[repr(C)]
158#[derive(Copy, Clone)]
159pub struct NSRange {
160    pub location: NSUInteger,
161    pub length: NSUInteger,
162}
163
164impl NSRange {
165    #[inline]
166    pub fn new(location: NSUInteger, length: NSUInteger) -> NSRange {
167        NSRange { location, length }
168    }
169}
170
171#[cfg_attr(feature = "link", link(name = "Foundation", kind = "framework"))]
172extern "C" {
173    pub static NSDefaultRunLoopMode: id;
174}
175
176pub trait NSAutoreleasePool: Sized {
177    unsafe fn new(_: Self) -> id {
178        msg_send![class!(NSAutoreleasePool), new]
179    }
180
181    unsafe fn autorelease(self) -> Self;
182    unsafe fn drain(self);
183}
184
185impl NSAutoreleasePool for id {
186    unsafe fn autorelease(self) -> id {
187        msg_send![self, autorelease]
188    }
189
190    unsafe fn drain(self) {
191        msg_send![self, drain]
192    }
193}
194
195#[repr(C)]
196#[derive(Copy, Clone)]
197pub struct NSOperatingSystemVersion {
198    pub majorVersion: NSUInteger,
199    pub minorVersion: NSUInteger,
200    pub patchVersion: NSUInteger,
201}
202
203impl NSOperatingSystemVersion {
204    #[inline]
205    pub fn new(
206        majorVersion: NSUInteger,
207        minorVersion: NSUInteger,
208        patchVersion: NSUInteger,
209    ) -> NSOperatingSystemVersion {
210        NSOperatingSystemVersion {
211            majorVersion,
212            minorVersion,
213            patchVersion,
214        }
215    }
216}
217
218pub trait NSProcessInfo: Sized {
219    unsafe fn processInfo(_: Self) -> id {
220        msg_send![class!(NSProcessInfo), processInfo]
221    }
222
223    unsafe fn systemUptime(self) -> NSTimeInterval;
224    unsafe fn processName(self) -> id;
225    unsafe fn operatingSystemVersion(self) -> NSOperatingSystemVersion;
226    unsafe fn isOperatingSystemAtLeastVersion(self, version: NSOperatingSystemVersion) -> bool;
227}
228
229impl NSProcessInfo for id {
230    unsafe fn processName(self) -> id {
231        msg_send![self, processName]
232    }
233
234    unsafe fn systemUptime(self) -> NSTimeInterval {
235        msg_send![self, systemUptime]
236    }
237
238    unsafe fn operatingSystemVersion(self) -> NSOperatingSystemVersion {
239        msg_send![self, operatingSystemVersion]
240    }
241
242    unsafe fn isOperatingSystemAtLeastVersion(self, version: NSOperatingSystemVersion) -> bool {
243        let res: BOOL = msg_send![self, isOperatingSystemAtLeastVersion: version];
244        res != NO
245    }
246}
247
248pub type NSTimeInterval = c_double;
249
250pub trait NSArray: Sized {
251    unsafe fn array(_: Self) -> id {
252        msg_send![class!(NSArray), array]
253    }
254
255    unsafe fn arrayWithObjects(_: Self, objects: &[id]) -> id {
256        msg_send![class!(NSArray), arrayWithObjects:objects.as_ptr()
257                                    count:objects.len()]
258    }
259
260    unsafe fn arrayWithObject(_: Self, object: id) -> id {
261        msg_send![class!(NSArray), arrayWithObject: object]
262    }
263
264    unsafe fn init(self) -> id;
265
266    unsafe fn count(self) -> NSUInteger;
267
268    unsafe fn arrayByAddingObjectFromArray(self, object: id) -> id;
269    unsafe fn arrayByAddingObjectsFromArray(self, objects: id) -> id;
270    unsafe fn objectAtIndex(self, index: NSUInteger) -> id;
271}
272
273impl NSArray for id {
274    unsafe fn init(self) -> id {
275        msg_send![self, init]
276    }
277
278    unsafe fn count(self) -> NSUInteger {
279        msg_send![self, count]
280    }
281
282    unsafe fn arrayByAddingObjectFromArray(self, object: id) -> id {
283        msg_send![self, arrayByAddingObjectFromArray: object]
284    }
285
286    unsafe fn arrayByAddingObjectsFromArray(self, objects: id) -> id {
287        msg_send![self, arrayByAddingObjectsFromArray: objects]
288    }
289
290    unsafe fn objectAtIndex(self, index: NSUInteger) -> id {
291        msg_send![self, objectAtIndex: index]
292    }
293}
294
295pub trait NSDictionary: Sized {
296    unsafe fn dictionary(_: Self) -> id {
297        msg_send![class!(NSDictionary), dictionary]
298    }
299
300    unsafe fn dictionaryWithContentsOfFile_(_: Self, path: id) -> id {
301        msg_send![class!(NSDictionary), dictionaryWithContentsOfFile: path]
302    }
303
304    unsafe fn dictionaryWithContentsOfURL_(_: Self, aURL: id) -> id {
305        msg_send![class!(NSDictionary), dictionaryWithContentsOfURL: aURL]
306    }
307
308    unsafe fn dictionaryWithDictionary_(_: Self, otherDictionary: id) -> id {
309        msg_send![
310            class!(NSDictionary),
311            dictionaryWithDictionary: otherDictionary
312        ]
313    }
314
315    unsafe fn dictionaryWithObject_forKey_(_: Self, anObject: id, aKey: id) -> id {
316        msg_send![class!(NSDictionary), dictionaryWithObject:anObject forKey:aKey]
317    }
318
319    unsafe fn dictionaryWithObjects_forKeys_(_: Self, objects: id, keys: id) -> id {
320        msg_send![class!(NSDictionary), dictionaryWithObjects:objects forKeys:keys]
321    }
322
323    unsafe fn dictionaryWithObjects_forKeys_count_(
324        _: Self,
325        objects: *const id,
326        keys: *const id,
327        count: NSUInteger,
328    ) -> id {
329        msg_send![class!(NSDictionary), dictionaryWithObjects:objects forKeys:keys count:count]
330    }
331
332    unsafe fn dictionaryWithObjectsAndKeys_(_: Self, firstObject: id) -> id {
333        msg_send![
334            class!(NSDictionary),
335            dictionaryWithObjectsAndKeys: firstObject
336        ]
337    }
338
339    unsafe fn init(self) -> id;
340    unsafe fn initWithContentsOfFile_(self, path: id) -> id;
341    unsafe fn initWithContentsOfURL_(self, aURL: id) -> id;
342    unsafe fn initWithDictionary_(self, otherDictionary: id) -> id;
343    unsafe fn initWithDictionary_copyItems_(self, otherDictionary: id, flag: BOOL) -> id;
344    unsafe fn initWithObjects_forKeys_(self, objects: id, keys: id) -> id;
345    unsafe fn initWithObjects_forKeys_count_(self, objects: id, keys: id, count: NSUInteger) -> id;
346    unsafe fn initWithObjectsAndKeys_(self, firstObject: id) -> id;
347
348    unsafe fn sharedKeySetForKeys_(_: Self, keys: id) -> id {
349        msg_send![class!(NSDictionary), sharedKeySetForKeys: keys]
350    }
351
352    unsafe fn count(self) -> NSUInteger;
353
354    unsafe fn isEqualToDictionary_(self, otherDictionary: id) -> BOOL;
355
356    unsafe fn allKeys(self) -> id;
357    unsafe fn allKeysForObject_(self, anObject: id) -> id;
358    unsafe fn allValues(self) -> id;
359    unsafe fn objectForKey_(self, aKey: id) -> id;
360    unsafe fn objectForKeyedSubscript_(self, key: id) -> id;
361    unsafe fn objectsForKeys_notFoundMarker_(self, keys: id, anObject: id) -> id;
362    unsafe fn valueForKey_(self, key: id) -> id;
363
364    unsafe fn keyEnumerator(self) -> id;
365    unsafe fn objectEnumerator(self) -> id;
366    unsafe fn enumerateKeysAndObjectsUsingBlock_(self, block: *mut Block<(id, id, *mut BOOL), ()>);
367    unsafe fn enumerateKeysAndObjectsWithOptions_usingBlock_(
368        self,
369        opts: NSEnumerationOptions,
370        block: *mut Block<(id, id, *mut BOOL), ()>,
371    );
372
373    unsafe fn keysSortedByValueUsingSelector_(self, comparator: SEL) -> id;
374    unsafe fn keysSortedByValueUsingComparator_(self, cmptr: NSComparator) -> id;
375    unsafe fn keysSortedByValueWithOptions_usingComparator_(
376        self,
377        opts: NSEnumerationOptions,
378        cmptr: NSComparator,
379    ) -> id;
380
381    unsafe fn keysOfEntriesPassingTest_(
382        self,
383        predicate: *mut Block<(id, id, *mut BOOL), BOOL>,
384    ) -> id;
385    unsafe fn keysOfEntriesWithOptions_PassingTest_(
386        self,
387        opts: NSEnumerationOptions,
388        predicate: *mut Block<(id, id, *mut BOOL), BOOL>,
389    ) -> id;
390
391    unsafe fn writeToFile_atomically_(self, path: id, flag: BOOL) -> BOOL;
392    unsafe fn writeToURL_atomically_(self, aURL: id, flag: BOOL) -> BOOL;
393
394    unsafe fn fileCreationDate(self) -> id;
395    unsafe fn fileExtensionHidden(self) -> BOOL;
396    unsafe fn fileGroupOwnerAccountID(self) -> id;
397    unsafe fn fileGroupOwnerAccountName(self) -> id;
398    unsafe fn fileIsAppendOnly(self) -> BOOL;
399    unsafe fn fileIsImmutable(self) -> BOOL;
400    unsafe fn fileModificationDate(self) -> id;
401    unsafe fn fileOwnerAccountID(self) -> id;
402    unsafe fn fileOwnerAccountName(self) -> id;
403    unsafe fn filePosixPermissions(self) -> NSUInteger;
404    unsafe fn fileSize(self) -> c_ulonglong;
405    unsafe fn fileSystemFileNumber(self) -> NSUInteger;
406    unsafe fn fileSystemNumber(self) -> NSInteger;
407    unsafe fn fileType(self) -> id;
408
409    unsafe fn description(self) -> id;
410    unsafe fn descriptionInStringsFileFormat(self) -> id;
411    unsafe fn descriptionWithLocale_(self, locale: id) -> id;
412    unsafe fn descriptionWithLocale_indent_(self, locale: id, indent: NSUInteger) -> id;
413}
414
415impl NSDictionary for id {
416    unsafe fn init(self) -> id {
417        msg_send![self, init]
418    }
419
420    unsafe fn initWithContentsOfFile_(self, path: id) -> id {
421        msg_send![self, initWithContentsOfFile: path]
422    }
423
424    unsafe fn initWithContentsOfURL_(self, aURL: id) -> id {
425        msg_send![self, initWithContentsOfURL: aURL]
426    }
427
428    unsafe fn initWithDictionary_(self, otherDictionary: id) -> id {
429        msg_send![self, initWithDictionary: otherDictionary]
430    }
431
432    unsafe fn initWithDictionary_copyItems_(self, otherDictionary: id, flag: BOOL) -> id {
433        msg_send![self, initWithDictionary:otherDictionary copyItems:flag]
434    }
435
436    unsafe fn initWithObjects_forKeys_(self, objects: id, keys: id) -> id {
437        msg_send![self, initWithObjects:objects forKeys:keys]
438    }
439
440    unsafe fn initWithObjects_forKeys_count_(self, objects: id, keys: id, count: NSUInteger) -> id {
441        msg_send![self, initWithObjects:objects forKeys:keys count:count]
442    }
443
444    unsafe fn initWithObjectsAndKeys_(self, firstObject: id) -> id {
445        msg_send![self, initWithObjectsAndKeys: firstObject]
446    }
447
448    unsafe fn count(self) -> NSUInteger {
449        msg_send![self, count]
450    }
451
452    unsafe fn isEqualToDictionary_(self, otherDictionary: id) -> BOOL {
453        msg_send![self, isEqualToDictionary: otherDictionary]
454    }
455
456    unsafe fn allKeys(self) -> id {
457        msg_send![self, allKeys]
458    }
459
460    unsafe fn allKeysForObject_(self, anObject: id) -> id {
461        msg_send![self, allKeysForObject: anObject]
462    }
463
464    unsafe fn allValues(self) -> id {
465        msg_send![self, allValues]
466    }
467
468    unsafe fn objectForKey_(self, aKey: id) -> id {
469        msg_send![self, objectForKey: aKey]
470    }
471
472    unsafe fn objectForKeyedSubscript_(self, key: id) -> id {
473        msg_send![self, objectForKeyedSubscript: key]
474    }
475
476    unsafe fn objectsForKeys_notFoundMarker_(self, keys: id, anObject: id) -> id {
477        msg_send![self, objectsForKeys:keys notFoundMarker:anObject]
478    }
479
480    unsafe fn valueForKey_(self, key: id) -> id {
481        msg_send![self, valueForKey: key]
482    }
483
484    unsafe fn keyEnumerator(self) -> id {
485        msg_send![self, keyEnumerator]
486    }
487
488    unsafe fn objectEnumerator(self) -> id {
489        msg_send![self, objectEnumerator]
490    }
491
492    unsafe fn enumerateKeysAndObjectsUsingBlock_(self, block: *mut Block<(id, id, *mut BOOL), ()>) {
493        msg_send![self, enumerateKeysAndObjectsUsingBlock: block]
494    }
495
496    unsafe fn enumerateKeysAndObjectsWithOptions_usingBlock_(
497        self,
498        opts: NSEnumerationOptions,
499        block: *mut Block<(id, id, *mut BOOL), ()>,
500    ) {
501        msg_send![self, enumerateKeysAndObjectsWithOptions:opts usingBlock:block]
502    }
503
504    unsafe fn keysSortedByValueUsingSelector_(self, comparator: SEL) -> id {
505        msg_send![self, keysSortedByValueUsingSelector: comparator]
506    }
507
508    unsafe fn keysSortedByValueUsingComparator_(self, cmptr: NSComparator) -> id {
509        msg_send![self, keysSortedByValueUsingComparator: cmptr]
510    }
511
512    unsafe fn keysSortedByValueWithOptions_usingComparator_(
513        self,
514        opts: NSEnumerationOptions,
515        cmptr: NSComparator,
516    ) -> id {
517        let rv: id = msg_send![self, keysSortedByValueWithOptions:opts usingComparator:cmptr];
518        rv
519    }
520
521    unsafe fn keysOfEntriesPassingTest_(
522        self,
523        predicate: *mut Block<(id, id, *mut BOOL), BOOL>,
524    ) -> id {
525        msg_send![self, keysOfEntriesPassingTest: predicate]
526    }
527
528    unsafe fn keysOfEntriesWithOptions_PassingTest_(
529        self,
530        opts: NSEnumerationOptions,
531        predicate: *mut Block<(id, id, *mut BOOL), BOOL>,
532    ) -> id {
533        msg_send![self, keysOfEntriesWithOptions:opts PassingTest:predicate]
534    }
535
536    unsafe fn writeToFile_atomically_(self, path: id, flag: BOOL) -> BOOL {
537        msg_send![self, writeToFile:path atomically:flag]
538    }
539
540    unsafe fn writeToURL_atomically_(self, aURL: id, flag: BOOL) -> BOOL {
541        msg_send![self, writeToURL:aURL atomically:flag]
542    }
543
544    unsafe fn fileCreationDate(self) -> id {
545        msg_send![self, fileCreationDate]
546    }
547
548    unsafe fn fileExtensionHidden(self) -> BOOL {
549        msg_send![self, fileExtensionHidden]
550    }
551
552    unsafe fn fileGroupOwnerAccountID(self) -> id {
553        msg_send![self, fileGroupOwnerAccountID]
554    }
555
556    unsafe fn fileGroupOwnerAccountName(self) -> id {
557        msg_send![self, fileGroupOwnerAccountName]
558    }
559
560    unsafe fn fileIsAppendOnly(self) -> BOOL {
561        msg_send![self, fileIsAppendOnly]
562    }
563
564    unsafe fn fileIsImmutable(self) -> BOOL {
565        msg_send![self, fileIsImmutable]
566    }
567
568    unsafe fn fileModificationDate(self) -> id {
569        msg_send![self, fileModificationDate]
570    }
571
572    unsafe fn fileOwnerAccountID(self) -> id {
573        msg_send![self, fileOwnerAccountID]
574    }
575
576    unsafe fn fileOwnerAccountName(self) -> id {
577        msg_send![self, fileOwnerAccountName]
578    }
579
580    unsafe fn filePosixPermissions(self) -> NSUInteger {
581        msg_send![self, filePosixPermissions]
582    }
583
584    unsafe fn fileSize(self) -> c_ulonglong {
585        msg_send![self, fileSize]
586    }
587
588    unsafe fn fileSystemFileNumber(self) -> NSUInteger {
589        msg_send![self, fileSystemFileNumber]
590    }
591
592    unsafe fn fileSystemNumber(self) -> NSInteger {
593        msg_send![self, fileSystemNumber]
594    }
595
596    unsafe fn fileType(self) -> id {
597        msg_send![self, fileType]
598    }
599
600    unsafe fn description(self) -> id {
601        msg_send![self, description]
602    }
603
604    unsafe fn descriptionInStringsFileFormat(self) -> id {
605        msg_send![self, descriptionInStringsFileFormat]
606    }
607
608    unsafe fn descriptionWithLocale_(self, locale: id) -> id {
609        msg_send![self, descriptionWithLocale: locale]
610    }
611
612    unsafe fn descriptionWithLocale_indent_(self, locale: id, indent: NSUInteger) -> id {
613        msg_send![self, descriptionWithLocale:locale indent:indent]
614    }
615}
616
617bitflags! {
618    #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
619    pub struct NSEnumerationOptions: c_ulonglong {
620        const NSEnumerationConcurrent = 1 << 0;
621        const NSEnumerationReverse = 1 << 1;
622    }
623}
624
625pub type NSComparator = *mut Block<(id, id), NSComparisonResult>;
626
627#[repr(isize)]
628#[derive(Clone, Copy, Debug, Eq, PartialEq)]
629pub enum NSComparisonResult {
630    NSOrderedAscending = -1,
631    NSOrderedSame = 0,
632    NSOrderedDescending = 1,
633}
634
635pub trait NSString: Sized {
636    unsafe fn alloc(_: Self) -> id {
637        msg_send![class!(NSString), alloc]
638    }
639
640    unsafe fn stringByAppendingString_(self, other: id) -> id;
641    unsafe fn init_str(self, string: &str) -> Self;
642    unsafe fn UTF8String(self) -> *const c_char;
643    unsafe fn len(self) -> usize;
644    unsafe fn isEqualToString(self, string: &str) -> bool;
645    unsafe fn substringWithRange(self, range: NSRange) -> id;
646}
647
648impl NSString for id {
649    unsafe fn isEqualToString(self, other: &str) -> bool {
650        let other = NSString::alloc(nil).init_str(other);
651        let rv: BOOL = msg_send![self, isEqualToString: other];
652        let _: () = msg_send![other, release];
653        rv != NO
654    }
655
656    unsafe fn stringByAppendingString_(self, other: id) -> id {
657        msg_send![self, stringByAppendingString: other]
658    }
659
660    unsafe fn init_str(self, string: &str) -> id {
661        msg_send![self,
662                  initWithBytes:string.as_ptr() as *const c_void
663                      length:string.len()
664                      encoding:UTF8_ENCODING]
665    }
666
667    unsafe fn len(self) -> usize {
668        msg_send![self, lengthOfBytesUsingEncoding: UTF8_ENCODING]
669    }
670
671    unsafe fn UTF8String(self) -> *const c_char {
672        msg_send![self, UTF8String]
673    }
674
675    unsafe fn substringWithRange(self, range: NSRange) -> id {
676        msg_send![self, substringWithRange: range]
677    }
678}
679
680pub trait NSDate: Sized {
681    unsafe fn distantPast(_: Self) -> id {
682        msg_send![class!(NSDate), distantPast]
683    }
684
685    unsafe fn distantFuture(_: Self) -> id {
686        msg_send![class!(NSDate), distantFuture]
687    }
688}
689
690impl NSDate for id {}
691
692#[repr(C)]
693struct NSFastEnumerationState {
694    pub state: c_ulong,
695    pub items_ptr: *mut id,
696    pub mutations_ptr: *mut c_ulong,
697    pub extra: [c_ulong; 5],
698}
699
700const NS_FAST_ENUM_BUF_SIZE: usize = 16;
701
702pub struct NSFastIterator {
703    state: NSFastEnumerationState,
704    buffer: [id; NS_FAST_ENUM_BUF_SIZE],
705    mut_val: Option<c_ulong>,
706    len: usize,
707    idx: usize,
708    object: id,
709}
710
711impl Iterator for NSFastIterator {
712    type Item = id;
713
714    fn next(&mut self) -> Option<id> {
715        if self.idx >= self.len {
716            self.len = unsafe {
717                msg_send![self.object, countByEnumeratingWithState:&mut self.state objects:self.buffer.as_mut_ptr() count:NS_FAST_ENUM_BUF_SIZE]
718            };
719            self.idx = 0;
720        }
721
722        let new_mut = unsafe { *self.state.mutations_ptr };
723
724        if let Some(old_mut) = self.mut_val {
725            assert!(
726                old_mut == new_mut,
727                "The collection was mutated while being enumerated"
728            );
729        }
730
731        if self.idx < self.len {
732            let object = unsafe { *self.state.items_ptr.add(self.idx) };
733            self.mut_val = Some(new_mut);
734            self.idx += 1;
735            Some(object)
736        } else {
737            None
738        }
739    }
740}
741
742pub trait NSFastEnumeration: Sized {
743    unsafe fn iter(self) -> NSFastIterator;
744}
745
746impl NSFastEnumeration for id {
747    unsafe fn iter(self) -> NSFastIterator {
748        NSFastIterator {
749            state: NSFastEnumerationState {
750                state: 0,
751                items_ptr: ptr::null_mut(),
752                mutations_ptr: ptr::null_mut(),
753                extra: [0; 5],
754            },
755            buffer: [nil; NS_FAST_ENUM_BUF_SIZE],
756            mut_val: None,
757            len: 0,
758            idx: 0,
759            object: self,
760        }
761    }
762}
763
764pub trait NSRunLoop: Sized {
765    unsafe fn currentRunLoop() -> Self;
766
767    unsafe fn performSelector_target_argument_order_modes_(
768        self,
769        aSelector: SEL,
770        target: id,
771        anArgument: id,
772        order: NSUInteger,
773        modes: id,
774    );
775}
776
777impl NSRunLoop for id {
778    unsafe fn currentRunLoop() -> id {
779        msg_send![class!(NSRunLoop), currentRunLoop]
780    }
781
782    unsafe fn performSelector_target_argument_order_modes_(
783        self,
784        aSelector: SEL,
785        target: id,
786        anArgument: id,
787        order: NSUInteger,
788        modes: id,
789    ) {
790        msg_send![self, performSelector:aSelector
791                                 target:target
792                               argument:anArgument
793                                  order:order
794                                  modes:modes]
795    }
796}
797
798bitflags! {
799    #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
800    pub struct NSURLBookmarkCreationOptions: NSUInteger {
801        const NSURLBookmarkCreationPreferFileIDResolution = 1 << 8;
802        const NSURLBookmarkCreationMinimalBookmark = 1 << 9;
803        const NSURLBookmarkCreationSuitableForBookmarkFile = 1 << 10;
804        const NSURLBookmarkCreationWithSecurityScope = 1 << 11;
805        const NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess = 1 << 12;
806    }
807}
808
809pub type NSURLBookmarkFileCreationOptions = NSURLBookmarkCreationOptions;
810
811bitflags! {
812    #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
813    pub struct NSURLBookmarkResolutionOptions: NSUInteger {
814        const NSURLBookmarkResolutionWithoutUI = 1 << 8;
815        const NSURLBookmarkResolutionWithoutMounting = 1 << 9;
816        const NSURLBookmarkResolutionWithSecurityScope = 1 << 10;
817    }
818}
819
820pub trait NSURL: Sized {
821    unsafe fn alloc(_: Self) -> id;
822
823    unsafe fn URLWithString_(_: Self, string: id) -> id;
824    unsafe fn initWithString_(self, string: id) -> id;
825    unsafe fn URLWithString_relativeToURL_(_: Self, string: id, url: id) -> id;
826    unsafe fn initWithString_relativeToURL_(self, string: id, url: id) -> id;
827    unsafe fn fileURLWithPath_isDirectory_(_: Self, path: id, is_dir: BOOL) -> id;
828    unsafe fn initFileURLWithPath_isDirectory_(self, path: id, is_dir: BOOL) -> id;
829    unsafe fn fileURLWithPath_relativeToURL_(_: Self, path: id, url: id) -> id;
830    unsafe fn initFileURLWithPath_relativeToURL_(self, path: id, url: id) -> id;
831    unsafe fn fileURLWithPath_isDirectory_relativeToURL_(
832        _: Self,
833        path: id,
834        is_dir: BOOL,
835        url: id,
836    ) -> id;
837    unsafe fn initFileURLWithPath_isDirectory_relativeToURL_(
838        self,
839        path: id,
840        is_dir: BOOL,
841        url: id,
842    ) -> id;
843    unsafe fn fileURLWithPath_(_: Self, path: id) -> id;
844    unsafe fn initFileURLWithPath_(self, path: id) -> id;
845    unsafe fn fileURLWithPathComponents_(
846        _: Self,
847        path_components: id, /* (NSArray<NSString*>*) */
848    ) -> id;
849    unsafe fn URLByResolvingAliasFileAtURL_options_error_(
850        _: Self,
851        url: id,
852        options: NSURLBookmarkResolutionOptions,
853        error: *mut id, /* (NSError _Nullable) */
854    ) -> id;
855    unsafe fn URLByResolvingBookmarkData_options_relativeToURL_bookmarkDataIsStale_error_(
856        _: Self,
857        data: id, /* (NSData) */
858        options: NSURLBookmarkResolutionOptions,
859        relative_to_url: id,
860        is_stale: *mut BOOL,
861        error: *mut id, /* (NSError _Nullable) */
862    ) -> id;
863    unsafe fn initByResolvingBookmarkData_options_relativeToURL_bookmarkDataIsStale_error_(
864        self,
865        data: id, /* (NSData) */
866        options: NSURLBookmarkResolutionOptions,
867        relative_to_url: id,
868        is_stale: *mut BOOL,
869        error: *mut id, /* (NSError _Nullable) */
870    ) -> id;
871    // unsafe fn fileURLWithFileSystemRepresentation_isDirectory_relativeToURL_
872    // unsafe fn getFileSystemRepresentation_maxLength_
873    // unsafe fn initFileURLWithFileSystemRepresentation_isDirectory_relativeToURL_
874    unsafe fn absoluteURLWithDataRepresentation_relativeToURL_(
875        _: Self,
876        data: id, /* (NSData) */
877        url: id,
878    ) -> id;
879    unsafe fn initAbsoluteURLWithDataRepresentation_relativeToURL_(
880        self,
881        data: id, /* (NSData) */
882        url: id,
883    ) -> id;
884    unsafe fn URLWithDataRepresentation_relativeToURL_(
885        _: Self,
886        data: id, /* (NSData) */
887        url: id,
888    ) -> id;
889    unsafe fn initWithDataRepresentation_relativeToURL_(
890        self,
891        data: id, /* (NSData) */
892        url: id,
893    ) -> id;
894    unsafe fn dataRepresentation(self) -> id /* (NSData) */;
895
896    unsafe fn isEqual_(self, id: id) -> BOOL;
897
898    unsafe fn checkResourceIsReachableAndReturnError_(
899        self,
900        error: id, /* (NSError _Nullable) */
901    ) -> BOOL;
902    unsafe fn isFileReferenceURL(self) -> BOOL;
903    unsafe fn isFileURL(self) -> BOOL;
904
905    unsafe fn absoluteString(self) -> id /* (NSString) */;
906    unsafe fn absoluteURL(self) -> id /* (NSURL) */;
907    unsafe fn baseURL(self) -> id /* (NSURL) */;
908    // unsafe fn fileSystemRepresentation
909    unsafe fn fragment(self) -> id /* (NSString) */;
910    unsafe fn host(self) -> id /* (NSString) */;
911    unsafe fn lastPathComponent(self) -> id /* (NSString) */;
912    unsafe fn parameterString(self) -> id /* (NSString) */;
913    unsafe fn password(self) -> id /* (NSString) */;
914    unsafe fn path(self) -> id /* (NSString) */;
915    unsafe fn pathComponents(self) -> id /* (NSArray<NSString*>) */;
916    unsafe fn pathExtension(self) -> id /* (NSString) */;
917    unsafe fn port(self) -> id /* (NSNumber) */;
918    unsafe fn query(self) -> id /* (NSString) */;
919    unsafe fn relativePath(self) -> id /* (NSString) */;
920    unsafe fn relativeString(self) -> id /* (NSString) */;
921    unsafe fn resourceSpecifier(self) -> id /* (NSString) */;
922    unsafe fn scheme(self) -> id /* (NSString) */;
923    unsafe fn standardizedURL(self) -> id /* (NSURL) */;
924    unsafe fn user(self) -> id /* (NSString) */;
925
926    // unsafe fn resourceValuesForKeys_error_
927    // unsafe fn getResourceValue_forKey_error_
928    // unsafe fn setResourceValue_forKey_error_
929    // unsafe fn setResourceValues_error_
930    // unsafe fn removeAllCachedResourceValues
931    // unsafe fn removeCachedResourceValueForKey_
932    // unsafe fn setTemporaryResourceValue_forKey_
933    unsafe fn NSURLResourceKey(self) -> id /* (NSString) */;
934
935    unsafe fn filePathURL(self) -> id;
936    unsafe fn fileReferenceURL(self) -> id;
937    unsafe fn URLByAppendingPathComponent_(self, path_component: id /* (NSString) */) -> id;
938    unsafe fn URLByAppendingPathComponent_isDirectory_(
939        self,
940        path_component: id, /* (NSString) */
941        is_dir: BOOL,
942    ) -> id;
943    unsafe fn URLByAppendingPathExtension_(self, extension: id /* (NSString) */) -> id;
944    unsafe fn URLByDeletingLastPathComponent(self) -> id;
945    unsafe fn URLByDeletingPathExtension(self) -> id;
946    unsafe fn URLByResolvingSymlinksInPath(self) -> id;
947    unsafe fn URLByStandardizingPath(self) -> id;
948    unsafe fn hasDirectoryPath(self) -> BOOL;
949
950    unsafe fn bookmarkDataWithContentsOfURL_error_(
951        _: Self,
952        url: id,
953        error: id, /* (NSError _Nullable) */
954    ) -> id /* (NSData) */;
955    unsafe fn bookmarkDataWithOptions_includingResourceValuesForKeys_relativeToURL_error_(
956        self,
957        options: NSURLBookmarkCreationOptions,
958        resource_value_for_keys: id, /* (NSArray<NSURLResourceKey>) */
959        relative_to_url: id,
960        error: id, /* (NSError _Nullable) */
961    ) -> id /* (NSData) */;
962    // unsafe fn resourceValuesForKeys_fromBookmarkData_
963    unsafe fn writeBookmarkData_toURL_options_error_(
964        _: Self,
965        data: id, /* (NSData) */
966        to_url: id,
967        options: NSURLBookmarkFileCreationOptions,
968        error: id, /* (NSError _Nullable) */
969    ) -> id;
970    unsafe fn startAccessingSecurityScopedResource(self) -> BOOL;
971    unsafe fn stopAccessingSecurityScopedResource(self);
972    unsafe fn NSURLBookmarkFileCreationOptions(self) -> NSURLBookmarkFileCreationOptions;
973    unsafe fn NSURLBookmarkCreationOptions(self) -> NSURLBookmarkCreationOptions;
974    unsafe fn NSURLBookmarkResolutionOptions(self) -> NSURLBookmarkResolutionOptions;
975
976    // unsafe fn checkPromisedItemIsReachableAndReturnError_
977    // unsafe fn getPromisedItemResourceValue_forKey_error_
978    // unsafe fn promisedItemResourceValuesForKeys_error_
979
980    // unsafe fn URLFromPasteboard_
981    // unsafe fn writeToPasteboard_
982}
983
984impl NSURL for id {
985    unsafe fn alloc(_: Self) -> id {
986        msg_send![class!(NSURL), alloc]
987    }
988
989    unsafe fn URLWithString_(_: Self, string: id) -> id {
990        msg_send![class!(NSURL), URLWithString: string]
991    }
992    unsafe fn initWithString_(self, string: id) -> id {
993        msg_send![self, initWithString: string]
994    }
995    unsafe fn URLWithString_relativeToURL_(_: Self, string: id, url: id) -> id {
996        msg_send![class!(NSURL), URLWithString: string relativeToURL:url]
997    }
998    unsafe fn initWithString_relativeToURL_(self, string: id, url: id) -> id {
999        msg_send![self, initWithString:string relativeToURL:url]
1000    }
1001    unsafe fn fileURLWithPath_isDirectory_(_: Self, path: id, is_dir: BOOL) -> id {
1002        msg_send![class!(NSURL), fileURLWithPath:path isDirectory:is_dir]
1003    }
1004    unsafe fn initFileURLWithPath_isDirectory_(self, path: id, is_dir: BOOL) -> id {
1005        msg_send![self, initFileURLWithPath:path isDirectory:is_dir]
1006    }
1007    unsafe fn fileURLWithPath_relativeToURL_(_: Self, path: id, url: id) -> id {
1008        msg_send![class!(NSURL), fileURLWithPath:path relativeToURL:url]
1009    }
1010    unsafe fn initFileURLWithPath_relativeToURL_(self, path: id, url: id) -> id {
1011        msg_send![self, initFileURLWithPath:path relativeToURL:url]
1012    }
1013    unsafe fn fileURLWithPath_isDirectory_relativeToURL_(
1014        _: Self,
1015        path: id,
1016        is_dir: BOOL,
1017        url: id,
1018    ) -> id {
1019        msg_send![class!(NSURL), fileURLWithPath:path isDirectory:is_dir relativeToURL:url]
1020    }
1021    unsafe fn initFileURLWithPath_isDirectory_relativeToURL_(
1022        self,
1023        path: id,
1024        is_dir: BOOL,
1025        url: id,
1026    ) -> id {
1027        msg_send![self, initFileURLWithPath:path isDirectory:is_dir relativeToURL:url]
1028    }
1029    unsafe fn fileURLWithPath_(_: Self, path: id) -> id {
1030        msg_send![class!(NSURL), fileURLWithPath: path]
1031    }
1032    unsafe fn initFileURLWithPath_(self, path: id) -> id {
1033        msg_send![self, initFileURLWithPath: path]
1034    }
1035    unsafe fn fileURLWithPathComponents_(
1036        _: Self,
1037        path_components: id, /* (NSArray<NSString*>*) */
1038    ) -> id {
1039        msg_send![class!(NSURL), fileURLWithPathComponents: path_components]
1040    }
1041    unsafe fn URLByResolvingAliasFileAtURL_options_error_(
1042        _: Self,
1043        url: id,
1044        options: NSURLBookmarkResolutionOptions,
1045        error: *mut id, /* (NSError _Nullable) */
1046    ) -> id {
1047        msg_send![class!(NSURL), URLByResolvingAliasFileAtURL:url options:options error:error]
1048    }
1049    unsafe fn URLByResolvingBookmarkData_options_relativeToURL_bookmarkDataIsStale_error_(
1050        _: Self,
1051        data: id, /* (NSData) */
1052        options: NSURLBookmarkResolutionOptions,
1053        relative_to_url: id,
1054        is_stale: *mut BOOL,
1055        error: *mut id, /* (NSError _Nullable) */
1056    ) -> id {
1057        msg_send![class!(NSURL), URLByResolvingBookmarkData:data options:options relativeToURL:relative_to_url bookmarkDataIsStale:is_stale error:error]
1058    }
1059    unsafe fn initByResolvingBookmarkData_options_relativeToURL_bookmarkDataIsStale_error_(
1060        self,
1061        data: id, /* (NSData) */
1062        options: NSURLBookmarkResolutionOptions,
1063        relative_to_url: id,
1064        is_stale: *mut BOOL,
1065        error: *mut id, /* (NSError _Nullable) */
1066    ) -> id {
1067        msg_send![self, initByResolvingBookmarkData:data options:options relativeToURL:relative_to_url bookmarkDataIsStale:is_stale error:error]
1068    }
1069    // unsafe fn fileURLWithFileSystemRepresentation_isDirectory_relativeToURL_
1070    // unsafe fn getFileSystemRepresentation_maxLength_
1071    // unsafe fn initFileURLWithFileSystemRepresentation_isDirectory_relativeToURL_
1072    unsafe fn absoluteURLWithDataRepresentation_relativeToURL_(
1073        _: Self,
1074        data: id, /* (NSData) */
1075        url: id,
1076    ) -> id {
1077        msg_send![class!(NSURL), absoluteURLWithDataRepresentation:data relativeToURL:url]
1078    }
1079    unsafe fn initAbsoluteURLWithDataRepresentation_relativeToURL_(
1080        self,
1081        data: id, /* (NSData) */
1082        url: id,
1083    ) -> id {
1084        msg_send![self, initAbsoluteURLWithDataRepresentation:data relativeToURL:url]
1085    }
1086    unsafe fn URLWithDataRepresentation_relativeToURL_(
1087        _: Self,
1088        data: id, /* (NSData) */
1089        url: id,
1090    ) -> id {
1091        msg_send![class!(NSURL), URLWithDataRepresentation:data relativeToURL:url]
1092    }
1093    unsafe fn initWithDataRepresentation_relativeToURL_(
1094        self,
1095        data: id, /* (NSData) */
1096        url: id,
1097    ) -> id {
1098        msg_send![self, initWithDataRepresentation:data relativeToURL:url]
1099    }
1100    unsafe fn dataRepresentation(self) -> id /* (NSData) */ {
1101        msg_send![self, dataRepresentation]
1102    }
1103
1104    unsafe fn isEqual_(self, id: id) -> BOOL {
1105        msg_send![self, isEqual: id]
1106    }
1107
1108    unsafe fn checkResourceIsReachableAndReturnError_(
1109        self,
1110        error: id, /* (NSError _Nullable) */
1111    ) -> BOOL {
1112        msg_send![self, checkResourceIsReachableAndReturnError: error]
1113    }
1114    unsafe fn isFileReferenceURL(self) -> BOOL {
1115        msg_send![self, isFileReferenceURL]
1116    }
1117    unsafe fn isFileURL(self) -> BOOL {
1118        msg_send![self, isFileURL]
1119    }
1120
1121    unsafe fn absoluteString(self) -> id /* (NSString) */ {
1122        msg_send![self, absoluteString]
1123    }
1124    unsafe fn absoluteURL(self) -> id /* (NSURL) */ {
1125        msg_send![self, absoluteURL]
1126    }
1127    unsafe fn baseURL(self) -> id /* (NSURL) */ {
1128        msg_send![self, baseURL]
1129    }
1130    // unsafe fn fileSystemRepresentation
1131    unsafe fn fragment(self) -> id /* (NSString) */ {
1132        msg_send![self, fragment]
1133    }
1134    unsafe fn host(self) -> id /* (NSString) */ {
1135        msg_send![self, host]
1136    }
1137    unsafe fn lastPathComponent(self) -> id /* (NSString) */ {
1138        msg_send![self, lastPathComponent]
1139    }
1140    unsafe fn parameterString(self) -> id /* (NSString) */ {
1141        msg_send![self, parameterString]
1142    }
1143    unsafe fn password(self) -> id /* (NSString) */ {
1144        msg_send![self, password]
1145    }
1146    unsafe fn path(self) -> id /* (NSString) */ {
1147        msg_send![self, path]
1148    }
1149    unsafe fn pathComponents(self) -> id /* (NSArray<NSString*>) */ {
1150        msg_send![self, pathComponents]
1151    }
1152    unsafe fn pathExtension(self) -> id /* (NSString) */ {
1153        msg_send![self, pathExtension]
1154    }
1155    unsafe fn port(self) -> id /* (NSNumber) */ {
1156        msg_send![self, port]
1157    }
1158    unsafe fn query(self) -> id /* (NSString) */ {
1159        msg_send![self, query]
1160    }
1161    unsafe fn relativePath(self) -> id /* (NSString) */ {
1162        msg_send![self, relativePath]
1163    }
1164    unsafe fn relativeString(self) -> id /* (NSString) */ {
1165        msg_send![self, relativeString]
1166    }
1167    unsafe fn resourceSpecifier(self) -> id /* (NSString) */ {
1168        msg_send![self, resourceSpecifier]
1169    }
1170    unsafe fn scheme(self) -> id /* (NSString) */ {
1171        msg_send![self, scheme]
1172    }
1173    unsafe fn standardizedURL(self) -> id /* (NSURL) */ {
1174        msg_send![self, standardizedURL]
1175    }
1176    unsafe fn user(self) -> id /* (NSString) */ {
1177        msg_send![self, user]
1178    }
1179
1180    // unsafe fn resourceValuesForKeys_error_
1181    // unsafe fn getResourceValue_forKey_error_
1182    // unsafe fn setResourceValue_forKey_error_
1183    // unsafe fn setResourceValues_error_
1184    // unsafe fn removeAllCachedResourceValues
1185    // unsafe fn removeCachedResourceValueForKey_
1186    // unsafe fn setTemporaryResourceValue_forKey_
1187    unsafe fn NSURLResourceKey(self) -> id /* (NSString) */ {
1188        msg_send![self, NSURLResourceKey]
1189    }
1190
1191    unsafe fn filePathURL(self) -> id {
1192        msg_send![self, filePathURL]
1193    }
1194    unsafe fn fileReferenceURL(self) -> id {
1195        msg_send![self, fileReferenceURL]
1196    }
1197    unsafe fn URLByAppendingPathComponent_(self, path_component: id /* (NSString) */) -> id {
1198        msg_send![self, URLByAppendingPathComponent: path_component]
1199    }
1200    unsafe fn URLByAppendingPathComponent_isDirectory_(
1201        self,
1202        path_component: id, /* (NSString) */
1203        is_dir: BOOL,
1204    ) -> id {
1205        msg_send![self, URLByAppendingPathComponent:path_component isDirectory:is_dir]
1206    }
1207    unsafe fn URLByAppendingPathExtension_(self, extension: id /* (NSString) */) -> id {
1208        msg_send![self, URLByAppendingPathExtension: extension]
1209    }
1210    unsafe fn URLByDeletingLastPathComponent(self) -> id {
1211        msg_send![self, URLByDeletingLastPathComponent]
1212    }
1213    unsafe fn URLByDeletingPathExtension(self) -> id {
1214        msg_send![self, URLByDeletingPathExtension]
1215    }
1216    unsafe fn URLByResolvingSymlinksInPath(self) -> id {
1217        msg_send![self, URLByResolvingSymlinksInPath]
1218    }
1219    unsafe fn URLByStandardizingPath(self) -> id {
1220        msg_send![self, URLByStandardizingPath]
1221    }
1222    unsafe fn hasDirectoryPath(self) -> BOOL {
1223        msg_send![self, hasDirectoryPath]
1224    }
1225
1226    unsafe fn bookmarkDataWithContentsOfURL_error_(
1227        _: Self,
1228        url: id,
1229        error: id, /* (NSError _Nullable) */
1230    ) -> id /* (NSData) */ {
1231        msg_send![class!(NSURL), bookmarkDataWithContentsOfURL:url error:error]
1232    }
1233    unsafe fn bookmarkDataWithOptions_includingResourceValuesForKeys_relativeToURL_error_(
1234        self,
1235        options: NSURLBookmarkCreationOptions,
1236        resource_value_for_keys: id, /* (NSArray<NSURLResourceKey>) */
1237        relative_to_url: id,
1238        error: id, /* (NSError _Nullable) */
1239    ) -> id /* (NSData) */ {
1240        msg_send![self, bookmarkDataWithOptions:options includingResourceValuesForKeys:resource_value_for_keys relativeToURL:relative_to_url error:error]
1241    }
1242    // unsafe fn resourceValuesForKeys_fromBookmarkData_
1243    unsafe fn writeBookmarkData_toURL_options_error_(
1244        _: Self,
1245        data: id, /* (NSData) */
1246        to_url: id,
1247        options: NSURLBookmarkFileCreationOptions,
1248        error: id, /* (NSError _Nullable) */
1249    ) -> id {
1250        msg_send![class!(NSURL), writeBookmarkData:data toURL:to_url options:options error:error]
1251    }
1252    unsafe fn startAccessingSecurityScopedResource(self) -> BOOL {
1253        msg_send![self, startAccessingSecurityScopedResource]
1254    }
1255    unsafe fn stopAccessingSecurityScopedResource(self) {
1256        msg_send![self, stopAccessingSecurityScopedResource]
1257    }
1258    unsafe fn NSURLBookmarkFileCreationOptions(self) -> NSURLBookmarkFileCreationOptions {
1259        msg_send![self, NSURLBookmarkFileCreationOptions]
1260    }
1261    unsafe fn NSURLBookmarkCreationOptions(self) -> NSURLBookmarkCreationOptions {
1262        msg_send![self, NSURLBookmarkCreationOptions]
1263    }
1264    unsafe fn NSURLBookmarkResolutionOptions(self) -> NSURLBookmarkResolutionOptions {
1265        msg_send![self, NSURLBookmarkResolutionOptions]
1266    }
1267
1268    // unsafe fn checkPromisedItemIsReachableAndReturnError_
1269    // unsafe fn getPromisedItemResourceValue_forKey_error_
1270    // unsafe fn promisedItemResourceValuesForKeys_error_
1271
1272    // unsafe fn URLFromPasteboard_
1273    // unsafe fn writeToPasteboard_
1274}
1275
1276pub trait NSBundle: Sized {
1277    unsafe fn mainBundle() -> Self;
1278
1279    unsafe fn loadNibNamed_owner_topLevelObjects_(
1280        self,
1281        name: id, /* NSString */
1282        owner: id,
1283        topLevelObjects: *mut id, /* NSArray */
1284    ) -> BOOL;
1285
1286    unsafe fn bundleIdentifier(self) -> id /* NSString */;
1287
1288    unsafe fn resourcePath(self) -> id /* NSString */;
1289}
1290
1291impl NSBundle for id {
1292    unsafe fn mainBundle() -> id {
1293        msg_send![class!(NSBundle), mainBundle]
1294    }
1295
1296    unsafe fn loadNibNamed_owner_topLevelObjects_(
1297        self,
1298        name: id, /* NSString */
1299        owner: id,
1300        topLevelObjects: *mut id, /* NSArray* */
1301    ) -> BOOL {
1302        msg_send![self, loadNibNamed:name
1303                               owner:owner
1304                     topLevelObjects:topLevelObjects]
1305    }
1306
1307    unsafe fn bundleIdentifier(self) -> id /* NSString */ {
1308        msg_send![self, bundleIdentifier]
1309    }
1310
1311    unsafe fn resourcePath(self) -> id /* NSString */ {
1312        msg_send![self, resourcePath]
1313    }
1314}
1315
1316pub trait NSData: Sized {
1317    unsafe fn data(_: Self) -> id {
1318        msg_send![class!(NSData), data]
1319    }
1320
1321    unsafe fn dataWithBytes_length_(_: Self, bytes: *const c_void, length: NSUInteger) -> id {
1322        msg_send![class!(NSData), dataWithBytes:bytes length:length]
1323    }
1324
1325    unsafe fn dataWithBytesNoCopy_length_(_: Self, bytes: *const c_void, length: NSUInteger) -> id {
1326        msg_send![class!(NSData), dataWithBytesNoCopy:bytes length:length]
1327    }
1328
1329    unsafe fn dataWithBytesNoCopy_length_freeWhenDone_(
1330        _: Self,
1331        bytes: *const c_void,
1332        length: NSUInteger,
1333        freeWhenDone: BOOL,
1334    ) -> id {
1335        msg_send![class!(NSData), dataWithBytesNoCopy:bytes length:length freeWhenDone:freeWhenDone]
1336    }
1337
1338    unsafe fn dataWithContentsOfFile_(_: Self, path: id) -> id {
1339        msg_send![class!(NSData), dataWithContentsOfFile: path]
1340    }
1341
1342    unsafe fn dataWithContentsOfFile_options_error_(
1343        _: Self,
1344        path: id,
1345        mask: NSDataReadingOptions,
1346        errorPtr: *mut id,
1347    ) -> id {
1348        msg_send![class!(NSData), dataWithContentsOfFile:path options:mask error:errorPtr]
1349    }
1350
1351    unsafe fn dataWithContentsOfURL_(_: Self, aURL: id) -> id {
1352        msg_send![class!(NSData), dataWithContentsOfURL: aURL]
1353    }
1354
1355    unsafe fn dataWithContentsOfURL_options_error_(
1356        _: Self,
1357        aURL: id,
1358        mask: NSDataReadingOptions,
1359        errorPtr: *mut id,
1360    ) -> id {
1361        msg_send![class!(NSData), dataWithContentsOfURL:aURL options:mask error:errorPtr]
1362    }
1363
1364    unsafe fn dataWithData_(_: Self, aData: id) -> id {
1365        msg_send![class!(NSData), dataWithData: aData]
1366    }
1367
1368    unsafe fn initWithBase64EncodedData_options_(
1369        self,
1370        base64Data: id,
1371        options: NSDataBase64DecodingOptions,
1372    ) -> id;
1373    unsafe fn initWithBase64EncodedString_options_(
1374        self,
1375        base64String: id,
1376        options: NSDataBase64DecodingOptions,
1377    ) -> id;
1378    unsafe fn initWithBytes_length_(self, bytes: *const c_void, length: NSUInteger) -> id;
1379    unsafe fn initWithBytesNoCopy_length_(self, bytes: *const c_void, length: NSUInteger) -> id;
1380    unsafe fn initWithBytesNoCopy_length_deallocator_(
1381        self,
1382        bytes: *const c_void,
1383        length: NSUInteger,
1384        deallocator: *mut Block<(*const c_void, NSUInteger), ()>,
1385    ) -> id;
1386    unsafe fn initWithBytesNoCopy_length_freeWhenDone_(
1387        self,
1388        bytes: *const c_void,
1389        length: NSUInteger,
1390        freeWhenDone: BOOL,
1391    ) -> id;
1392    unsafe fn initWithContentsOfFile_(self, path: id) -> id;
1393    unsafe fn initWithContentsOfFile_options_error(
1394        self,
1395        path: id,
1396        mask: NSDataReadingOptions,
1397        errorPtr: *mut id,
1398    ) -> id;
1399    unsafe fn initWithContentsOfURL_(self, aURL: id) -> id;
1400    unsafe fn initWithContentsOfURL_options_error_(
1401        self,
1402        aURL: id,
1403        mask: NSDataReadingOptions,
1404        errorPtr: *mut id,
1405    ) -> id;
1406    unsafe fn initWithData_(self, data: id) -> id;
1407
1408    unsafe fn bytes(self) -> *const c_void;
1409    unsafe fn description(self) -> id;
1410    unsafe fn enumerateByteRangesUsingBlock_(
1411        self,
1412        block: *mut Block<(*const c_void, NSRange, *mut BOOL), ()>,
1413    );
1414    unsafe fn getBytes_length_(self, buffer: *mut c_void, length: NSUInteger);
1415    unsafe fn getBytes_range_(self, buffer: *mut c_void, range: NSRange);
1416    unsafe fn subdataWithRange_(self, range: NSRange) -> id;
1417    unsafe fn rangeOfData_options_range_(
1418        self,
1419        dataToFind: id,
1420        options: NSDataSearchOptions,
1421        searchRange: NSRange,
1422    ) -> NSRange;
1423
1424    unsafe fn base64EncodedDataWithOptions_(self, options: NSDataBase64EncodingOptions) -> id;
1425    unsafe fn base64EncodedStringWithOptions_(self, options: NSDataBase64EncodingOptions) -> id;
1426
1427    unsafe fn isEqualToData_(self, otherData: id) -> id;
1428    unsafe fn length(self) -> NSUInteger;
1429
1430    unsafe fn writeToFile_atomically_(self, path: id, atomically: BOOL) -> BOOL;
1431    unsafe fn writeToFile_options_error_(
1432        self,
1433        path: id,
1434        mask: NSDataWritingOptions,
1435        errorPtr: *mut id,
1436    ) -> BOOL;
1437    unsafe fn writeToURL_atomically_(self, aURL: id, atomically: BOOL) -> BOOL;
1438    unsafe fn writeToURL_options_error_(
1439        self,
1440        aURL: id,
1441        mask: NSDataWritingOptions,
1442        errorPtr: *mut id,
1443    ) -> BOOL;
1444}
1445
1446impl NSData for id {
1447    unsafe fn initWithBase64EncodedData_options_(
1448        self,
1449        base64Data: id,
1450        options: NSDataBase64DecodingOptions,
1451    ) -> id {
1452        msg_send![self, initWithBase64EncodedData:base64Data options:options]
1453    }
1454
1455    unsafe fn initWithBase64EncodedString_options_(
1456        self,
1457        base64String: id,
1458        options: NSDataBase64DecodingOptions,
1459    ) -> id {
1460        msg_send![self, initWithBase64EncodedString:base64String options:options]
1461    }
1462
1463    unsafe fn initWithBytes_length_(self, bytes: *const c_void, length: NSUInteger) -> id {
1464        msg_send![self,initWithBytes:bytes length:length]
1465    }
1466
1467    unsafe fn initWithBytesNoCopy_length_(self, bytes: *const c_void, length: NSUInteger) -> id {
1468        msg_send![self, initWithBytesNoCopy:bytes length:length]
1469    }
1470
1471    unsafe fn initWithBytesNoCopy_length_deallocator_(
1472        self,
1473        bytes: *const c_void,
1474        length: NSUInteger,
1475        deallocator: *mut Block<(*const c_void, NSUInteger), ()>,
1476    ) -> id {
1477        msg_send![self, initWithBytesNoCopy:bytes length:length deallocator:deallocator]
1478    }
1479
1480    unsafe fn initWithBytesNoCopy_length_freeWhenDone_(
1481        self,
1482        bytes: *const c_void,
1483        length: NSUInteger,
1484        freeWhenDone: BOOL,
1485    ) -> id {
1486        msg_send![self, initWithBytesNoCopy:bytes length:length freeWhenDone:freeWhenDone]
1487    }
1488
1489    unsafe fn initWithContentsOfFile_(self, path: id) -> id {
1490        msg_send![self, initWithContentsOfFile: path]
1491    }
1492
1493    unsafe fn initWithContentsOfFile_options_error(
1494        self,
1495        path: id,
1496        mask: NSDataReadingOptions,
1497        errorPtr: *mut id,
1498    ) -> id {
1499        msg_send![self, initWithContentsOfFile:path options:mask error:errorPtr]
1500    }
1501
1502    unsafe fn initWithContentsOfURL_(self, aURL: id) -> id {
1503        msg_send![self, initWithContentsOfURL: aURL]
1504    }
1505
1506    unsafe fn initWithContentsOfURL_options_error_(
1507        self,
1508        aURL: id,
1509        mask: NSDataReadingOptions,
1510        errorPtr: *mut id,
1511    ) -> id {
1512        msg_send![self, initWithContentsOfURL:aURL options:mask error:errorPtr]
1513    }
1514
1515    unsafe fn initWithData_(self, data: id) -> id {
1516        msg_send![self, initWithData: data]
1517    }
1518
1519    unsafe fn bytes(self) -> *const c_void {
1520        msg_send![self, bytes]
1521    }
1522
1523    unsafe fn description(self) -> id {
1524        msg_send![self, description]
1525    }
1526
1527    unsafe fn enumerateByteRangesUsingBlock_(
1528        self,
1529        block: *mut Block<(*const c_void, NSRange, *mut BOOL), ()>,
1530    ) {
1531        msg_send![self, enumerateByteRangesUsingBlock: block]
1532    }
1533
1534    unsafe fn getBytes_length_(self, buffer: *mut c_void, length: NSUInteger) {
1535        msg_send![self, getBytes:buffer length:length]
1536    }
1537
1538    unsafe fn getBytes_range_(self, buffer: *mut c_void, range: NSRange) {
1539        msg_send![self, getBytes:buffer range:range]
1540    }
1541
1542    unsafe fn subdataWithRange_(self, range: NSRange) -> id {
1543        msg_send![self, subdataWithRange: range]
1544    }
1545
1546    unsafe fn rangeOfData_options_range_(
1547        self,
1548        dataToFind: id,
1549        options: NSDataSearchOptions,
1550        searchRange: NSRange,
1551    ) -> NSRange {
1552        msg_send![self, rangeOfData:dataToFind options:options range:searchRange]
1553    }
1554
1555    unsafe fn base64EncodedDataWithOptions_(self, options: NSDataBase64EncodingOptions) -> id {
1556        msg_send![self, base64EncodedDataWithOptions: options]
1557    }
1558
1559    unsafe fn base64EncodedStringWithOptions_(self, options: NSDataBase64EncodingOptions) -> id {
1560        msg_send![self, base64EncodedStringWithOptions: options]
1561    }
1562
1563    unsafe fn isEqualToData_(self, otherData: id) -> id {
1564        msg_send![self, isEqualToData: otherData]
1565    }
1566
1567    unsafe fn length(self) -> NSUInteger {
1568        msg_send![self, length]
1569    }
1570
1571    unsafe fn writeToFile_atomically_(self, path: id, atomically: BOOL) -> BOOL {
1572        msg_send![self, writeToFile:path atomically:atomically]
1573    }
1574
1575    unsafe fn writeToFile_options_error_(
1576        self,
1577        path: id,
1578        mask: NSDataWritingOptions,
1579        errorPtr: *mut id,
1580    ) -> BOOL {
1581        msg_send![self, writeToFile:path options:mask error:errorPtr]
1582    }
1583
1584    unsafe fn writeToURL_atomically_(self, aURL: id, atomically: BOOL) -> BOOL {
1585        msg_send![self, writeToURL:aURL atomically:atomically]
1586    }
1587
1588    unsafe fn writeToURL_options_error_(
1589        self,
1590        aURL: id,
1591        mask: NSDataWritingOptions,
1592        errorPtr: *mut id,
1593    ) -> BOOL {
1594        msg_send![self, writeToURL:aURL options:mask error:errorPtr]
1595    }
1596}
1597
1598bitflags! {
1599    #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
1600    pub struct NSDataReadingOptions: c_ulonglong {
1601       const NSDataReadingMappedIfSafe = 1 << 0;
1602       const NSDataReadingUncached = 1 << 1;
1603       const NSDataReadingMappedAlways = 1 << 3;
1604    }
1605}
1606
1607bitflags! {
1608    #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
1609    pub struct NSDataBase64EncodingOptions: c_ulonglong {
1610        const NSDataBase64Encoding64CharacterLineLength = 1 << 0;
1611        const NSDataBase64Encoding76CharacterLineLength = 1 << 1;
1612        const NSDataBase64EncodingEndLineWithCarriageReturn = 1 << 4;
1613        const NSDataBase64EncodingEndLineWithLineFeed = 1 << 5;
1614    }
1615}
1616
1617bitflags! {
1618    #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
1619    pub struct NSDataBase64DecodingOptions: c_ulonglong {
1620       const NSDataBase64DecodingIgnoreUnknownCharacters = 1 << 0;
1621    }
1622}
1623
1624bitflags! {
1625    #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
1626    pub struct NSDataWritingOptions: c_ulonglong {
1627        const NSDataWritingAtomic = 1 << 0;
1628        const NSDataWritingWithoutOverwriting = 1 << 1;
1629    }
1630}
1631
1632bitflags! {
1633    #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
1634    pub struct NSDataSearchOptions: c_ulonglong {
1635        const NSDataSearchBackwards = 1 << 0;
1636        const NSDataSearchAnchored = 1 << 1;
1637    }
1638}
1639
1640pub trait NSUserDefaults {
1641    unsafe fn standardUserDefaults() -> Self;
1642
1643    unsafe fn setBool_forKey_(self, value: BOOL, key: id);
1644    unsafe fn bool_forKey_(self, key: id) -> BOOL;
1645
1646    unsafe fn removeObject_forKey_(self, key: id);
1647}
1648
1649impl NSUserDefaults for id {
1650    unsafe fn standardUserDefaults() -> id {
1651        msg_send![class!(NSUserDefaults), standardUserDefaults]
1652    }
1653
1654    unsafe fn setBool_forKey_(self, value: BOOL, key: id) {
1655        msg_send![self, setBool:value forKey:key]
1656    }
1657
1658    unsafe fn bool_forKey_(self, key: id) -> BOOL {
1659        msg_send![self, boolForKey: key]
1660    }
1661
1662    unsafe fn removeObject_forKey_(self, key: id) {
1663        msg_send![self, removeObjectForKey: key]
1664    }
1665}