1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
use alloc::string::ToString;
use core::ffi::c_void;
use core::fmt;
use core::hash;
use core::mem::MaybeUninit;
use core::ptr::NonNull;
use core::str;
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use super::{NSCopying, NSObject, NSPoint, NSRange, NSRect, NSSize};
use crate::rc::{Id, Shared};
use crate::{extern_class, extern_methods, msg_send, msg_send_id, ClassType, Encode};
extern_class!(
/// A container wrapping any encodable type as an Obective-C object.
///
/// Since Objective-C collections like [`NSArray`] can only contain
/// objects, it is common to wrap pointers or structures like [`NSRange`].
///
/// Note that creating `NSValue`s is not `unsafe`, but almost all usage of
/// it is, since we cannot guarantee that the type that was used to
/// construct it is the same as the expected output type.
///
/// See also the [`NSNumber`] subclass for when you want to wrap numbers.
///
/// See [Apple's documentation][apple-doc] for more information.
///
/// [`NSArray`]: super::NSArray
/// [`NSRange`]: super::NSRange
/// [`NSNumber`]: super::NSNumber
/// [apple-doc]: https://developer.apple.com/documentation/foundation/nsnumber?language=objc
pub struct NSValue;
unsafe impl ClassType for NSValue {
type Super = NSObject;
}
);
// We can't implement any auto traits for NSValue, since it can contain an
// arbitary object!
extern_methods!(
/// Creation methods.
unsafe impl NSValue {
// Default / empty new is not provided because `-init` returns `nil` on
// Apple and GNUStep throws an exception on all other messages to this
// invalid instance.
/// Create a new `NSValue` containing the given type.
///
/// Be careful when using this since you may accidentally pass a reference
/// when you wanted to pass a concrete type instead.
///
///
/// # Examples
///
/// Create an `NSValue` containing an [`NSPoint`][super::NSPoint].
///
/// ```
/// use objc2::foundation::{NSPoint, NSValue};
/// # #[cfg(feature = "gnustep-1-7")]
/// # unsafe { objc2::__gnustep_hack::get_class_to_force_linkage() };
/// let val = NSValue::new::<NSPoint>(NSPoint::new(1.0, 1.0));
/// ```
pub fn new<T: 'static + Copy + Encode>(value: T) -> Id<Self, Shared> {
let bytes: *const T = &value;
let bytes: *const c_void = bytes.cast();
let encoding = CString::new(T::ENCODING.to_string()).unwrap();
unsafe {
msg_send_id![
msg_send_id![Self::class(), alloc],
initWithBytes: bytes,
objCType: encoding.as_ptr(),
]
}
}
}
/// Getter methods.
unsafe impl NSValue {
/// Retrieve the data contained in the `NSValue`.
///
/// Note that this is broken on GNUStep for some types, see
/// [gnustep/libs-base#216].
///
/// [gnustep/libs-base#216]: https://github.com/gnustep/libs-base/pull/216
///
///
/// # Safety
///
/// The type of `T` must be what the NSValue actually stores, and any
/// safety invariants that the value has must be upheld.
///
/// Note that it may be, but is not always, enough to simply check whether
/// [`contains_encoding`] returns `true`. For example, `NonNull<T>` have
/// the same encoding as `*const T`, but `NonNull<T>` is clearly not
/// safe to return from this function even if you've checked the encoding
/// beforehand.
///
/// [`contains_encoding`]: Self::contains_encoding
///
///
/// # Examples
///
/// Store a pointer in `NSValue`, and retrieve it again afterwards.
///
/// ```
/// use std::ffi::c_void;
/// use std::ptr;
/// use objc2::foundation::NSValue;
///
/// # #[cfg(feature = "gnustep-1-7")]
/// # unsafe { objc2::__gnustep_hack::get_class_to_force_linkage() };
/// let val = NSValue::new::<*const c_void>(ptr::null());
/// // SAFETY: The value was just created with a pointer
/// let res = unsafe { val.get::<*const c_void>() };
/// assert!(res.is_null());
/// ```
pub unsafe fn get<T: 'static + Copy + Encode>(&self) -> T {
debug_assert!(
self.contains_encoding::<T>(),
"wrong encoding. NSValue tried to return something with encoding {}, but the encoding of the given type was {}",
self.encoding().unwrap_or("(NULL)"),
T::ENCODING,
);
let mut value = MaybeUninit::<T>::uninit();
let ptr: *mut c_void = value.as_mut_ptr().cast();
let _: () = unsafe { msg_send![self, getValue: ptr] };
// SAFETY: We know that `getValue:` initialized the value, and user
// ensures that it is safe to access.
unsafe { value.assume_init() }
}
pub fn get_range(&self) -> Option<NSRange> {
if self.contains_encoding::<NSRange>() {
// SAFETY: We just checked that this contains an NSRange
Some(unsafe { msg_send![self, rangeValue] })
} else {
None
}
}
pub fn get_point(&self) -> Option<NSPoint> {
if self.contains_encoding::<NSPoint>() {
// SAFETY: We just checked that this contains an NSPoint
//
// Note: The documentation says that `pointValue`, `sizeValue` and
// `rectValue` is only available on macOS, but turns out that they
// are actually available everywhere!
let res = unsafe { msg_send![self, pointValue] };
Some(res)
} else {
None
}
}
pub fn get_size(&self) -> Option<NSSize> {
if self.contains_encoding::<NSSize>() {
// SAFETY: We just checked that this contains an NSSize
let res = unsafe { msg_send![self, sizeValue] };
Some(res)
} else {
None
}
}
pub fn get_rect(&self) -> Option<NSRect> {
if self.contains_encoding::<NSRect>() {
// SAFETY: We just checked that this contains an NSRect
let res = unsafe { msg_send![self, rectValue] };
Some(res)
} else {
None
}
}
pub fn encoding(&self) -> Option<&str> {
let result: Option<NonNull<c_char>> = unsafe { msg_send![self, objCType] };
result.map(|s| unsafe { CStr::from_ptr(s.as_ptr()) }.to_str().unwrap())
}
pub fn contains_encoding<T: 'static + Copy + Encode>(&self) -> bool {
if let Some(encoding) = self.encoding() {
T::ENCODING.equivalent_to_str(encoding)
} else {
panic!("missing NSValue encoding");
}
}
#[sel(isEqualToValue:)]
fn is_equal_to_value(&self, other: &Self) -> bool;
}
);
unsafe impl NSCopying for NSValue {
type Ownership = Shared;
type Output = NSValue;
}
impl alloc::borrow::ToOwned for NSValue {
type Owned = Id<NSValue, Shared>;
fn to_owned(&self) -> Self::Owned {
self.copy()
}
}
impl PartialEq for NSValue {
#[doc(alias = "isEqualToValue:")]
fn eq(&self, other: &Self) -> bool {
// Use isEqualToValue: instaed of isEqual: since it is faster
self.is_equal_to_value(other)
}
}
impl hash::Hash for NSValue {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
// Delegate to NSObject
(**self).hash(state)
}
}
impl fmt::Debug for NSValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let enc = self.encoding().unwrap_or("(NULL)");
let bytes = &**self; // Delegate to -[NSObject description]
f.debug_struct("NSValue")
.field("encoding", &enc)
.field("bytes", bytes)
.finish()
}
}
#[cfg(test)]
mod tests {
use alloc::format;
use core::{ptr, slice};
use super::*;
use crate::rc::{RcTestObject, ThreadTestData};
#[test]
fn basic() {
let val = NSValue::new(13u32);
assert_eq!(unsafe { val.get::<u32>() }, 13);
}
#[test]
fn does_not_retain() {
let obj = RcTestObject::new();
let expected = ThreadTestData::current();
let val = NSValue::new::<*const RcTestObject>(&*obj);
expected.assert_current();
assert!(ptr::eq(unsafe { val.get::<*const RcTestObject>() }, &*obj));
expected.assert_current();
let _clone = val.clone();
expected.assert_current();
let _copy = val.copy();
expected.assert_current();
drop(val);
expected.assert_current();
}
#[test]
fn test_equality() {
let val1 = NSValue::new(123u32);
let val2 = NSValue::new(123u32);
assert_eq!(val1, val1);
assert_eq!(val1, val2);
let val3 = NSValue::new(456u32);
assert_ne!(val1, val3);
}
#[test]
fn test_equality_across_types() {
let val1 = NSValue::new(123i32);
let val2 = NSValue::new(123u32);
// Test that `objCType` is checked when comparing equality
assert_ne!(val1, val2);
}
#[test]
#[ignore = "the debug output changes depending on OS version"]
fn test_debug() {
let expected = if cfg!(feature = "gnustep-1-7") {
r#"NSValue { encoding: "C", bytes: (C) <ab> }"#
} else if cfg!(newer_apple) {
r#"NSValue { encoding: "C", bytes: {length = 1, bytes = 0xab} }"#
} else {
r#"NSValue { encoding: "C", bytes: <ab> }"#
};
assert_eq!(format!("{:?}", NSValue::new(171u8)), expected);
}
#[test]
fn nsrange() {
let range = NSRange::from(1..2);
let val = NSValue::new(range);
assert_eq!(val.get_range(), Some(range));
assert_eq!(val.get_point(), None);
assert_eq!(val.get_size(), None);
assert_eq!(val.get_rect(), None);
// NSValue -getValue is broken on GNUStep for some types
#[cfg(not(feature = "gnustep-1-7"))]
assert_eq!(unsafe { val.get::<NSRange>() }, range);
}
#[test]
fn nspoint() {
let point = NSPoint::new(1.0, 2.0);
let val = NSValue::new(point);
assert_eq!(val.get_point(), Some(point));
#[cfg(not(feature = "gnustep-1-7"))]
assert_eq!(unsafe { val.get::<NSPoint>() }, point);
}
#[test]
fn nssize() {
let point = NSSize::new(1.0, 2.0);
let val = NSValue::new(point);
assert_eq!(val.get_size(), Some(point));
#[cfg(not(feature = "gnustep-1-7"))]
assert_eq!(unsafe { val.get::<NSSize>() }, point);
}
#[test]
fn nsrect() {
let rect = NSRect::new(NSPoint::new(1.0, 2.0), NSSize::new(3.0, 4.0));
let val = NSValue::new(rect);
assert_eq!(val.get_rect(), Some(rect));
#[cfg(not(feature = "gnustep-1-7"))]
assert_eq!(unsafe { val.get::<NSRect>() }, rect);
}
#[test]
fn store_str() {
let s = "abc";
let val = NSValue::new(s.as_ptr());
assert!(val.contains_encoding::<*const u8>());
let slice = unsafe { slice::from_raw_parts(val.get(), s.len()) };
let s2 = str::from_utf8(slice).unwrap();
assert_eq!(s2, s);
}
#[test]
fn store_cstr() {
// The following Apple article says that NSValue can't easily store
// C-strings, but apparently that doesn't apply to us!
// <https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/NumbersandValues/Articles/Values.html#//apple_ref/doc/uid/20000174-BAJJHDEG>
let s = CStr::from_bytes_with_nul(b"test123\0").unwrap();
let val = NSValue::new(s.as_ptr());
assert!(val.contains_encoding::<*const c_char>());
let s2 = unsafe { CStr::from_ptr(val.get()) };
assert_eq!(s2, s);
}
}