Skip to content

Commit e8602d5

Browse files
authored
Merge pull request #223 from madsmtm/cleanup-after-move
Cleanup after moving `objc2::foundation`
2 parents 4346f3e + b5b0ed4 commit e8602d5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+211
-300
lines changed

README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ appreciated!
1212
## Crate overview
1313

1414
The core crate is [`objc2`], which contains everything you need to interface
15-
with Objective-C. We use it in [`objc2-foundation`] to provide a safe
16-
abstraction over some core objects from the Foundation Framework, since these
17-
are used in almost all other Objective-C frameworks.
15+
with Objective-C. It also provides safe abstraction over (parts of) the
16+
Foundation Framework, since that is used in almost all Objective-C code.
1817

1918
[`block2`] has a bit of a weird position in all of this: Apple's C language
2019
extension of blocks is _technically_ not limited to being used in Objective-C,
@@ -27,7 +26,6 @@ separate crate to help people cutting down on unneeded dependencies.
2726
runtime libraries.
2827

2928
[`objc2`]: ./objc2
30-
[`objc2-foundation`]: ./objc2-foundation
3129
[`block2`]: ./block2
3230
[`objc2-encode`]: ./objc2-encode
3331
[`objc-sys`]: ./objc-sys
@@ -126,21 +124,27 @@ Work is in progress to make it dual-licensed under the Apache License
126124

127125
## Acknowledgements
128126

