Skip to content

Commit

Permalink
cfg-guard things that don't work on older macOS versions
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jul 27, 2022
1 parent 251912c commit 99cd14d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ jobs:
- name: Setup SDK environment
if: matrix.sdk
# This changes a variable, so is only set when a custom SDK is used
run: echo "SDKROOT=$HOME/extern/sdk" >> $GITHUB_ENV
run: |
echo "SDKROOT=$HOME/extern/sdk" >> $GITHUB_ENV
# Temporary
echo "RUSTFLAGS=$RUSTFLAGS --cfg=macos_10_7" >> $GITHUB_ENV
- name: Install Clang & Valgrind
if: contains(matrix.os, 'ubuntu')
Expand Down
11 changes: 11 additions & 0 deletions objc2-foundation/examples/declaration.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
#[cfg(all(feature = "apple", target_os = "macos"))]
use objc2::{
msg_send, msg_send_id,
rc::{Id, Shared},
runtime::{Bool, Object},
};
#[cfg(all(feature = "apple", target_os = "macos"))]
use objc2_foundation::{declare_class, extern_class, NSObject};

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

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

#[cfg(all(feature = "apple", target_os = "macos"))]
declare_class! {
unsafe struct CustomAppDelegate: NSResponder, NSObject {
pub ivar: u8,
Expand Down Expand Up @@ -66,6 +70,7 @@ declare_class! {
}
}

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

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

println!("{}", delegate.ivar);
println!("{}", delegate.another_ivar.as_bool());
}

#[cfg(not(all(feature = "apple", target_os = "macos")))]
fn main() {
panic!("This example uses AppKit, which is only present on macOS");
}
3 changes: 3 additions & 0 deletions objc2-foundation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub use self::process_info::NSProcessInfo;
pub use self::range::NSRange;
pub use self::string::NSString;
pub use self::thread::{is_main_thread, is_multi_threaded, MainThreadMarker, NSThread};
#[cfg(not(macos_10_7))] // Temporary
pub use self::uuid::NSUUID;
pub use self::value::NSValue;
pub use self::zone::NSZone;
Expand Down Expand Up @@ -112,6 +113,8 @@ mod process_info;
mod range;
mod string;
mod thread;
// Temporarily disable testing UUID on macOS 10.7 until
#[cfg(not(macos_10_7))]
mod uuid;
mod value;
mod zone;
Expand Down
2 changes: 2 additions & 0 deletions objc2-foundation/src/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ extern_class! {
/// Conversion methods to/from UUIDs from the `uuid` crate can be
/// enabled with the `uuid` crate feature.
///
/// macOS: This is only available on 10.8 and above.
///
/// See [Apple's documentation](https://developer.apple.com/documentation/foundation/nsuuid?language=objc).
#[derive(PartialEq, Eq, Hash)]
unsafe pub struct NSUUID: NSObject;
Expand Down
33 changes: 20 additions & 13 deletions objc2/examples/talk_to_me.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,29 @@
//! **Untested**!
//!
//! Works on macOS >= 10.15 or iOS > 7.0!
use objc2::ffi::NSUInteger;
use objc2::rc::{Id, Owned, Shared};
use objc2::runtime::Object;
use objc2::{class, msg_send, msg_send_bool, msg_send_id};
use std::ffi::c_void;

#[cfg(feature = "apple")]
#[link(name = "AVFoundation", kind = "framework")]
extern "C" {}
#[cfg(feature = "apple")]
#[link(name = "Foundation", kind = "framework")]
extern "C" {}

const UTF8_ENCODING: NSUInteger = 4;
#[cfg(not(talk_to_me_example))]
fn main() {
panic!("pass the `--cfg talk_to_me_example` flag to run this example!");
}

#[cfg(talk_to_me_example)]
fn main() {
use objc2::ffi::NSUInteger;
use objc2::rc::{Id, Owned, Shared};
use objc2::runtime::Object;
use objc2::{class, msg_send, msg_send_bool, msg_send_id};
use std::ffi::c_void;

#[cfg(feature = "apple")]
#[link(name = "AVFoundation", kind = "framework")]
extern "C" {}
#[cfg(feature = "apple")]
#[link(name = "Foundation", kind = "framework")]
extern "C" {}

const UTF8_ENCODING: NSUInteger = 4;

let text = "Hello from Rust!";

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

0 comments on commit 99cd14d

Please sign in to comment.