Skip to content

Commit 981db60

Browse files
committed
cfg-guard things that don't work on OS versions
1 parent 320e4f4 commit 981db60

File tree

7 files changed

+59
-18
lines changed

7 files changed

+59
-18
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,10 @@ jobs:
206206
- name: Setup SDK environment
207207
if: matrix.sdk
208208
# This changes a variable, so is only set when a custom SDK is used
209-
run: echo "SDKROOT=$HOME/extern/sdk" >> $GITHUB_ENV
209+
run: |
210+
echo "SDKROOT=$HOME/extern/sdk" >> $GITHUB_ENV
211+
# Temporary
212+
echo "RUSTFLAGS=$RUSTFLAGS --cfg=macos_10_7" >> $GITHUB_ENV
210213
211214
- name: Install Clang & Valgrind
212215
if: contains(matrix.os, 'ubuntu')

objc2-foundation/examples/declaration.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1+
#[cfg(all(feature = "apple", target_os = "macos"))]
12
use objc2::{
23
msg_send, msg_send_id,
34
rc::{Id, Shared},
45
runtime::{Bool, Object},
56
};
7+
#[cfg(all(feature = "apple", target_os = "macos"))]
68
use objc2_foundation::{declare_class, extern_class, NSObject};
79

810
#[cfg(all(feature = "apple", target_os = "macos"))]
911
#[link(name = "AppKit", kind = "framework")]
1012
extern "C" {}
1113

14+
#[cfg(all(feature = "apple", target_os = "macos"))]
1215
extern_class! {
1316
unsafe struct NSResponder: NSObject;
1417
}
1518

