Skip to content

Commit

Permalink
Use extern_methods! functionality
Browse files Browse the repository at this point in the history
To cut down on the amount of code, which should make things easier to review and understand.

This uses features which are not actually yet done, see #244.

Not happy with the formatting either, but not sure how to fix that?
  • Loading branch information
madsmtm committed Nov 1, 2022
1 parent b7b9feb commit 9328d14
Show file tree
Hide file tree
Showing 168 changed files with 9,302 additions and 16,873 deletions.
2 changes: 1 addition & 1 deletion header-translator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl RustFile {

let tokens = quote! {
#[allow(unused_imports)]
use objc2::{ClassType, extern_class, extern_methods, msg_send, msg_send_id};
use objc2::{ClassType, extern_class, extern_methods};
#[allow(unused_imports)]
use objc2::rc::{Id, Shared};

Expand Down
38 changes: 10 additions & 28 deletions header-translator/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,31 +332,15 @@ impl ToTokens for Method {
.map(|(param, arg_ty)| quote!(#param: #arg_ty))
.collect();

let method_call = if self.selector.contains(':') {
let split_selector: Vec<_> = self
let selector = if self.selector.contains(':') {
let iter = self
.selector
.split(':')
.filter(|sel| !sel.is_empty())
.collect();
.map(|sel| format_ident!("{}", sel));

assert!(
arguments.len() + (is_error as usize) == split_selector.len(),
"incorrect method argument length",
);

let iter = arguments
.into_iter()
.map(|(param, _)| param)
.chain(is_error.then(|| format_ident!("_")))
.zip(split_selector)
.map(|(param, sel)| {
let sel = format_ident!("{}", sel);
quote!(#sel: #param)
});

quote!(#(#iter),*)
quote!(#(#iter:)*)
} else {
assert_eq!(arguments.len(), 0, "too many arguments");
let sel = format_ident!("{}", self.selector);
quote!(#sel)
};
Expand All @@ -369,24 +353,22 @@ impl ToTokens for Method {
};

let macro_name = if self.result_type.is_id() {
format_ident!("msg_send_id")
format_ident!("method_id")
} else {
format_ident!("msg_send")
format_ident!("method")
};

let unsafe_ = if self.safe { quote!() } else { quote!(unsafe) };

let result = if self.is_class {
quote! {
pub #unsafe_ fn #fn_name(#(#fn_args),*) #ret {
#macro_name![Self::class(), #method_call]
}
#[#macro_name(#selector)]
pub #unsafe_ fn #fn_name(#(#fn_args),*) #ret;
}
} else {
quote! {
pub #unsafe_ fn #fn_name(&self #(, #fn_args)*) #ret {
#macro_name![self, #method_call]
}
#[#macro_name(#selector)]
pub #unsafe_ fn #fn_name(&self #(, #fn_args)*) #ret;
}
};
tokens.append_all(result);
Expand Down
2 changes: 1 addition & 1 deletion icrate/src/Foundation/generated/FoundationErrors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ use crate::Foundation::generated::NSObject::*;
#[allow(unused_imports)]
use objc2::rc::{Id, Shared};
#[allow(unused_imports)]
use objc2::{extern_class, extern_methods, msg_send, msg_send_id, ClassType};
use objc2::{extern_class, extern_methods, ClassType};
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ use crate::Foundation::generated::NSObjCRuntime::*;
#[allow(unused_imports)]
use objc2::rc::{Id, Shared};
#[allow(unused_imports)]
use objc2::{extern_class, extern_methods, msg_send, msg_send_id, ClassType};
use objc2::{extern_class, extern_methods, ClassType};
77 changes: 31 additions & 46 deletions icrate/src/Foundation/generated/NSAffineTransform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::Foundation::generated::NSObject::*;
#[allow(unused_imports)]
use objc2::rc::{Id, Shared};
#[allow(unused_imports)]
use objc2::{extern_class, extern_methods, msg_send, msg_send_id, ClassType};
use objc2::{extern_class, extern_methods, ClassType};
extern_class!(
#[derive(Debug)]
pub struct NSAffineTransform;
Expand All @@ -14,50 +14,35 @@ extern_class!(
);
extern_methods!(
unsafe impl NSAffineTransform {
pub unsafe fn transform() -> Id<NSAffineTransform, Shared> {
msg_send_id![Self::class(), transform]
}
pub unsafe fn initWithTransform(&self, transform: &NSAffineTransform) -> Id<Self, Shared> {
msg_send_id![self, initWithTransform: transform]
}
pub unsafe fn init(&self) -> Id<Self, Shared> {
msg_send_id![self, init]
}
pub unsafe fn translateXBy_yBy(&self, deltaX: CGFloat, deltaY: CGFloat) {
msg_send![self, translateXBy: deltaX, yBy: deltaY]
}
pub unsafe fn rotateByDegrees(&self, angle: CGFloat) {
msg_send![self, rotateByDegrees: angle]
}
pub unsafe fn rotateByRadians(&self, angle: CGFloat) {
msg_send![self, rotateByRadians: angle]
}
pub unsafe fn scaleBy(&self, scale: CGFloat) {
msg_send![self, scaleBy: scale]
}
pub unsafe fn scaleXBy_yBy(&self, scaleX: CGFloat, scaleY: CGFloat) {
msg_send![self, scaleXBy: scaleX, yBy: scaleY]
}
pub unsafe fn invert(&self) {
msg_send![self, invert]
}
pub unsafe fn appendTransform(&self, transform: &NSAffineTransform) {
msg_send![self, appendTransform: transform]
}
pub unsafe fn prependTransform(&self, transform: &NSAffineTransform) {
msg_send![self, prependTransform: transform]
}
pub unsafe fn transformPoint(&self, aPoint: NSPoint) -> NSPoint {
msg_send![self, transformPoint: aPoint]
}
pub unsafe fn transformSize(&self, aSize: NSSize) -> NSSize {
msg_send![self, transformSize: aSize]
}
pub unsafe fn transformStruct(&self) -> NSAffineTransformStruct {
msg_send![self, transformStruct]
}
pub unsafe fn setTransformStruct(&self, transformStruct: NSAffineTransformStruct) {
msg_send![self, setTransformStruct: transformStruct]
}
#[method_id(transform)]
pub unsafe fn transform() -> Id<NSAffineTransform, Shared>;
# [method_id (initWithTransform :)]
pub unsafe fn initWithTransform(&self, transform: &NSAffineTransform) -> Id<Self, Shared>;
#[method_id(init)]
pub unsafe fn init(&self) -> Id<Self, Shared>;
# [method (translateXBy : yBy :)]
pub unsafe fn translateXBy_yBy(&self, deltaX: CGFloat, deltaY: CGFloat);
# [method (rotateByDegrees :)]
pub unsafe fn rotateByDegrees(&self, angle: CGFloat);
# [method (rotateByRadians :)]
pub unsafe fn rotateByRadians(&self, angle: CGFloat);
# [method (scaleBy :)]
pub unsafe fn scaleBy(&self, scale: CGFloat);
# [method (scaleXBy : yBy :)]
pub unsafe fn scaleXBy_yBy(&self, scaleX: CGFloat, scaleY: CGFloat);
#[method(invert)]
pub unsafe fn invert(&self);
# [method (appendTransform :)]
pub unsafe fn appendTransform(&self, transform: &NSAffineTransform);
# [method (prependTransform :)]
pub unsafe fn prependTransform(&self, transform: &NSAffineTransform);
# [method (transformPoint :)]
pub unsafe fn transformPoint(&self, aPoint: NSPoint) -> NSPoint;
# [method (transformSize :)]
pub unsafe fn transformSize(&self, aSize: NSSize) -> NSSize;
#[method(transformStruct)]
pub unsafe fn transformStruct(&self) -> NSAffineTransformStruct;
# [method (setTransformStruct :)]
pub unsafe fn setTransformStruct(&self, transformStruct: NSAffineTransformStruct);
}
);
Loading

0 comments on commit 9328d14

Please sign in to comment.