forked from drewcrawford/objr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
44 changed files
with
5,841 additions
and
54 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1 @@ | ||
mod cgdirectdisplay; | ||
|
||
use objr::bindings::PerformsSelector; | ||
|
||
// pub use cgcolor::CGColorRef; | ||
pub use cgdirectdisplay::CGDirectDisplayID; | ||
use objr::bindings::PerformsSelector; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
use coregraphicsr::CGDirectDisplayID; | ||
extern crate coregraphicsr; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
on: [push] | ||
jobs: | ||
ci: | ||
runs-on: macos-11 | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- run: cargo test | ||
- run: cargo doc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Cargo.lock | ||
procmacro/target | ||
target | ||
.DS_Store | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "objr" | ||
version = "0.2.0" | ||
authors = ["Drew Crawford <drew@sealedabstract.com>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
procmacro = { path = "procmacro" } | ||
|
||
[dev-dependencies] | ||
criterion = "0.3" | ||
|
||
[[bench]] | ||
name = "basic_bench" | ||
harness = false | ||
|
||
#[build-dependencies] | ||
#cc = "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
This software is licensed under the Prosperity Public License which permits noncommercial use only. This software may also be offered under other licenses. Commercial licensing and support is available from [the author](mailto:drew@sealedabstract.com). | ||
|
||
As a special exception, if you've received under $100k in funding last year, **your use doesn't count as commercial use**, even if you make money. As long as you qualify, you can use this software for any purpose without another license and free of charge. | ||
|
||
To decide if you qualify: | ||
|
||
|
||
- If you're using this software for a client, use your client’s funding. | ||
- Otherwise, if you’re an individual, sole proprietor, or disregarded entity, only gross revenue and funding related to this software counts. | ||
- For example, if you use this software on a side project, but not for your main business, use the revenue and funding of your side project. You aren’t disqualified by the main business. | ||
- Otherwise, if you’re another kind of company, use the gross revenue and funding of the company. | ||
|
||
# The Prosperity Public License 3.0.0 | ||
|
||
|
||
Contributor: Drew Crawford | ||
|
||
Source Code: https://github.com/drewcrawford/objr | ||
|
||
## Purpose | ||
|
||
|
||
This license allows you to use and share this software for noncommercial purposes for free and to try this software for commercial purposes for thirty days. | ||
|
||
## Agreement | ||
|
||
|
||
In order to receive this license, you have to agree to its rules. Those rules are both obligations under that agreement and conditions to your license. Don’t do anything with this software that triggers a rule you can’t or won’t follow. | ||
|
||
## Notices | ||
|
||
|
||
Make sure everyone who gets a copy of any part of this software from you, with or without changes, also gets the text of this license and the contributor and source code lines above. | ||
|
||
## Commercial Trial | ||
|
||
|
||
Limit your use of this software for commercial purposes to a thirty-day trial period. If you use this software for work, your company gets one trial period for all personnel, not one trial per person. | ||
|
||
## Contributions Back | ||
|
||
|
||
Developing feedback, changes, or additions that you contribute back to the contributor on the terms of a standardized public software license such as [the Blue Oak Model License 1.0.0](https://blueoakcouncil.org/license/1.0.0), [the Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0.html), [the MIT license](https://spdx.org/licenses/MIT.html), or [the two-clause BSD license](https://spdx.org/licenses/BSD-2-Clause.html) doesn’t count as use for a commercial purpose. | ||
|
||
## Personal Uses | ||
|
||
|
||
Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, doesn’t count as use for a commercial purpose. | ||
|
||
## Noncommercial Organizations | ||
|
||
|
||
Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution doesn’t count as use for a commercial purpose regardless of the source of funding or obligations resulting from the funding. | ||
|
||
## Defense | ||
|
||
|
||
Don’t make any legal claim against anyone accusing this software, with or without changes, alone or with other technology, of infringing any patent. | ||
|
||
## Copyright | ||
|
||
|
||
The contributor licenses you to do everything with this software that would otherwise infringe their copyright in it. | ||
|
||
## Patent | ||
|
||
|
||
The contributor licenses you to do everything with this software that would otherwise infringe any patents they can license or become able to license. | ||
|
||
## Reliability | ||
|
||
|
||
The contributor can’t revoke this license. | ||
|
||
## Excuse | ||
|
||
|
||
You’re excused for unknowingly breaking [Notices](https://prosperitylicense.com/versions/3.0.0#notices) if you take all practical steps to comply within thirty days of learning you broke the rule. | ||
|
||
## No Liability | ||
|
||
|
||
***As far as the law allows, this software comes as is, without any warranty or condition, and the contributor won’t be liable to anyone for any damages related to this software or this license, under any kind of legal claim.*** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# Drew's very fast objc Rust bindings | ||
This library provides low-level Rust bindings for ObjC, which is in practice the ABI for macOS. You might compare | ||
this crate with [objc](https://crates.io/crates/objc), [fruity](https://docs.rs/fruity/0.2.0/fruity/), [objcrs](https://crates.io/crates/objrs) | ||
and many others. | ||
|
||
Distinctive features of this library include: | ||
* Zero-cost abstractions, including the elusive "compile-time selectors" as well as many other new and exciting static technologies | ||
* Advanced performance and codesize optimizations suitable for real-time, game, and graphical applications | ||
* Smart pointers that integrate ObjC memory management into safe Rust abstractions | ||
* Write ObjC subclasses directly in Rust | ||
* Emits code similar to real ObjC compilers, for speed and future-compatibility | ||
* Macro system for productively hand-coding bindings for new ObjC APIs | ||
* Low level, ObjC-like APIs that you can build on to expose a Rusty interface – or not, for extra control and performance | ||
* Minimal API coverage, leaving the question of which APIs to use and how to expose them to Rust for other crates | ||
* Focus on modern, Apple-platform ObjC | ||
* Free for noncommercial and "small commercial" use. | ||
|
||
# Detailed general examples | ||
|
||
## Using an external class | ||
|
||
```rust | ||
//We intend to write bindings for ObjC APIs | ||
use objr::bindings::*; | ||
objc_class! { | ||
//Rust wrapper type | ||
pub struct NSDate { | ||
//ObjC class name | ||
@class(NSDate) | ||
} | ||
} | ||
autoreleasepool(|pool| { | ||
//In this library, autoreleasepools are often arguments to ObjC-calling APIs, providing static guarantees you created one. | ||
//Forgetting this is a common ObjC bug. | ||
let date = NSDate::class().alloc_init(&pool); | ||
println!("{}",date); // 2021-06-21 19:03:15 +0000 | ||
}) | ||
``` | ||
|
||
Compare this with `objc_instance!` for non-class instances. | ||
|
||
## Binding an ObjC API | ||
|
||
```rust | ||
use objr::bindings::*; | ||
objc_class! { | ||
//Rust wrapper type | ||
pub struct NSDate { | ||
@class(NSDate) | ||
} | ||
} | ||
//Declares a group of static selectors. | ||
objc_selector_group! { | ||
pub trait NSDateSelectors { | ||
@selector("dateByAddingTimeInterval:") | ||
} | ||
//Adds support for these selectors to our `Sel` APIs. | ||
impl NSDateSelectors for Sel {} | ||
} | ||
|
||
//Declare APIs directly on our `NSDate` wrapper type | ||
impl NSDate { | ||
fn dateByAddingTimeInterval(&self, pool: &ActiveAutoreleasePool, interval: f64) | ||
//Although the underlying ObjC API returns a +0 unowned reference, | ||
//We create a binding that returns +1 retained instead. We might do this | ||
//because it's the preferred pattern of our application. | ||
//For more details, see the documentation of [objc_instance!] | ||
-> StrongCell<NSDate> { | ||
//Use of ObjC is unsafe. There is no runtime or dynamic checking of your work here, | ||
//so you must provide a safe abstraction to callers (or mark the enclosing function unsafe). | ||
unsafe { | ||
/*Convert from an autoreleased return value to a strong one. | ||
This uses tricks used by real ObjC compilers and is far faster than calling `retain` yourself. | ||
*/ | ||
let raw = Self::perform_autorelease_to_retain( | ||
//the objc method we are calling does not mutate the receiver | ||
self.assume_nonmut_perform(), | ||
///Use the compile-time selector we declared above | ||
Sel::dateByAddingTimeInterval_(), | ||
///Static checking that we have an autoreleasepool available | ||
pool, | ||
///Arguments. Note the trailing `,`. Arguments are tuple types. | ||
(interval,)); | ||
//assume the result is nonnil | ||
Self::assume_nonnil(raw) | ||
//assume the object is +1 convention (it is, because we called perform_autorelease_to_retain above) | ||
.assume_retained() | ||
} | ||
} | ||
} | ||
autoreleasepool(|pool| { | ||
//In this library, autoreleasepools are often arguments to ObjC-calling APIs, providing compile-time guarantees you created one. | ||
//Forgetting this is a common ObjC bug. | ||
let date = NSDate::class().alloc_init(&pool); | ||
let new_date = date.dateByAddingTimeInterval(&pool, 23.5); | ||
}) | ||
``` | ||
|
||
For more examples, see the documentation for `objc_instance!`. | ||
|
||
# Feature index | ||
|
||
* Statically declare selectors and classes, string literals, enums, etc. so they don't have to be looked up at runtime | ||
* Leverage the Rust typesystem to elide `retain`/`release`/`autorelease` calls in many cases. | ||
* Participate in runtime autorelease eliding which reduces memory overhead when calling system code | ||
This means that for programs that are mostly Rust, codegeneration may be significantly better even than real ObjC programs. | ||
* Pointer packing for `Option<&NSObject>` | ||
* Smart pointer system, with support for `StrongCell` and `AutoreleasedCell` | ||
* Subclassing directly from Rust | ||
* (limited) support for mutability and exclusive references in imported types | ||
|
||
Not yet implemented, but planned or possible: | ||
|
||
* iOS support | ||
* Exceptions (Debug-quality API available now, see ``bindings::try_unwrap_void`) | ||
|
||
# Design limitations | ||
|
||
This library intends to follow normal guidelines for safe Rust. However, calling into ObjC means there's | ||
a giant ball of `unsafe` somewhere. | ||
|
||
This library makes the assumption that the underlying ball of ObjC is implemented correctly. In particular, | ||
it avoids runtime checks that ObjC is implemented correctly, so to the extent that it isn't, you may encounter UB. | ||
|
||
For more information, see the safety section of `objc_instance`! | ||
|
||
# objr expanded universe | ||
|
||
objr is part of an expanded universe of related crates that take a similar approach | ||
to binding Apple technologies to Rust. | ||
|
||
* [blocksr](https://github.com/drewcrawford/blocksr) provides Clang/ObjC blocks | ||
* [dispatchr](http://github.com/drewcrawford/dispatchr) binds libdispatch/GCD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use criterion::{black_box, criterion_group, criterion_main, Criterion}; | ||
extern crate objr; | ||
use objr::foundation::*; | ||
|
||
fn alloc_init_description(c: &mut Criterion) { | ||
autoreleasepool(|pool| { | ||
c.bench_function("NSObject_alloc_init_description", |b| b.iter(|| { | ||
let class = NSObject::class(); | ||
let instance = class.alloc_init(pool); | ||
let description = instance.description(pool).to_str(pool).len(); | ||
black_box(description) | ||
})); | ||
}); | ||
} | ||
|
||
criterion_group!(benches, alloc_init_description); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
|
||
[package] | ||
name = "procmacro" | ||
version = "0.1.0" | ||
authors = ["Drew Crawford <drew@sealedabstract.com>"] | ||
edition = "2018" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
Oops, something went wrong.