Skip to content

Commit e2064a7

Browse files
authored
Merge pull request #165 from madsmtm/small-fixes
Small fixes
2 parents d5733cb + 7acb719 commit e2064a7

File tree

6 files changed

+77
-10
lines changed

6 files changed

+77
-10
lines changed

objc2-encode/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66

77
## Unreleased - YYYY-MM-DD
88

9+
### Changed
10+
* **BREAKING**: Renamed `Encoding::C_U_LONG` to `Encoding::C_ULONG`.
11+
912

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

objc2-encode/src/encoding.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,13 @@ impl Encoding<'_> {
161161
/// The encoding of [`c_ulong`](`std::os::raw::c_ulong`).
162162
///
163163
/// See [`Encoding::C_LONG`] for explanation.
164-
pub const C_U_LONG: Self = {
164+
pub const C_ULONG: Self = {
165165
if cfg!(any(target_pointer_width = "32", windows)) {
166166
// @encode(unsigned long) = 'L'
167-
Encoding::ULong
167+
Self::ULong
168168
} else {
169169
// @encode(unsigned long) = 'Q'
170-
Encoding::ULongLong
170+
Self::ULongLong
171171
}
172172
};
173173

objc2-foundation/src/enumerator.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ unsafe impl<T: Message> Encode for NSFastEnumerationState<T> {
5757
const ENCODING: Encoding<'static> = Encoding::Struct(
5858
"?",
5959
&[
60-
Encoding::C_U_LONG,
60+
Encoding::C_ULONG,
6161
Encoding::Pointer(&Encoding::Object), // <*const *const T>::ENCODING
62-
Encoding::Pointer(&Encoding::C_U_LONG),
63-
Encoding::Array(5, &Encoding::C_U_LONG),
62+
Encoding::Pointer(&Encoding::C_ULONG),
63+
Encoding::Array(5, &Encoding::C_ULONG),
6464
],
6565
);
6666
}

objc2-foundation/src/thread.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ impl NSThread {
4141
let obj: *mut NSString = unsafe { msg_send![self, name] };
4242
unsafe { Id::retain_autoreleased(obj) }
4343
}
44+
45+
fn new() -> Id<Self, Shared> {
46+
let obj: *mut Self = unsafe { msg_send![Self::class(), new] };
47+
unsafe { Id::new(obj) }.unwrap()
48+
}
49+
50+
fn start(&self) {
51+
unsafe { msg_send![self, start] }
52+
}
4453
}
4554

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

65+
#[allow(unused)]
66+
fn make_multithreaded() {
67+
let thread = NSThread::new();
68+
thread.start();
69+
// Don't bother waiting for it to complete!
70+
}
71+
5672
#[cfg(test)]
5773
mod tests {
5874
use super::*;
5975

6076
#[test]
6177
#[cfg_attr(
6278
feature = "gnustep-1-7",
63-
should_panic = "Could not retrieve main thread"
79+
ignore = "Retrieving main thread is weirdly broken, only works with --test-threads=1"
6480
)]
6581
fn test_main_thread() {
6682
let current = NSThread::current();

objc2/src/declare.rs

+48
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ fn log2_align_of<T>() -> u8 {
164164
/// before registering it.
165165
#[derive(Debug)]
166166
pub struct ClassBuilder {
167+
// Note: Don't ever construct a &mut Class, since it is possible to get
168+
// this pointer using `Class::classes`!
167169
cls: NonNull<Class>,
168170
}
169171

@@ -452,8 +454,31 @@ impl ProtocolBuilder {
452454

453455
#[cfg(test)]
454456
mod tests {
457+
use super::*;
455458
use crate::test_utils;
456459

460+
#[test]
461+
fn test_classbuilder_duplicate() {
462+
let cls = test_utils::custom_class();
463+
let builder = ClassBuilder::new("TestClassBuilderDuplicate", cls).unwrap();
464+
let _ = builder.register();
465+
466+
assert!(ClassBuilder::new("TestClassBuilderDuplicate", cls).is_none());
467+
}
468+
469+
#[test]
470+
#[cfg_attr(
471+
feature = "gnustep-1-7",
472+
ignore = "Dropping ClassBuilder has weird threading side effects on GNUStep"
473+
)]
474+
fn test_classbuilder_drop() {
475+
let cls = test_utils::custom_class();
476+
let builder = ClassBuilder::new("TestClassBuilderDrop", cls).unwrap();
477+
drop(builder);
478+
// After we dropped the class, we can create a new one with the same name:
479+
let _builder = ClassBuilder::new("TestClassBuilderDrop", cls).unwrap();
480+
}
481+
457482
#[test]
458483
fn test_custom_class() {
459484
// Registering the custom class is in test_utils
@@ -463,6 +488,29 @@ mod tests {
463488
assert_eq!(result, 13);
464489
}
465490

491+
#[test]
492+
#[cfg(feature = "malloc")]
493+
fn test_in_all_classes() {
494+
fn assert_is_present(cls: *const Class) {
495+
// Check that the class is present in Class::classes()
496+
assert!(Class::classes()
497+
.into_iter()
498+
.find(|&item| ptr::eq(cls, *item))
499+
.is_some());
500+
}
501+
502+
let superclass = test_utils::custom_class();
503+
let builder = ClassBuilder::new("TestFetchWhileCreatingClass", superclass).unwrap();
504+
505+
if cfg!(feature = "apple") {
506+
// It is IMO a bug in Apple's runtime that it is present here
507+
assert_is_present(builder.cls.as_ptr());
508+
}
509+
510+
let cls = builder.register();
511+
assert_is_present(cls);
512+
}
513+
466514
#[test]
467515
fn test_class_method() {
468516
let cls = test_utils::custom_class();

tests/src/test_encode_utils.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ assert_inner!(enc ENCODING_LONG => Encoding::C_LONG);
169169
assert_inner!(enc ENCODING_LONG_POINTER => Encoding::Pointer(&Encoding::C_LONG));
170170
assert_inner!(str ENCODING_LONG_ATOMIC => format!("A{}", Encoding::C_LONG));
171171

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

176176
// No appropriate Rust types for these:
177177

0 commit comments

Comments
 (0)