7 releases

0.1.2 Apr 17, 2024
0.1.1 Apr 17, 2024
0.1.0 Dec 23, 2023
0.0.4 Jul 31, 2023
0.0.0 Sep 19, 2022

#212 in macOS and iOS APIs

Download history 19503/week @ 2024-08-14 18330/week @ 2024-08-21 17693/week @ 2024-08-28 18101/week @ 2024-09-04 15027/week @ 2024-09-11 15875/week @ 2024-09-18 17842/week @ 2024-09-25 13889/week @ 2024-10-02 13433/week @ 2024-10-09 13005/week @ 2024-10-16 11057/week @ 2024-10-23 11899/week @ 2024-10-30 12213/week @ 2024-11-06 11568/week @ 2024-11-13 10558/week @ 2024-11-20 7090/week @ 2024-11-27

43,803 downloads per month
Used in 428 crates (11 directly)

MIT license

10MB
215K SLoC

icrate

Latest version License Documentation CI

Rust bindings to Apple's frameworks.

This crate has been split into multiple smaller ones, and is now deprecated!

See objc2 for the list of available crates.

Supported versions

These bindings are automatically generated from the SDKs in Xcode 15.0.1 (will be periodically updated).

Currently supports:

  • macOS: 10.12-14.0
  • iOS/iPadOS: 10.0-17.0 (WIP)
  • tvOS: 10.0-17.0 (WIP)
  • watchOS: 5.0-10.0 (WIP)

lib.rs:

Bindings to Apple's frameworks

icrate is an autogenerated interface to Apple's Objective-C frameworks like AppKit, Foundation, Metal, WebKit, and so on.

The bindings currently contain very little documentation, you should view Apple's developer documentation for detailed information about each API. (There are plans for importing that documentation here).

Use of Deref

icrate uses the Deref trait in a bit special way: All objects deref to their superclasses. For example, NSMutableArray derefs to NSArray, which in turn derefs to NSObject.

Note that this is explicitly recommended against in the documentation and the Rust Design patterns book (see those links for details).

Due to Objective-C objects only ever being accessible behind pointers in the first place, the problems stated there are less severe, and having the implementation just means that everything is much nicer when you actually want to use the objects!

All objects also implement AsRef and AsMut to their superclass, and can be used in Id::into_super, so if you favour explicit conversion, that is a possibility too.

Rust vs. Objective-C types

A quick overview of some types you will encounter often in Objective-C, and their approximate Rust equivalent.

Objective-C (approximately) equivalent Rust
NSData* Arc<[u8]>
NSMutableData* Vec<u8>
NSString* Arc<str>
NSMutableString* String
NSValue* Arc<dyn Any>
NSNumber* Arc<enum { I8(i8), U8(u8), I16(i16), U16(u16), I32(i32), U32(u32), I64(i64), U64(u64), F32(f32), F64(f64), CLong(ffi::c_long), CULong(ffi::c_ulong) }>
NSError* Arc<dyn Error + Send + Sync>
NSException* Arc<dyn Error + Send + Sync>
NSRange ops::Range<usize>
NSComparisonResult cmp::Ordering
NSArray<T>* Arc<[T]>
NSMutableArray<T>* Vec<T>
NSDictionary<K, V>* Arc<HashMap<K, V>>
NSMutableDictionary<K, V>* HashMap<K, V>
NSEnumerator<T>* Box<dyn Iterator<T>>
NSCopying* Box<dyn Clone>

Example

$ cargo add icrate --features=Foundation,Foundation_all

use icrate::Foundation::{ns_string, NSCopying, NSArray};

let string = ns_string!("world"); println!("hello {string}");

let array = NSArray::from_id_slice(&[string.copy()]); println!("{array:?}");

Dependencies