129-
This repository is originally a fork of [`objc`] (hence the name `objc2`),
130-
with the following projects merged into it (see reasoning for the fork
131-
[here][origin-issue-101]):
127+
This repository is a merge of the following projects, see reasoning for the
128+
fork [here][origin-issue-101]:
129+
- [`objc`](https://github.com/SSheldon/rust-objc)
130+
- Renamed to `objc2`.
132131
- [`objc-encode`](https://github.com/SSheldon/rust-objc-encode)
132+
- Renamed to `objc2-encode`.
133133
- [`objc_exception`](https://github.com/SSheldon/rust-objc-exception)
134+
- Moved to `objc2::exception`.
134135
- [`objc_id`](https://github.com/SSheldon/rust-objc-id)
136+
- Moved to `objc2::rc`.
135137
- [`objc-foundation`](https://github.com/SSheldon/rust-objc-foundation)
138+
- Moved to `objc2::foundation`.
136139
- [`block`](https://github.com/SSheldon/rust-block)
140+
- Renamed to `block2`.
137141

138142
These were created almost solely by [@SSheldon](https://github.com/SSheldon),
139143
so a huge thanks for their fantastic work on these crates!
140144

141-
This project also draws heavy inspiration from [`fruity`] and [`objrs`].
145+
This project also draws heavy inspiration from [`fruity`], the [`core-foundation-rs` project] and [`objrs`].
142146

143-
[`objc`]: https://github.com/SSheldon/rust-objc
144147
[origin-issue-101]: https://github.com/SSheldon/rust-objc/issues/101
145148
[`fruity`]: https://github.com/nvzqz/fruity
149+
[`core-foundation-rs` project]: https://github.com/servo/core-foundation-rs
146150
[`objrs`]: https://gitlab.com/objrs/objrs

objc-sys/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ pub type NSInteger = isize;
156156
/// use objc_sys::NSUInteger;
157157
/// // Or:
158158
/// // use objc2::ffi::NSUInteger;
159-
/// // use objc2_foundation::NSUInteger;
159+
/// // use objc2::foundation::NSUInteger;
160160
/// extern "C" {
161161
/// fn some_external_function() -> NSUInteger;
162162
/// }

objc2/benches/autorelease.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ use objc2::rc::{autoreleasepool, Id, Shared};
55
use objc2::runtime::{Class, Object, Sel};
66
use objc2::{class, msg_send, sel};
77

8-
#[cfg(feature = "apple")]
9-
#[link(name = "Foundation", kind = "framework")]
10-
extern "C" {}
11-
12-
#[cfg(feature = "gnustep-1-7")]
13-
#[link(name = "gnustep-base", kind = "dylib")]
14-
extern "C" {}
15-
168
const BYTES: &[u8] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
179

1810
fn empty() {

objc2/examples/introspection.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1+
use objc2::msg_send;
12
use objc2::rc::{Id, Owned};
2-
use objc2::runtime::{Class, Object};
3-
use objc2::{class, msg_send, msg_send_id};
3+
use objc2::runtime::Class;
4+
use objc2::{foundation::NSObject, msg_send_id};
45
#[cfg(feature = "malloc")]
56
use objc2::{sel, Encode};
67

7-
#[cfg(feature = "apple")]
8-
#[link(name = "Foundation", kind = "framework")]
9-
extern "C" {}
10-
118
fn main() {
129
// Get a class
13-
let cls = class!(NSObject);
10+
let cls = NSObject::class();
1411
println!("NSObject size: {}", cls.instance_size());
1512

1613
#[cfg(feature = "malloc")]
@@ -23,10 +20,12 @@ fn main() {
2320
}
2421

2522
// Allocate an instance
26-
let obj: Id<Object, Owned> = unsafe {
23+
let obj: Id<NSObject, Owned> = unsafe {
2724
let obj = msg_send_id![cls, alloc];
2825
msg_send_id![obj, init].unwrap()
2926
};
27+
// In this case `let obj = NSObject::new()` would have been the same
28+
3029
println!("NSObject address: {:p}", obj);
3130

3231
// Access an ivar of the object

objc2/examples/nspasteboard.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl NSPasteboard {
9696
/// <https://developer.apple.com/documentation/appkit/nspasteboard/1524454-readobjectsforclasses?language=objc>
9797
pub fn text_impl_2(&self) -> Id<NSString, Shared> {
9898
// The NSPasteboard API is a bit weird, it requires you to pass
99-
// classes as objects, which `objc2_foundation::NSArray` was not
99+
// classes as objects, which `objc2::foundation::NSArray` was not
100100
// really made for - so we convert the class to an `Object` type
101101
// instead. Also, we wrap it in `ManuallyDrop` because I'm not sure
102102
// how classes handle `release` calls?

objc2/examples/talk_to_me.rs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,21 @@ fn main() {
1111

1212
#[cfg(talk_to_me_example)]
1313
fn main() {
14-
use objc2::ffi::NSUInteger;
15-
use objc2::rc::{Id, Owned, Shared};
14+
use objc2::rc::{Id, Owned};
1615
use objc2::runtime::Object;
17-
use objc2::{class, msg_send, msg_send_bool, msg_send_id};
18-
use std::ffi::c_void;
16+
use objc2::{class, msg_send, msg_send_bool, msg_send_id, ns_string};
1917

20-
#[cfg(feature = "apple")]
2118
#[link(name = "AVFoundation", kind = "framework")]
2219
extern "C" {}
23-
#[cfg(feature = "apple")]
24-
#[link(name = "Foundation", kind = "framework")]
25-
extern "C" {}
26-
27-
const UTF8_ENCODING: NSUInteger = 4;
2820

29-
let text = "Hello from Rust!";
30-
31-
// Note: objc2-foundation has functionality to do this safely!
32-
let string = unsafe { msg_send_id![class!(NSString), alloc] };
33-
let text_ptr: *const c_void = text.as_ptr().cast();
34-
let string: Id<Object, Shared> = unsafe {
35-
msg_send_id![
36-
string,
37-
initWithBytes: text_ptr,
38-
length: text.len(),
39-
encoding: UTF8_ENCODING,
40-
]
41-
}
42-
.unwrap();
21+
let string = ns_string!("Hello from Rust!");
4322

4423
let synthesizer: Id<Object, Owned> =
4524
unsafe { msg_send_id![class!(AVSpeechSynthesizer), new] }.unwrap();
4625

4726
let utterance = unsafe { msg_send_id![class!(AVSpeechUtterance), alloc] };
4827
let utterance: Id<Object, Owned> =
49-
unsafe { msg_send_id![utterance, initWithString: &*string] }.unwrap();
28+
unsafe { msg_send_id![utterance, initWithString: string] }.unwrap();
5029

5130
// let _: () = unsafe { msg_send![&utterance, setVolume: 90.0f32 };
5231
// let _: () = unsafe { msg_send![&utterance, setRate: 0.50f32 };

objc2/src/__macro_helpers.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ use crate::{Message, MessageArguments, MessageReceiver};
66
pub use crate::cache::CachedClass;
77
pub use crate::cache::CachedSel;
88

9+
pub use core::borrow::{Borrow, BorrowMut};
910
pub use core::cell::UnsafeCell;
11+
pub use core::convert::{AsMut, AsRef};
12+
pub use core::ops::{Deref, DerefMut};
1013
pub use core::option::Option::{self, None, Some};
1114
pub use core::primitive::{bool, str, u8};
1215
pub use core::{compile_error, concat, panic, stringify};
1316
#[cfg(feature = "objc2-proc-macros")]
1417
pub use objc2_proc_macros::__hash_idents;
18+
// TODO: Use `core::cell::LazyCell`
19+
pub use std::sync::Once;
1520

1621
// Common selectors.
1722
//

objc2/src/exception.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use crate::runtime::Object;
4444
use crate::Message;
4545
use crate::{msg_send, msg_send_bool, msg_send_id, sel};
4646

47-
/// Unfortunate reimplementation of `objc2_foundation::NSString`.
47+
/// Unfortunate reimplementation of `objc2::foundation::NSString`.
4848
///
4949
/// I guess this is the price of wanting to do things "right"...
5050
unsafe fn to_string_hack(obj: Id<Object, Shared>) -> String {

objc2/src/macros/ns_string.rs renamed to objc2/src/foundation/__ns_string.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ use core::mem::ManuallyDrop;
1414
use core::ptr;
1515
use core::sync::atomic::{AtomicPtr, Ordering};
1616

17-
use objc2::rc::Id;
18-
use objc2::runtime::Class;
19-
2017
use crate::foundation::NSString;
18+
use crate::rc::Id;
19+
use crate::runtime::Class;
2120

2221
// This is defined in CoreFoundation, but we don't emit a link attribute
2322
// here because it is already linked via Foundation.
@@ -237,7 +236,7 @@ impl CachedNSString {
237236
}
238237
}
239238

240-
/// Creates an [`NSString`][`crate::NSString`] from a static string.
239+
/// Creates an [`NSString`][`crate::foundation::NSString`] from a static string.
241240
///
242241
/// Currently only supported on Apple targets.
243242
///
@@ -248,7 +247,8 @@ impl CachedNSString {
248247
/// the argument, and produces a `&'static NSString`:
249248
///
250249
/// ```
251-
/// use objc2_foundation::{ns_string, NSString};
250+
/// use objc2::ns_string;
251+
/// use objc2::foundation::NSString;
252252
/// # #[cfg(feature = "gnustep-1-7")]
253253
/// # unsafe { objc2::__gnustep_hack::get_class_to_force_linkage() };
254254
/// let hello: &'static NSString = ns_string!("hello");
@@ -267,7 +267,7 @@ impl CachedNSString {
267267
/// string to the most efficient encoding, you don't have to do anything!
268268
///
269269
/// ```
270-
/// # use objc2_foundation::ns_string;
270+
/// # use objc2::ns_string;
271271
/// # #[cfg(feature = "gnustep-1-7")]
272272
/// # unsafe { objc2::__gnustep_hack::get_class_to_force_linkage() };
273273
/// let hello_ru = ns_string!("Привет");
@@ -285,7 +285,7 @@ impl CachedNSString {
285285
/// expect:
286286
///
287287
/// ```
288-
/// # use objc2_foundation::ns_string;
288+
/// # use objc2::ns_string;
289289
/// # #[cfg(feature = "gnustep-1-7")]
290290
/// # unsafe { objc2::__gnustep_hack::get_class_to_force_linkage() };
291291
/// let example = ns_string!("example\0");
@@ -305,7 +305,7 @@ impl CachedNSString {
305305
/// Because of that, this should be preferred over [`NSString::from_str`]
306306
/// where possible.
307307
///
308-
/// [`NSString::from_str`]: crate::NSString::from_str
308+
/// [`NSString::from_str`]: crate::foundation::NSString::from_str
309309
#[macro_export]
310310
macro_rules! ns_string {
311311
($s:expr) => {{
@@ -354,7 +354,7 @@ macro_rules! __ns_string_inner {
354354
// The full UTF-16 contents along with the written length.
355355
const UTF16_FULL: (&[u16; $inp.len()], usize) = {
356356
let mut out = [0u16; $inp.len()];
357-
let mut iter = $crate::__string_macro::EncodeUtf16Iter::new($inp);
357+
let mut iter = $crate::foundation::__ns_string::EncodeUtf16Iter::new($inp);
358358
let mut written = 0;
359359

360360
while let Some((state, chars)) = iter.next() {
@@ -395,17 +395,17 @@ macro_rules! __ns_string_inner {
395395
// The section is the same as what clang sets, see:
396396
// https://github.com/llvm/llvm-project/blob/release/13.x/clang/lib/CodeGen/CodeGenModule.cpp#L5243
397397
#[link_section = "__DATA,__cfstring"]
398-
static CFSTRING: $crate::__string_macro::CFConstString = unsafe {
399-
if $crate::__string_macro::is_ascii_no_nul($inp) {
398+
static CFSTRING: $crate::foundation::__ns_string::CFConstString = unsafe {
399+
if $crate::foundation::__ns_string::is_ascii_no_nul($inp) {
400400
// This is technically an optimization (UTF-16 strings are
401401
// always valid), but it's a fairly important one!
402-
$crate::__string_macro::CFConstString::new_ascii(
403-
&$crate::__string_macro::__CFConstantStringClassReference,
402+
$crate::foundation::__ns_string::CFConstString::new_ascii(
403+
&$crate::foundation::__ns_string::__CFConstantStringClassReference,
404404
&ASCII,
405405
)
406406
} else {
407-
$crate::__string_macro::CFConstString::new_utf16(
408-
&$crate::__string_macro::__CFConstantStringClassReference,
407+
$crate::foundation::__ns_string::CFConstString::new_utf16(
408+
&$crate::foundation::__ns_string::__CFConstantStringClassReference,
409409
&UTF16,
410410
)
411411
}
@@ -418,7 +418,7 @@ macro_rules! __ns_string_inner {
418418
#[macro_export]
419419
macro_rules! __ns_string_inner {
420420
($inp:ident) => {{
421-
use $crate::__string_macro::CachedNSString;
421+
use $crate::foundation::__ns_string::CachedNSString;
422422
static CACHED_NSSTRING: CachedNSString = CachedNSString::new();
423423
CACHED_NSSTRING.get($inp)
424424
}};
@@ -508,7 +508,7 @@ mod tests {
508508
"\0",
509509
"\0\x01\x02\x03\x04\x05\x06\x07\x08\x09",
510510
// "\u{feff}", // TODO
511-
include_str!("ns_string.rs"),
511+
include_str!("__ns_string.rs"),
512512
}
513513
}
514514

objc2/src/foundation/array.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@ use core::marker::PhantomData;
44
use core::ops::{Index, IndexMut, Range};
55
use core::panic::{RefUnwindSafe, UnwindSafe};
66

7-
use objc2::rc::{DefaultId, Id, Owned, Ownership, Shared, SliceId};
8-
use objc2::runtime::{Class, Object};
9-
use objc2::Message;
10-
use objc2::{msg_send, msg_send_id};
11-
127
use super::{
138
NSCopying, NSEnumerator, NSFastEnumeration, NSFastEnumerator, NSMutableArray, NSMutableCopying,
149
NSObject, NSRange,
1510
};
16-
use crate::__inner_extern_class;
11+
use crate::rc::{DefaultId, Id, Owned, Ownership, Shared, SliceId};
12+
use crate::runtime::{Class, Object};
13+
use crate::Message;
14+
use crate::{__inner_extern_class, msg_send, msg_send_id};
1715

1816
__inner_extern_class! {
1917
/// TODO
@@ -244,10 +242,9 @@ mod tests {
244242
use alloc::format;
245243
use alloc::vec::Vec;
246244

247-
use objc2::rc::autoreleasepool;
248-
249245
use super::*;
250246
use crate::foundation::{NSString, NSValue};
247+
use crate::rc::autoreleasepool;
251248

252249
fn sample_array(len: usize) -> Id<NSArray<NSObject, Owned>, Owned> {
253250
let mut vec = Vec::with_capacity(len);

0 commit comments

Comments
 (0)