Skip to content

Commit

Permalink
Merge pull request #165 from madsmtm/small-fixes
Browse files Browse the repository at this point in the history
Small fixes
  • Loading branch information
madsmtm committed Jun 13, 2022
2 parents d5733cb + 7acb719 commit e2064a7
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 10 deletions.
3 changes: 3 additions & 0 deletions objc2-encode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Unreleased - YYYY-MM-DD

### Changed
* **BREAKING**: Renamed `Encoding::C_U_LONG` to `Encoding::C_ULONG`.


## 2.0.0-pre.0 - 2022-06-13

Expand Down
6 changes: 3 additions & 3 deletions objc2-encode/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ impl Encoding<'_> {
/// The encoding of [`c_ulong`](`std::os::raw::c_ulong`).
///
/// See [`Encoding::C_LONG`] for explanation.
pub const C_U_LONG: Self = {
pub const C_ULONG: Self = {
if cfg!(any(target_pointer_width = "32", windows)) {
// @encode(unsigned long) = 'L'
Encoding::ULong
Self::ULong
} else {
// @encode(unsigned long) = 'Q'
Encoding::ULongLong
Self::ULongLong
}
};

Expand Down
6 changes: 3 additions & 3 deletions objc2-foundation/src/enumerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ unsafe impl<T: Message> Encode for NSFastEnumerationState<T> {
const ENCODING: Encoding<'static> = Encoding::Struct(
"?",
&[
Encoding::C_U_LONG,
Encoding::C_ULONG,
Encoding::Pointer(&Encoding::Object), // <*const *const T>::ENCODING
Encoding::Pointer(&Encoding::C_U_LONG),
Encoding::Array(5, &Encoding::C_U_LONG),
Encoding::Pointer(&Encoding::C_ULONG),
Encoding::Array(5, &Encoding::C_ULONG),
],
);
}
Expand Down
18 changes: 17 additions & 1 deletion objc2-foundation/src/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ impl NSThread {
let obj: *mut NSString = unsafe { msg_send![self, name] };
unsafe { Id::retain_autoreleased(obj) }
}

fn new() -> Id<Self, Shared> {
let obj: *mut Self = unsafe { msg_send![Self::class(), new] };
unsafe { Id::new(obj) }.unwrap()
}

fn start(&self) {
unsafe { msg_send![self, start] }
}
}

/// Whether the application is multithreaded according to Cocoa.
Expand All @@ -53,14 +62,21 @@ pub fn is_main_thread() -> bool {
unsafe { msg_send_bool![NSThread::class(), isMainThread] }
}

#[allow(unused)]
fn make_multithreaded() {
let thread = NSThread::new();
thread.start();
// Don't bother waiting for it to complete!
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[cfg_attr(
feature = "gnustep-1-7",
should_panic = "Could not retrieve main thread"
ignore = "Retrieving main thread is weirdly broken, only works with --test-threads=1"
)]
fn test_main_thread() {
let current = NSThread::current();
Expand Down
48 changes: 48 additions & 0 deletions objc2/src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ fn log2_align_of<T>() -> u8 {
/// before registering it.
#[derive(Debug)]
pub struct ClassBuilder {
// Note: Don't ever construct a &mut Class, since it is possible to get
// this pointer using `Class::classes`!
cls: NonNull<Class>,
}

Expand Down Expand Up @@ -452,8 +454,31 @@ impl ProtocolBuilder {

#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils;

#[test]
fn test_classbuilder_duplicate() {
let cls = test_utils::custom_class();
let builder = ClassBuilder::new("TestClassBuilderDuplicate", cls).unwrap();
let _ = builder.register();

assert!(ClassBuilder::new("TestClassBuilderDuplicate", cls).is_none());
}

#[test]
#[cfg_attr(
feature = "gnustep-1-7",
ignore = "Dropping ClassBuilder has weird threading side effects on GNUStep"
)]
fn test_classbuilder_drop() {
let cls = test_utils::custom_class();
let builder = ClassBuilder::new("TestClassBuilderDrop", cls).unwrap();
drop(builder);
// After we dropped the class, we can create a new one with the same name:
let _builder = ClassBuilder::new("TestClassBuilderDrop", cls).unwrap();
}

#[test]
fn test_custom_class() {
// Registering the custom class is in test_utils
Expand All @@ -463,6 +488,29 @@ mod tests {
assert_eq!(result, 13);
}

#[test]
#[cfg(feature = "malloc")]
fn test_in_all_classes() {
fn assert_is_present(cls: *const Class) {
// Check that the class is present in Class::classes()
assert!(Class::classes()
.into_iter()
.find(|&item| ptr::eq(cls, *item))
.is_some());
}

let superclass = test_utils::custom_class();
let builder = ClassBuilder::new("TestFetchWhileCreatingClass", superclass).unwrap();

if cfg!(feature = "apple") {
// It is IMO a bug in Apple's runtime that it is present here
assert_is_present(builder.cls.as_ptr());
}

let cls = builder.register();
assert_is_present(cls);
}

#[test]
fn test_class_method() {
let cls = test_utils::custom_class();
Expand Down
6 changes: 3 additions & 3 deletions tests/src/test_encode_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ assert_inner!(enc ENCODING_LONG => Encoding::C_LONG);
assert_inner!(enc ENCODING_LONG_POINTER => Encoding::Pointer(&Encoding::C_LONG));
assert_inner!(str ENCODING_LONG_ATOMIC => format!("A{}", Encoding::C_LONG));

assert_inner!(enc ENCODING_UNSIGNED_LONG => Encoding::C_U_LONG);
assert_inner!(enc ENCODING_UNSIGNED_LONG_POINTER => Encoding::Pointer(&Encoding::C_U_LONG));
assert_inner!(str ENCODING_UNSIGNED_LONG_ATOMIC => format!("A{}", Encoding::C_U_LONG));
assert_inner!(enc ENCODING_UNSIGNED_LONG => Encoding::C_ULONG);
assert_inner!(enc ENCODING_UNSIGNED_LONG_POINTER => Encoding::Pointer(&Encoding::C_ULONG));
assert_inner!(str ENCODING_UNSIGNED_LONG_ATOMIC => format!("A{}", Encoding::C_ULONG));

// No appropriate Rust types for these:

Expand Down

0 comments on commit e2064a7

Please sign in to comment.