19+
#[cfg(all(feature = "apple", target_os = "macos"))]
1620
declare_class! {
1721
unsafe struct CustomAppDelegate: NSResponder, NSObject {
1822
pub ivar: u8,
@@ -66,6 +70,7 @@ declare_class! {
6670
}
6771
}
6872

73+
#[cfg(all(feature = "apple", target_os = "macos"))]
6974
impl CustomAppDelegate {
7075
pub fn new(ivar: u8, another_ivar: bool) -> Id<Self, Shared> {
7176
let cls = Self::class();
@@ -80,9 +85,15 @@ impl CustomAppDelegate {
8085
}
8186
}
8287

88+
#[cfg(all(feature = "apple", target_os = "macos"))]
8389
fn main() {
8490
let delegate = CustomAppDelegate::new(42, true);
8591

8692
println!("{}", delegate.ivar);
8793
println!("{}", delegate.another_ivar.as_bool());
8894
}
95+
96+
#[cfg(not(all(feature = "apple", target_os = "macos")))]
97+
fn main() {
98+
panic!("This example uses AppKit, which is only present on macOS");
99+
}

objc2-foundation/examples/nspasteboard.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::ops::{Deref, DerefMut};
77

88
use objc2::rc::{Id, Shared};
99
use objc2::runtime::{Class, Object};
10-
use objc2::{class, msg_send, msg_send_bool, msg_send_id};
10+
use objc2::{msg_send, msg_send_bool, msg_send_id};
1111
use objc2::{Encoding, Message, RefEncode};
1212

1313
use objc2_foundation::{NSArray, NSDictionary, NSInteger, NSObject, NSString};
@@ -59,7 +59,14 @@ impl DerefMut for NSPasteboard {
5959
impl NSPasteboard {
6060
/// Common convenience method.
6161
pub fn class() -> &'static Class {
62-
class!(NSPasteboard)
62+
#[cfg(all(feature = "apple", target_os = "macos"))]
63+
{
64+
objc2::class!(NSPasteboard)
65+
}
66+
#[cfg(not(all(feature = "apple", target_os = "macos")))]
67+
{
68+
panic!("this example only works on macOS")
69+
}
6370
}
6471

6572
/// We return a `Shared` `Id` because `general` can easily be called

objc2-foundation/examples/nsspeechsynthesizer.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::time::Duration;
88

99
use objc2::rc::{Id, Owned};
1010
use objc2::runtime::Class;
11-
use objc2::{class, msg_send, msg_send_bool, msg_send_id};
11+
use objc2::{msg_send, msg_send_bool, msg_send_id};
1212
use objc2::{Encoding, Message, RefEncode};
1313

1414
use objc2_foundation::{NSObject, NSString};
@@ -47,7 +47,14 @@ impl DerefMut for NSSpeechSynthesizer {
4747
// TODO: Unsure about when to use `&mut` here?
4848
impl NSSpeechSynthesizer {
4949
pub fn class() -> &'static Class {
50-
class!(NSSpeechSynthesizer)
50+
#[cfg(all(feature = "apple", target_os = "macos"))]
51+
{
52+
objc2::class!(NSSpeechSynthesizer)
53+
}
54+
#[cfg(not(all(feature = "apple", target_os = "macos")))]
55+
{
56+
panic!("this example only works on macOS")
57+
}
5158
}
5259

5360
// Uses default voice

objc2-foundation/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pub use self::process_info::NSProcessInfo;
6666
pub use self::range::NSRange;
6767
pub use self::string::NSString;
6868
pub use self::thread::{is_main_thread, is_multi_threaded, MainThreadMarker, NSThread};
69+
#[cfg(not(macos_10_7))] // Temporary
6970
pub use self::uuid::NSUUID;
7071
pub use self::value::NSValue;
7172
pub use self::zone::NSZone;
@@ -112,6 +113,8 @@ mod process_info;
112113
mod range;
113114
mod string;
114115
mod thread;
116+
// Temporarily disable testing UUID on macOS 10.7 until
117+
#[cfg(not(macos_10_7))]
115118
mod uuid;
116119
mod value;
117120
mod zone;
@@ -175,6 +178,7 @@ mod tests {
175178
assert_auto_traits::<NSString>();
176179
assert_unwindsafe::<MainThreadMarker>(); // Intentional
177180
assert_auto_traits::<NSThread>();
181+
#[cfg(not(macos_10_7))]
178182
assert_auto_traits::<NSUUID>();
179183
assert_auto_traits::<NSValue<i32>>();
180184
assert_unwindsafe::<NSZone>(); // Intentional

objc2-foundation/src/uuid.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ extern_class! {
1515
/// Conversion methods to/from UUIDs from the `uuid` crate can be
1616
/// enabled with the `uuid` crate feature.
1717
///
18+
/// macOS: This is only available on 10.8 and above.
19+
///
1820
/// See [Apple's documentation](https://developer.apple.com/documentation/foundation/nsuuid?language=objc).
1921
#[derive(PartialEq, Eq, Hash)]
2022
unsafe pub struct NSUUID: NSObject;

objc2/examples/talk_to_me.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,29 @@
33
//! **Untested**!
44
//!
55
//! Works on macOS >= 10.15 or iOS > 7.0!
6-
use objc2::ffi::NSUInteger;
7-
use objc2::rc::{Id, Owned, Shared};
8-
use objc2::runtime::Object;
9-
use objc2::{class, msg_send, msg_send_bool, msg_send_id};
10-
use std::ffi::c_void;
116
12-
#[cfg(feature = "apple")]
13-
#[link(name = "AVFoundation", kind = "framework")]
14-
extern "C" {}
15-
#[cfg(feature = "apple")]
16-
#[link(name = "Foundation", kind = "framework")]
17-
extern "C" {}
18-
19-
const UTF8_ENCODING: NSUInteger = 4;
7+
#[cfg(not(talk_to_me_example))]
8+
fn main() {
9+
panic!("pass the `--cfg talk_to_me_example` flag to run this example!");
10+
}
2011

12+
#[cfg(talk_to_me_example)]
2113
fn main() {
14+
use objc2::ffi::NSUInteger;
15+
use objc2::rc::{Id, Owned, Shared};
16+
use objc2::runtime::Object;
17+
use objc2::{class, msg_send, msg_send_bool, msg_send_id};
18+
use std::ffi::c_void;
19+
20+
#[cfg(feature = "apple")]
21+
#[link(name = "AVFoundation", kind = "framework")]
22+
extern "C" {}
23+
#[cfg(feature = "apple")]
24+
#[link(name = "Foundation", kind = "framework")]
25+
extern "C" {}
26+
27+
const UTF8_ENCODING: NSUInteger = 4;
28+
2229
let text = "Hello from Rust!";
2330

2431
// Note: objc2-foundation has functionality to do this safely!

0 commit comments

Comments
 (0)