Skip to content

Commit 00694f3

Browse files
committed
Improve examples and remove redundant linking code
1 parent 3b3ad3c commit 00694f3

File tree

9 files changed

+11
-70
lines changed

9 files changed

+11
-70
lines changed

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/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/foundation/string.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,6 @@ mod tests {
307307
use alloc::format;
308308
use core::ptr;
309309

310-
#[cfg(feature = "gnustep-1-7")]
311-
#[test]
312-
fn ensure_linkage() {
313-
unsafe { objc2::__gnustep_hack::get_class_to_force_linkage() };
314-
}
315-
316310
#[test]
317311
fn test_equality() {
318312
let s1 = NSString::from_str("abc");

objc2/src/lib.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,6 @@ mod test_utils;
219219
#[cfg(feature = "malloc")]
220220
mod verify;
221221

222-
// Hack to make doctests work
223-
#[cfg(all(feature = "apple", feature = "unstable-static-class"))]
224-
#[link(name = "Foundation", kind = "framework")]
225-
extern "C" {}
226-
227222
/// Hacky way to make GNUStep link properly to Foundation while testing.
228223
///
229224
/// This is a temporary solution to make our CI work for now!
@@ -232,8 +227,6 @@ extern "C" {}
232227
pub mod __gnustep_hack {
233228
use super::runtime::Class;
234229

235-
#[link(name = "gnustep-base", kind = "dylib")]
236-
// This linking doesn't have to be on the correct `extern` block.
237230
extern "C" {
238231
// The linking changed in libobjc2 v2.0
239232
#[cfg_attr(feature = "gnustep-2-0", link_name = "._OBJC_CLASS_NSObject")]

objc2/tests/id_retain_autoreleased.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ fn ensure_linkage() {
1010
unsafe { objc2::__gnustep_hack::get_class_to_force_linkage() };
1111
}
1212

13-
#[cfg(feature = "apple")]
14-
#[link(name = "Foundation", kind = "framework")]
15-
extern "C" {}
16-
1713
fn retain_count(obj: &Object) -> usize {
1814
unsafe { msg_send![obj, retainCount] }
1915
}

objc2/tests/no_prelude.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ fn ensure_linkage() {
1515
unsafe { new_objc2::__gnustep_hack::get_class_to_force_linkage() };
1616
}
1717

18-
#[cfg(feature = "apple")]
19-
#[link(name = "Foundation", kind = "framework")]
20-
extern "C" {}
21-
2218
mod core {}
2319
mod std {}
2420
mod libc {}

objc2/tests/use_macros.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ fn ensure_linkage() {
77
unsafe { objc2::__gnustep_hack::get_class_to_force_linkage() };
88
}
99

10-
#[cfg(feature = "apple")]
11-
#[link(name = "Foundation", kind = "framework")]
12-
extern "C" {}
13-
1410
#[test]
1511
fn use_class_and_msg_send() {
1612
unsafe {

test-assembly/crates/test_static_class/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
use objc2::class;
44
use objc2::runtime::Class;
55

6-
#[cfg(feature = "apple")]
7-
#[link(name = "Foundation", kind = "framework")]
8-
extern "C" {}
9-
106
#[no_mangle]
117
fn get_class() -> &'static Class {
128
class!(NSObject)

0 commit comments

Comments
 (0)