Skip to content

Commit e5fda1a

Browse files
committed
Add simple NSProcessInfo
1 parent 7a44ed9 commit e5fda1a

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

objc2-foundation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1616
* Added `NSRange` methods `new`, `is_empty`, `contains` and `end`.
1717
* Added `NSThread` object.
1818
* Added `is_multi_threaded` and `is_main_thread` helper functions.
19+
* Added `NSProcessInfo` object.
1920

2021
### Changed
2122
* **BREAKING**: Removed the following helper traits in favor of inherent

objc2-foundation/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub use self::data::{NSData, NSMutableData};
2828
pub use self::dictionary::NSDictionary;
2929
pub use self::enumerator::{NSEnumerator, NSFastEnumeration, NSFastEnumerator};
3030
pub use self::object::NSObject;
31+
pub use self::process_info::NSProcessInfo;
3132
pub use self::range::NSRange;
3233
pub use self::string::NSString;
3334
pub use self::thread::{is_main_thread, is_multi_threaded, NSThread};
@@ -51,6 +52,7 @@ mod data;
5152
mod dictionary;
5253
mod enumerator;
5354
mod object;
55+
mod process_info;
5456
mod range;
5557
mod string;
5658
mod thread;

objc2-foundation/src/process_info.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use core::ptr::NonNull;
2+
3+
use objc2::msg_send;
4+
use objc2::rc::{Id, Shared};
5+
6+
use crate::{NSObject, NSString};
7+
8+
object! {
9+
/// A collection of information about the current process.
10+
///
11+
/// See [Apple's documentation](https://developer.apple.com/documentation/foundation/nsprocessinfo?language=objc).
12+
unsafe pub struct NSProcessInfo: NSObject;
13+
14+
// TODO: This contains a lot more important functionality!
15+
}
16+
17+
// The documentation explicitly states:
18+
// > NSProcessInfo is thread-safe in macOS 10.7 and later.
19+
unsafe impl Send for NSProcessInfo {}
20+
unsafe impl Sync for NSProcessInfo {}
21+
22+
impl NSProcessInfo {
23+
pub fn process_info() -> Id<NSProcessInfo, Shared> {
24+
// currentThread is @property(strong), what does that mean?
25+
let obj: *mut Self = unsafe { msg_send![Self::class(), processInfo] };
26+
let obj = unsafe { NonNull::new_unchecked(obj) };
27+
unsafe { Id::retain(obj) }
28+
}
29+
30+
pub fn process_name(&self) -> Id<NSString, Shared> {
31+
let obj: *mut NSString = unsafe { msg_send![Self::class(), processName] };
32+
let obj = NonNull::new(obj).unwrap();
33+
unsafe { Id::retain(obj) }
34+
}
35+
}

0 commit comments

Comments
 (0)