Skip to main content

objc2/runtime/
nszone.rs

1use core::fmt;
2use core::panic::{RefUnwindSafe, UnwindSafe};
3
4#[cfg(feature = "gnustep-1-7")]
5use crate::encode::Encode;
6use crate::encode::{Encoding, RefEncode};
7use crate::ffi;
8
9/// A type used to identify and manage memory zones.
10///
11/// Zones are ignored on all newer platforms, you should very rarely need to
12/// use this, but may be useful if you need to implement `copyWithZone:` or
13/// `allocWithZone:`.
14///
15/// See [Apple's documentation](https://developer.apple.com/documentation/foundation/nszone?language=objc).
16#[repr(C)]
17pub struct NSZone {
18    _priv: [u8; 0],
19    // Use `OpaqueData` to mark the types as !Send, !Sync and UnsafeCell.
20    _inner: ffi::OpaqueData,
21}
22
23impl fmt::Debug for NSZone {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        write!(f, "<NSZone {self:p}>")
26    }
27}
28
29// Note: We don't know anything about the internals of `NSZone`, so best not
30// to make it `Send` and `Sync` for now.
31
32impl UnwindSafe for NSZone {}
33impl RefUnwindSafe for NSZone {}
34
35unsafe impl RefEncode for NSZone {
36    #[cfg(not(feature = "gnustep-1-7"))]
37    const ENCODING_REF: Encoding = Encoding::Pointer(&Encoding::Struct("_NSZone", &[]));
38    #[cfg(feature = "gnustep-1-7")]
39    const ENCODING_REF: Encoding = Encoding::Pointer(&Encoding::Struct(
40        "_NSZone",
41        &[
42            // Functions
43            Encoding::Pointer(&Encoding::Unknown),
44            Encoding::Pointer(&Encoding::Unknown),
45            Encoding::Pointer(&Encoding::Unknown),
46            Encoding::Pointer(&Encoding::Unknown),
47            Encoding::Pointer(&Encoding::Unknown),
48            Encoding::Pointer(&Encoding::Unknown),
49            // Stats
50            Encoding::Pointer(&Encoding::Unknown),
51            // Zone granularity
52            usize::ENCODING,
53            // Name of zone
54            Encoding::Object,
55            // Next zone - note that the contents of this doesn't matter,
56            // since this is nested far enough that the encoding string ends
57            // up ignoring it.
58            Encoding::Pointer(&Encoding::Struct("_NSZone", &[])),
59        ],
60    ));
61}
62
63#[cfg(test)]
64mod tests {
65    use alloc::string::ToString;
66    use core::ptr;
67
68    use super::*;
69    use crate::msg_send;
70    use crate::rc::Allocated;
71    use crate::runtime::NSObject;
72    use crate::ClassType;
73
74    #[test]
75    fn alloc_with_zone() {
76        let zone: *const NSZone = ptr::null();
77        let _obj: Allocated<NSObject> =
78            unsafe { msg_send![NSObject::class(), allocWithZone: zone] };
79    }
80
81    #[test]
82    fn verify_encoding() {
83        let expected = if cfg!(all(feature = "gnustep-1-7", target_pointer_width = "64")) {
84            "^{_NSZone=^?^?^?^?^?^?^?Q@^{_NSZone}}"
85        } else if cfg!(all(
86            feature = "gnustep-1-7",
87            not(target_pointer_width = "64")
88        )) {
89            "^{_NSZone=^?^?^?^?^?^?^?I@^{_NSZone}}"
90        } else {
91            "^{_NSZone=}"
92        };
93        assert_eq!(NSZone::ENCODING_REF.to_string(), expected);
94    }
95}