Skip to content

Commit 406cc7d

Browse files
committed
Add initial NSBundle
1 parent 7c7efbd commit 406cc7d

File tree

5 files changed

+86
-10
lines changed

5 files changed

+86
-10
lines changed

objc2/CHANGELOG_FOUNDATION.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1313
* Added `NSMutableSet`.
1414
* Added `NSMutableDictionary`.
1515
* Added `NSNotFound`.
16+
* Added `NSBundle`.
1617

1718

1819
## objc2 0.3.0-beta.2 - 2022-08-28

objc2/src/foundation/bundle.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use core::fmt;
2+
use core::panic::{RefUnwindSafe, UnwindSafe};
3+
4+
use super::{NSCopying, NSDictionary, NSObject, NSString};
5+
use crate::rc::{Id, Shared};
6+
use crate::{extern_class, extern_methods, msg_send_id, ns_string, ClassType};
7+
8+
extern_class!(
9+
/// A representation of the code and resources stored in a bundle
10+
/// directory on disk.
11+
///
12+
/// See [Apple's documentation](https://developer.apple.com/documentation/foundation/nsbundle?language=objc).
13+
#[derive(PartialEq, Eq, Hash)]
14+
pub struct NSBundle;
15+
16+
unsafe impl ClassType for NSBundle {
17+
type Super = NSObject;
18+
}
19+
);
20+
21+
// SAFETY: Bundles are documented as thread-safe.
22+
unsafe impl Sync for NSBundle {}
23+
unsafe impl Send for NSBundle {}
24+
25+
impl UnwindSafe for NSBundle {}
26+
impl RefUnwindSafe for NSBundle {}
27+
28+
extern_methods!(
29+
unsafe impl NSBundle {
30+
pub fn main() -> Id<Self, Shared> {
31+
unsafe { msg_send_id![Self::class(), mainBundle] }
32+
}
33+
34+
pub fn info(&self) -> Id<NSDictionary<NSString, NSObject>, Shared> {
35+
unsafe { msg_send_id![self, infoDictionary] }
36+
}
37+
38+
pub fn name(&self) -> Option<Id<NSString, Shared>> {
39+
self.info().get(ns_string!("CFBundleName")).map(|name| {
40+
let ptr: *const NSObject = name;
41+
let ptr: *const NSString = ptr.cast();
42+
// SAFETY: TODO
43+
let name = unsafe { ptr.as_ref().unwrap_unchecked() };
44+
name.copy()
45+
})
46+
}
47+
}
48+
);
49+
50+
impl fmt::Debug for NSBundle {
51+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52+
// Delegate to NSObject
53+
(**self).fmt(f)
54+
}
55+
}
56+
57+
#[cfg(test)]
58+
mod tests {
59+
use super::*;
60+
use alloc::format;
61+
use std::println;
62+
63+
#[test]
64+
#[cfg_attr(not(target_os = "macos"), ignore = "varies between platforms")]
65+
fn try_running_functions() {
66+
// This is mostly empty since cargo doesn't bundle the application
67+
// before executing.
68+
let bundle = NSBundle::main();
69+
println!("{:?}", bundle);
70+
assert_eq!(format!("{:?}", bundle.info()), "{}");
71+
assert_eq!(bundle.name(), None);
72+
}
73+
}

objc2/src/foundation/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353

5454
pub use self::array::NSArray;
5555
pub use self::attributed_string::{NSAttributedString, NSAttributedStringKey};
56+
pub use self::bundle::NSBundle;
5657
pub use self::comparison_result::NSComparisonResult;
5758
pub use self::copying::{NSCopying, NSMutableCopying};
5859
pub use self::data::NSData;
@@ -102,6 +103,7 @@ extern "C" {}
102103
pub mod __ns_string;
103104
mod array;
104105
mod attributed_string;
106+
mod bundle;
105107
mod comparison_result;
106108
mod copying;
107109
mod data;

test-ui/ui/msg_send_id_invalid_return.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ error[E0277]: the trait bound `objc2::runtime::Class: Message` is not satisfied
2727
Exception
2828
NSArray<T, O>
2929
NSAttributedString
30+
NSBundle
3031
NSData
3132
NSDictionary<K, V>
3233
NSError
3334
NSException
34-
NSMutableArray<T, O>
35-
and 14 others
35+
and 15 others
3636
= note: required for `RetainSemantics<true, false, false, false>` to implement `MsgSendId<&objc2::runtime::Class, objc2::runtime::Class, Shared>`
3737

3838
error[E0277]: the trait bound `objc2::runtime::Class: Message` is not satisfied
@@ -48,12 +48,12 @@ error[E0277]: the trait bound `objc2::runtime::Class: Message` is not satisfied
4848
Exception
4949
NSArray<T, O>
5050
NSAttributedString
51+
NSBundle
5152
NSData
5253
NSDictionary<K, V>
5354
NSError
5455
NSException
55-
NSMutableArray<T, O>
56-
and 14 others
56+
and 15 others
5757
= note: required for `RetainSemantics<true, false, false, false>` to implement `MsgSendId<&objc2::runtime::Class, objc2::runtime::Class, Shared>`
5858

5959
error[E0277]: the trait bound `&objc2::runtime::Object: MaybeUnwrap<Allocated<_>, _>` is not satisfied
@@ -85,12 +85,12 @@ error[E0277]: the trait bound `objc2::runtime::Class: Message` is not satisfied
8585
Exception
8686
NSArray<T, O>
8787
NSAttributedString
88+
NSBundle
8889
NSData
8990
NSDictionary<K, V>
9091
NSError
9192
NSException
92-
NSMutableArray<T, O>
93-
and 14 others
93+
and 15 others
9494
= note: required for `RetainSemantics<false, true, false, false>` to implement `MsgSendId<&objc2::runtime::Class, Allocated<objc2::runtime::Class>, Shared>`
9595

9696
error[E0277]: the trait bound `Id<objc2::runtime::Object, Shared>: MaybeUnwrap<Allocated<_>, _>` is not satisfied

test-ui/ui/msg_send_super_not_classtype.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ error[E0277]: the trait bound `objc2::runtime::Object: ClassType` is not satisfi
1010
= help: the following other types implement trait `ClassType`:
1111
NSArray<T, O>
1212
NSAttributedString
13+
NSBundle
1314
NSData
1415
NSDictionary<K, V>
1516
NSError
1617
NSException
1718
NSMutableArray<T, O>
18-
NSMutableAttributedString
19-
and 12 others
19+
and 13 others
2020
note: required by a bound in `__send_super_message_static`
2121
--> $WORKSPACE/objc2/src/message/mod.rs
2222
|
@@ -35,13 +35,13 @@ error[E0277]: the trait bound `objc2::runtime::Object: ClassType` is not satisfi
3535
= help: the following other types implement trait `ClassType`:
3636
NSArray<T, O>
3737
NSAttributedString
38+
NSBundle
3839
NSData
3940
NSDictionary<K, V>
4041
NSError
4142
NSException
4243
NSMutableArray<T, O>
43-
NSMutableAttributedString
44-
and 12 others
44+
and 13 others
4545
note: required by a bound in `__send_super_message_static`
4646
--> $WORKSPACE/objc2/src/message/mod.rs
4747
|

0 commit comments

Comments
 (0)