Skip to content

Commit

Permalink
Merge pull request #303 from madsmtm/icrate-foundation
Browse files Browse the repository at this point in the history
Move `objc2::foundation` to `icrate::Foundation`
  • Loading branch information
madsmtm committed Dec 23, 2022
2 parents 3834c3a + c79d16d commit 5dfd34c
Show file tree
Hide file tree
Showing 88 changed files with 668 additions and 892 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ jobs:
ARGS: --no-default-features --features=std,${{ matrix.runtime || 'apple' }} ${{ matrix.args }}
# Use --no-fail-fast, except with dinghy
TESTARGS: ${{ matrix.dinghy && ' ' || '--no-fail-fast' }} ${{ matrix.test-args }}
SOME_FEATURES: ${{ matrix.features || 'malloc,block,exception,foundation' }}
FEATURES: ${{ matrix.features || 'malloc,block,exception,foundation,catch-all,verify,uuid' }}
SOME_FEATURES: ${{ matrix.features || 'malloc,block,exception,Foundation' }}
FEATURES: ${{ matrix.features || 'malloc,block,exception,unstable-all-frameworks,catch-all,verify,uuid' }}
UNSTABLE_FEATURES: ${{ matrix.unstable-features || 'unstable-autoreleasesafe,unstable-c-unwind' }}
CMD: cargo

Expand Down Expand Up @@ -428,7 +428,7 @@ jobs:

- name: Test static class and selectors
if: ${{ !matrix.dinghy && (matrix.runtime || 'apple') == 'apple' }}
run: cargo test ${{ env.ARGS }} ${{ env.TESTARGS }} --features=foundation,unstable-static-sel,unstable-static-class
run: cargo test ${{ env.ARGS }} ${{ env.TESTARGS }} --features=Foundation,unstable-static-sel,unstable-static-class

- name: Run assembly tests
if: ${{ !contains(matrix.runtime, 'compiler-rt') }}
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ appreciated!
## Crate overview

The core crate is [`objc2`], which contains everything you need to interface
with Objective-C. It also provides safe abstraction over (parts of) the
Foundation Framework, since that is used in almost all Objective-C code.
with Objective-C. [`icrate`] then use that to provide a safe abstraction over
(parts of) the Foundation Framework, since that is used in almost all
Objective-C code.

[`block2`] has a bit of a weird position in all of this: Apple's C language
extension of blocks is _technically_ not limited to being used in Objective-C,
though in practice that's the only place it's used, so it makes sense to
develop these together.

[`objc2-encode`] is really just a part of `objc2`, it mostly exists as a
separate crate to help people cutting down on unneeded dependencies.
separate crate to help users of it cutting down on unneeded dependencies.
[`objc-sys`] and [`block-sys`] contain raw bindings to the underlying C
runtime libraries.

[`objc2`]: ./crates/objc2
[`icrate`]: ./crates/icrate
[`block2`]: ./crates/block2
[`objc2-encode`]: ./crates/objc2-encode
[`objc-sys`]: ./crates/objc-sys
Expand All @@ -49,8 +51,8 @@ objc = { package = "objc2", version = "0.2.7" }

Afterwards, you can upgrade to the next release, in this case
`v0.3.0-alpha.0`, and make the required changes to your code (the
[changelog](crates/objc2/CHANGELOG.md) contains recommendations for this). And so on,
with every following release.
[changelog](crates/objc2/CHANGELOG.md) contains recommendations for this). And
so on, with every following release.


