Skip to content

Commit

Permalink
Fix NSValue::new on GNUStep
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Nov 1, 2021
1 parent c38fb60 commit 9b8ce34
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions objc2_foundation/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,31 @@ unsafe impl<T: 'static> INSObject for NSValue<T> {
type Ownership = Shared;

fn class() -> &'static Class {
class!(NSValue)
#[cfg(not(gnustep))]
return class!(NSValue);

#[cfg(gnustep)]
// We can't use NSValue directly, because its `new` method throws an
// exception (instead of just becoming an invalid NSValue). Luckily,
// the `GSValue` subclass has the desired behaviour, so we can just
// use that. Unfortunately, this is less efficient for the following:
// ```
// match T::ENCODING {
// Encoding::Object => ...,
// Encoding::Struct("_NSPoint", _) => ...,
// Encoding::Pointer(&Encoding::Void) => ...,
// Encoding::Struct("_NSRange", _) => ...,
// Encoding::Struct("_NSRect", _) => ...,
// Encoding::Struct("_NSSize", _) => ...,
// }
// ```
//
// See GNUStep's `NSValue +valueClassWithObjCType` and
// `GSConcreteValueTemplate.m` for more, though we can't use the
// classes in there either, because their `new` methods return valid
// objects (and so `<NSValue<NSRange>>::new()` would work differently
// on GNUStep).
return class!(GSValue);
}
}

Expand All @@ -84,7 +108,7 @@ unsafe impl<T: 'static> INSCopying for NSValue<T> {

#[cfg(test)]
mod tests {
use crate::{INSObject, INSValue, NSValue};
use crate::{INSObject, INSValue, NSRange, NSValue};
use objc2::Encode;

#[test]
Expand All @@ -100,4 +124,15 @@ mod tests {
assert!(val.encoding().is_none());
assert!(val.get().is_none());
}

#[test]
fn test_value_nsrange() {
let val = NSValue::from_value(NSRange::from(1..2));
assert!(&NSRange::ENCODING == val.encoding().unwrap());
assert_eq!(val.get().unwrap(), NSRange::from(1..2));

let val = <NSValue<NSRange>>::new();
assert!(val.encoding().is_none());
assert!(val.get().is_none());
}
}

0 comments on commit 9b8ce34

Please sign in to comment.