-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Emit main thread requirements on protocols
- Loading branch information
Showing
15 changed files
with
212 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule generated
updated
from cf4125 to eaa615
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
crates/test-ui/ui/declare_class_delegate_not_mainthreadonly.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//! Test that implementing `NSApplicationDelegate` and similar requires | ||
//! a `MainThreadOnly` class. | ||
use icrate::AppKit::{NSApplication, NSApplicationDelegate}; | ||
use icrate::Foundation::{MainThreadMarker, NSNotification, NSObject, NSObjectProtocol}; | ||
use objc2::rc::Id; | ||
use objc2::runtime::ProtocolObject; | ||
use objc2::{declare_class, extern_methods, mutability, ClassType}; | ||
|
||
declare_class!( | ||
struct CustomObject; | ||
|
||
unsafe impl ClassType for CustomObject { | ||
type Super = NSObject; | ||
type Mutability = mutability::InteriorMutable; // Not `MainThreadOnly` | ||
const NAME: &'static str = "CustomObject"; | ||
} | ||
|
||
unsafe impl NSObjectProtocol for CustomObject {} | ||
|
||
unsafe impl NSApplicationDelegate for CustomObject { | ||
#[method(applicationDidFinishLaunching:)] | ||
unsafe fn application_did_finish_launching(&self, _notification: &NSNotification) { | ||
// Unclear for the user how to get a main thread marker if `self` is not `MainThreadOnly` | ||
let _mtm = MainThreadMarker::new().unwrap(); | ||
} | ||
} | ||
); | ||
|
||
extern_methods!( | ||
unsafe impl CustomObject { | ||
#[method_id(new)] | ||
fn new(mtm: MainThreadMarker) -> Id<Self>; | ||
} | ||
); | ||
|
||
fn main() { | ||
let mtm = MainThreadMarker::new().unwrap(); | ||
let app = NSApplication::sharedApplication(mtm); | ||
|
||
let delegate = CustomObject::new(mtm); | ||
let delegate = ProtocolObject::from_ref(&*delegate); | ||
app.setDelegate(Some(delegate)); | ||
} |
21 changes: 21 additions & 0 deletions
21
crates/test-ui/ui/declare_class_delegate_not_mainthreadonly.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
error[E0277]: the trait bound `InteriorMutable: mutability::MutabilityIsMainThreadOnly` is not satisfied | ||
--> ui/declare_class_delegate_not_mainthreadonly.rs | ||
| | ||
| unsafe impl NSApplicationDelegate for CustomObject { | ||
| ^^^^^^^^^^^^ the trait `mutability::MutabilityIsMainThreadOnly` is not implemented for `InteriorMutable` | ||
| | ||
= help: the trait `mutability::MutabilityIsMainThreadOnly` is implemented for `MainThreadOnly` | ||
= note: required for `CustomObject` to implement `IsMainThreadOnly` | ||
note: required by a bound in `NSApplicationDelegate` | ||
--> $WORKSPACE/crates/icrate/src/generated/AppKit/NSApplication.rs | ||
| | ||
| / extern_protocol!( | ||
| | pub unsafe trait NSApplicationDelegate: NSObjectProtocol + IsMainThreadOnly { | ||
| | --------------------- required by a bound in this trait | ||
| | #[cfg(feature = "AppKit_NSApplication")] | ||
| | #[optional] | ||
... | | ||
| | unsafe impl ProtocolType for dyn NSApplicationDelegate {} | ||
| | ); | ||
| |_^ required by this bound in `NSApplicationDelegate` | ||
= note: this error originates in the macro `extern_protocol` (in Nightly builds, run with -Z macro-backtrace for more info) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//! Test that implementing traits like `NSApplicationDelegate` requires super | ||
//! protocols like `NSObjectProtocol` to also be implemented. | ||
use icrate::AppKit::NSApplicationDelegate; | ||
use icrate::Foundation::NSObject; | ||
use objc2::{declare_class, mutability, ClassType}; | ||
|
||
declare_class!( | ||
struct CustomObject; | ||
|
||
unsafe impl ClassType for CustomObject { | ||
type Super = NSObject; | ||
type Mutability = mutability::MainThreadOnly; | ||
const NAME: &'static str = "CustomObject"; | ||
} | ||
|
||
unsafe impl NSApplicationDelegate for CustomObject {} | ||
); | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
error[E0277]: the trait bound `CustomObject: NSObjectProtocol` is not satisfied | ||
--> ui/implement_protocol_missing_super.rs | ||
| | ||
| unsafe impl NSApplicationDelegate for CustomObject {} | ||
| ^^^^^^^^^^^^ the trait `NSObjectProtocol` is not implemented for `CustomObject` | ||
| | ||
= help: the following other types implement trait `NSObjectProtocol`: | ||
ProtocolObject<T> | ||
NSObject | ||
__NSProxy | ||
NSApplication | ||
NSCollectionView | ||
NSCollectionLayoutSection | ||
NSCollectionLayoutGroupCustomItem | ||
NSControl | ||
and $N others | ||
note: required by a bound in `NSApplicationDelegate` | ||
--> $WORKSPACE/crates/icrate/src/generated/AppKit/NSApplication.rs | ||
| | ||
| / extern_protocol!( | ||
| | pub unsafe trait NSApplicationDelegate: NSObjectProtocol + IsMainThreadOnly { | ||
| | --------------------- required by a bound in this trait | ||
| | #[cfg(feature = "AppKit_NSApplication")] | ||
| | #[optional] | ||
... | | ||
| | unsafe impl ProtocolType for dyn NSApplicationDelegate {} | ||
| | ); | ||
| |_^ required by this bound in `NSApplicationDelegate` | ||
= note: this error originates in the macro `extern_protocol` (in Nightly builds, run with -Z macro-backtrace for more info) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.