## Goals
Expand Down Expand Up @@ -135,7 +137,7 @@ fork [here][origin-issue-101]:
- [`objc_id`](https://github.com/SSheldon/rust-objc-id)
- Moved to `objc2::rc`.
- [`objc-foundation`](https://github.com/SSheldon/rust-objc-foundation)
- Moved to `objc2::foundation`.
- Moved to `icrate::Foundation`.
- [`block`](https://github.com/SSheldon/rust-block)
- Renamed to `block2`.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
# Changelog

Changes to the `objc2::foundation` module will be documented in this file.
This previously existed as a separate crate `objc2_foundation`, hence the
separation.
Changes to `icrate` will be documented in this file.

This previously existed as a separate crate `objc2_foundation`, later it was
integrated into `objc2::foundation`, and later again split out - hence the
confusing versioning.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Unreleased - YYYY-MM-DD
## icrate Unreleased - YYYY-MM-DD

### Added
* Added `NSString::write_to_file`.
* Added `NSLock` class and `NSLocking` protocol.

### Changed
* **BREAKING**: Moved from `objc2::foundation` into `icrate::Foundation`.

### Fixed
* Fixed `NSZone` not being `#[repr(C)]`.


## objc2 0.3.0-beta.3 - 2022-09-01
## objc2::foundation 0.3.0-beta.3 - 2022-09-01

### Added
* Added `NSSet`.
Expand All @@ -36,7 +41,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
binding to requires a non-negative size.


## objc2 0.3.0-beta.2 - 2022-08-28
## objc2::foundation 0.3.0-beta.2 - 2022-08-28

### Added
* Added `NSNumber`.
Expand Down
26 changes: 23 additions & 3 deletions crates/icrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ license = "MIT"
objc2 = { path = "../objc2", version = "=0.3.0-beta.3", default-features = false, optional = true }
block2 = { path = "../block2", version = "=0.2.0-alpha.6", default-features = false, optional = true }

# Provide methods to convert between `uuid::Uuid` and `icrate::Foundation::NSUUID`
uuid = { version = "1.1.2", optional = true, default-features = false }

[package.metadata.docs.rs]
default-target = "x86_64-apple-darwin"
all-features = true
features = ["block", "objective-c", "uuid", "unstable-all-frameworks", "unstable-docsrs"]

targets = [
# MacOS
Expand All @@ -43,6 +46,22 @@ targets = [
"x86_64-pc-windows-msvc",
]

[[example]]
name = "basic_usage"
required-features = ["Foundation"]

[[example]]
name = "delegate"
required-features = ["apple", "Foundation"]

[[example]]
name = "speech_synthesis"
required-features = ["apple", "Foundation"]

[[example]]
name = "nspasteboard"
required-features = ["apple", "Foundation"]

[features]
default = ["std", "apple"]

Expand All @@ -62,7 +81,7 @@ gnustep-2-1 = ["gnustep-2-0", "objc2?/gnustep-2-1", "block2?/gnustep-2-1"]
objective-c = ["objc2"]

# Expose features that requires creating blocks.
blocks = ["objc2?/block", "block2"]
block = ["objc2?/block", "block2"]

# For better documentation on docs.rs
unstable-docsrs = []
Expand All @@ -72,4 +91,5 @@ unstable-docsrs = []
AppKit = ["Foundation", "CoreData"]
AuthenticationServices = ["Foundation"]
CoreData = ["Foundation"]
Foundation = ["objective-c", "blocks", "objc2/foundation"]
Foundation = ["objective-c", "block"]
unstable-all-frameworks = ["AppKit", "AuthenticationServices", "CoreData", "Foundation"]
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use objc2::foundation::{NSArray, NSDictionary, NSObject};
use objc2::ns_string;
use objc2::rc::autoreleasepool;
use icrate::ns_string;
use icrate::objc2::rc::autoreleasepool;
use icrate::Foundation::{NSArray, NSDictionary, NSObject};

fn main() {
// Create and compare NSObjects
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#![cfg_attr(not(target_os = "macos"), allow(unused))]
use objc2::declare::{Ivar, IvarDrop};
use objc2::foundation::{NSCopying, NSObject, NSString};
use objc2::rc::{Id, Shared};
use objc2::runtime::Object;
use objc2::{declare_class, extern_class, msg_send, msg_send_id, ns_string, ClassType};
use icrate::ns_string;
use icrate::objc2::declare::{Ivar, IvarDrop};
use icrate::objc2::rc::{Id, Shared};
use icrate::objc2::runtime::Object;
use icrate::objc2::{declare_class, extern_class, msg_send, msg_send_id, ClassType};
use icrate::Foundation::{NSCopying, NSObject, NSString};

#[cfg(target_os = "macos")]
#[link(name = "AppKit", kind = "framework")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use std::mem::ManuallyDrop;

use objc2::foundation::{NSArray, NSDictionary, NSInteger, NSObject, NSString};
use objc2::rc::{Id, Shared};
use objc2::runtime::{Class, Object};
use objc2::{extern_class, msg_send, msg_send_id, ClassType};
use icrate::objc2::rc::{Id, Shared};
use icrate::objc2::runtime::{Class, Object};
use icrate::objc2::{extern_class, msg_send, msg_send_id, ClassType};
use icrate::Foundation::{NSArray, NSDictionary, NSInteger, NSObject, NSString};

type NSPasteboardType = NSString;
type NSPasteboardReadingOptionKey = NSString;
Expand Down Expand Up @@ -63,7 +63,7 @@ impl NSPasteboard {
/// <https://developer.apple.com/documentation/appkit/nspasteboard/1524454-readobjectsforclasses?language=objc>
pub fn text_impl_2(&self) -> Id<NSString, Shared> {
// The NSPasteboard API is a bit weird, it requires you to pass
// classes as objects, which `objc2::foundation::NSArray` was not
// classes as objects, which `icrate::Foundation::NSArray` was not
// really made for - so we convert the class to an `Object` type
// instead. Also, we wrap it in `ManuallyDrop` because I'm not sure
// how classes handle `release` calls?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
use std::thread;
use std::time::Duration;

use objc2::foundation::{NSObject, NSString};
use objc2::rc::{Id, Owned};
use objc2::{extern_class, msg_send, msg_send_id, ns_string, ClassType};
use icrate::ns_string;
use icrate::objc2::rc::{Id, Owned};
use icrate::objc2::{extern_class, msg_send, msg_send_id, ClassType};
use icrate::Foundation::{NSObject, NSString};

#[cfg(target_os = "macos")]
mod appkit {
use objc2::{foundation::NSCopying, rc::Shared};
use icrate::objc2::rc::Shared;
use icrate::Foundation::NSCopying;

use super::*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use core::mem::ManuallyDrop;
use core::ptr;
use core::sync::atomic::{AtomicPtr, Ordering};

use crate::foundation::NSString;
use crate::rc::Id;
use crate::runtime::Class;
use crate::Foundation::NSString;
use objc2::rc::Id;
use objc2::runtime::Class;

// This is defined in CoreFoundation, but we don't emit a link attribute
// here because it is already linked via Foundation.
Expand Down Expand Up @@ -239,7 +239,7 @@ impl CachedNSString {
}
}

/// Creates an [`NSString`][`crate::foundation::NSString`] from a static string.
/// Creates an [`NSString`][`crate::Foundation::NSString`] from a static string.
///
/// Note: This works by placing statics in special sections, which may not
/// work completely reliably yet, see [#258]; until then, you should be
Expand All @@ -254,10 +254,9 @@ impl CachedNSString {
/// the argument, and produces a `&'static NSString`:
///
/// ```
/// use objc2::ns_string;
/// use objc2::foundation::NSString;
/// # #[cfg(feature = "gnustep-1-7")]
/// # unsafe { objc2::__gnustep_hack::get_class_to_force_linkage() };
/// use icrate::ns_string;
/// use icrate::Foundation::NSString;
///
/// let hello: &'static NSString = ns_string!("hello");
/// assert_eq!(hello.to_string(), "hello");
///
Expand All @@ -274,9 +273,7 @@ impl CachedNSString {
/// string to the most efficient encoding, you don't have to do anything!
///
/// ```
/// # use objc2::ns_string;
/// # #[cfg(feature = "gnustep-1-7")]
/// # unsafe { objc2::__gnustep_hack::get_class_to_force_linkage() };
/// # use icrate::ns_string;
/// let hello_ru = ns_string!("Привет");
/// assert_eq!(hello_ru.to_string(), "Привет");
/// ```
Expand All @@ -292,9 +289,7 @@ impl CachedNSString {
/// expect:
///
/// ```
/// # use objc2::ns_string;
/// # #[cfg(feature = "gnustep-1-7")]
/// # unsafe { objc2::__gnustep_hack::get_class_to_force_linkage() };
/// # use icrate::ns_string;
/// let example = ns_string!("example\0");
/// assert_eq!(example.to_string(), "example\0");
///
Expand All @@ -312,8 +307,8 @@ impl CachedNSString {
/// Because of that, this should be preferred over [`NSString::from_str`]
/// where possible.
///
/// [`NSString::from_str`]: crate::foundation::NSString::from_str
#[cfg(feature = "foundation")] // For auto_doc_cfg
/// [`NSString::from_str`]: crate::Foundation::NSString::from_str
#[cfg(feature = "Foundation")] // For auto_doc_cfg
#[macro_export]
macro_rules! ns_string {
($s:expr) => {{
Expand Down Expand Up @@ -362,7 +357,7 @@ macro_rules! __ns_string_inner {
// The full UTF-16 contents along with the written length.
const UTF16_FULL: (&[u16; $inp.len()], usize) = {
let mut out = [0u16; $inp.len()];
let mut iter = $crate::foundation::__ns_string::EncodeUtf16Iter::new($inp);
let mut iter = $crate::Foundation::__ns_string::EncodeUtf16Iter::new($inp);
let mut written = 0;

while let Some((state, chars)) = iter.next() {
Expand Down Expand Up @@ -403,17 +398,17 @@ macro_rules! __ns_string_inner {
// The section is the same as what clang sets, see:
// https://github.com/llvm/llvm-project/blob/release/13.x/clang/lib/CodeGen/CodeGenModule.cpp#L5243
#[link_section = "__DATA,__cfstring"]
static CFSTRING: $crate::foundation::__ns_string::CFConstString = unsafe {
if $crate::foundation::__ns_string::is_ascii_no_nul($inp) {
static CFSTRING: $crate::Foundation::__ns_string::CFConstString = unsafe {
if $crate::Foundation::__ns_string::is_ascii_no_nul($inp) {
// This is technically an optimization (UTF-16 strings are
// always valid), but it's a fairly important one!
$crate::foundation::__ns_string::CFConstString::new_ascii(
&$crate::foundation::__ns_string::__CFConstantStringClassReference,
$crate::Foundation::__ns_string::CFConstString::new_ascii(
&$crate::Foundation::__ns_string::__CFConstantStringClassReference,
&ASCII,
)
} else {
$crate::foundation::__ns_string::CFConstString::new_utf16(
&$crate::foundation::__ns_string::__CFConstantStringClassReference,
$crate::Foundation::__ns_string::CFConstString::new_utf16(
&$crate::Foundation::__ns_string::__CFConstantStringClassReference,
&UTF16,
)
}
Expand All @@ -426,7 +421,7 @@ macro_rules! __ns_string_inner {
#[macro_export]
macro_rules! __ns_string_inner {
($inp:ident) => {{
use $crate::foundation::__ns_string::CachedNSString;
use $crate::Foundation::__ns_string::CachedNSString;
static CACHED_NSSTRING: CachedNSString = CachedNSString::new();
CACHED_NSSTRING.get($inp)
}};
Expand Down
Loading

0 comments on commit 5dfd34c

Please sign in to comment.