Skip to content

Commit 840b738

Browse files
simlayemilio
authored andcommitted
Initial stuff for changing ownership and adding inheritance
1 parent f4d10c3 commit 840b738

13 files changed

+77
-51
lines changed

src/codegen/mod.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3806,7 +3806,7 @@ fn objc_method_codegen(
38063806
}
38073807
} else {
38083808
let fn_args = fn_args.clone();
3809-
let args = iter::once(quote! { self }).chain(fn_args.into_iter());
3809+
let args = iter::once(quote! { &self }).chain(fn_args.into_iter());
38103810
quote! {
38113811
( #( #args ),* ) #fn_ret
38123812
}
@@ -3825,7 +3825,7 @@ fn objc_method_codegen(
38253825
}
38263826
} else {
38273827
quote! {
3828-
msg_send!(self, #methods_and_args)
3828+
msg_send!(*self, #methods_and_args)
38293829
}
38303830
};
38313831

@@ -3901,7 +3901,7 @@ impl CodeGenerator for ObjCInterface {
39013901
if !self.is_category() && !self.is_protocol() {
39023902
let struct_block = quote! {
39033903
#[repr(transparent)]
3904-
#[derive(Clone, Copy)]
3904+
#[derive(Clone)]
39053905
pub struct #class_name(pub id);
39063906
impl std::ops::Deref for #class_name {
39073907
type Target = objc::runtime::Object;
@@ -3962,6 +3962,17 @@ impl CodeGenerator for ObjCInterface {
39623962
}
39633963
};
39643964
result.push(impl_trait);
3965+
if !parent.is_template() {
3966+
let parent_struct_name = ctx.rust_ident(parent.name());
3967+
let from_block = quote! {
3968+
impl From<#class_name> for #parent_struct_name {
3969+
fn from(child: #class_name) -> #parent_struct_name {
3970+
#parent_struct_name(child.0)
3971+
}
3972+
}
3973+
};
3974+
result.push(from_block);
3975+
}
39653976
parent.parent_class
39663977
} else {
39673978
None

tests/expectations/tests/libclang-9/objc_inheritance.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extern crate objc;
1111
#[allow(non_camel_case_types)]
1212
pub type id = *mut objc::runtime::Object;
1313
#[repr(transparent)]
14-
#[derive(Clone, Copy)]
14+
#[derive(Clone)]
1515
pub struct Foo(pub id);
1616
impl std::ops::Deref for Foo {
1717
type Target = objc::runtime::Object;
@@ -28,7 +28,7 @@ impl Foo {
2828
impl IFoo for Foo {}
2929
pub trait IFoo: Sized + std::ops::Deref {}
3030
#[repr(transparent)]
31-
#[derive(Clone, Copy)]
31+
#[derive(Clone)]
3232
pub struct Bar(pub id);
3333
impl std::ops::Deref for Bar {
3434
type Target = objc::runtime::Object;
@@ -43,10 +43,15 @@ impl Bar {
4343
}
4444
}
4545
impl IFoo for Bar {}
46+
impl From<Bar> for Foo {
47+
fn from(child: Bar) -> Foo {
48+
Foo(child.0)
49+
}
50+
}
4651
impl IBar for Bar {}
4752
pub trait IBar: Sized + std::ops::Deref {}
4853
#[repr(transparent)]
49-
#[derive(Clone, Copy)]
54+
#[derive(Clone)]
5055
pub struct Baz(pub id);
5156
impl std::ops::Deref for Baz {
5257
type Target = objc::runtime::Object;
@@ -61,6 +66,16 @@ impl Baz {
6166
}
6267
}
6368
impl IBar for Baz {}
69+
impl From<Baz> for Bar {
70+
fn from(child: Baz) -> Bar {
71+
Bar(child.0)
72+
}
73+
}
6474
impl IFoo for Baz {}
75+
impl From<Baz> for Foo {
76+
fn from(child: Baz) -> Foo {
77+
Foo(child.0)
78+
}
79+
}
6580
impl IBaz for Baz {}
6681
pub trait IBaz: Sized + std::ops::Deref {}

tests/expectations/tests/libclang-9/objc_template.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extern crate objc;
1111
#[allow(non_camel_case_types)]
1212
pub type id = *mut objc::runtime::Object;
1313
#[repr(transparent)]
14-
#[derive(Clone, Copy)]
14+
#[derive(Clone)]
1515
pub struct Foo(pub id);
1616
impl std::ops::Deref for Foo {
1717
type Target = objc::runtime::Object;
@@ -27,15 +27,15 @@ impl Foo {
2727
}
2828
impl<ObjectType: 'static> IFoo<ObjectType> for Foo {}
2929
pub trait IFoo<ObjectType>: Sized + std::ops::Deref {
30-
unsafe fn get(self) -> u64
30+
unsafe fn get(&self) -> u64
3131
where
3232
<Self as std::ops::Deref>::Target: objc::Message + Sized,
3333
{
34-
msg_send!(self, get)
34+
msg_send!(*self, get)
3535
}
3636
}
3737
#[repr(transparent)]
38-
#[derive(Clone, Copy)]
38+
#[derive(Clone)]
3939
pub struct FooMultiGeneric(pub id);
4040
impl std::ops::Deref for FooMultiGeneric {
4141
type Target = objc::runtime::Object;
@@ -56,10 +56,10 @@ impl<KeyType: 'static, ObjectType: 'static>
5656
pub trait IFooMultiGeneric<KeyType, ObjectType>:
5757
Sized + std::ops::Deref
5858
{
59-
unsafe fn objectForKey_(self, key: u64) -> u64
59+
unsafe fn objectForKey_(&self, key: u64) -> u64
6060
where
6161
<Self as std::ops::Deref>::Target: objc::Message + Sized,
6262
{
63-
msg_send!(self, objectForKey: key)
63+
msg_send!(*self, objectForKey: key)
6464
}
6565
}

tests/expectations/tests/objc_category.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extern crate objc;
1111
#[allow(non_camel_case_types)]
1212
pub type id = *mut objc::runtime::Object;
1313
#[repr(transparent)]
14-
#[derive(Clone, Copy)]
14+
#[derive(Clone)]
1515
pub struct Foo(pub id);
1616
impl std::ops::Deref for Foo {
1717
type Target = objc::runtime::Object;
@@ -27,19 +27,19 @@ impl Foo {
2727
}
2828
impl IFoo for Foo {}
2929
pub trait IFoo: Sized + std::ops::Deref {
30-
unsafe fn method(self)
30+
unsafe fn method(&self)
3131
where
3232
<Self as std::ops::Deref>::Target: objc::Message + Sized,
3333
{
34-
msg_send!(self, method)
34+
msg_send!(*self, method)
3535
}
3636
}
3737
impl Foo_BarCategory for Foo {}
3838
pub trait Foo_BarCategory: Sized + std::ops::Deref {
39-
unsafe fn categoryMethod(self)
39+
unsafe fn categoryMethod(&self)
4040
where
4141
<Self as std::ops::Deref>::Target: objc::Message + Sized,
4242
{
43-
msg_send!(self, categoryMethod)
43+
msg_send!(*self, categoryMethod)
4444
}
4545
}

tests/expectations/tests/objc_class.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extern "C" {
1414
pub static mut fooVar: Foo;
1515
}
1616
#[repr(transparent)]
17-
#[derive(Clone, Copy)]
17+
#[derive(Clone)]
1818
pub struct Foo(pub id);
1919
impl std::ops::Deref for Foo {
2020
type Target = objc::runtime::Object;
@@ -30,10 +30,10 @@ impl Foo {
3030
}
3131
impl IFoo for Foo {}
3232
pub trait IFoo: Sized + std::ops::Deref {
33-
unsafe fn method(self)
33+
unsafe fn method(&self)
3434
where
3535
<Self as std::ops::Deref>::Target: objc::Message + Sized,
3636
{
37-
msg_send!(self, method)
37+
msg_send!(*self, method)
3838
}
3939
}

tests/expectations/tests/objc_class_method.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extern crate objc;
1111
#[allow(non_camel_case_types)]
1212
pub type id = *mut objc::runtime::Object;
1313
#[repr(transparent)]
14-
#[derive(Clone, Copy)]
14+
#[derive(Clone)]
1515
pub struct Foo(pub id);
1616
impl std::ops::Deref for Foo {
1717
type Target = objc::runtime::Object;

tests/expectations/tests/objc_interface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extern crate objc;
1111
#[allow(non_camel_case_types)]
1212
pub type id = *mut objc::runtime::Object;
1313
#[repr(transparent)]
14-
#[derive(Clone, Copy)]
14+
#[derive(Clone)]
1515
pub struct Foo(pub id);
1616
impl std::ops::Deref for Foo {
1717
type Target = objc::runtime::Object;

tests/expectations/tests/objc_interface_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extern crate objc;
1111
#[allow(non_camel_case_types)]
1212
pub type id = *mut objc::runtime::Object;
1313
#[repr(transparent)]
14-
#[derive(Clone, Copy)]
14+
#[derive(Clone)]
1515
pub struct Foo(pub id);
1616
impl std::ops::Deref for Foo {
1717
type Target = objc::runtime::Object;

tests/expectations/tests/objc_method.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extern crate objc;
1111
#[allow(non_camel_case_types)]
1212
pub type id = *mut objc::runtime::Object;
1313
#[repr(transparent)]
14-
#[derive(Clone, Copy)]
14+
#[derive(Clone)]
1515
pub struct Foo(pub id);
1616
impl std::ops::Deref for Foo {
1717
type Target = objc::runtime::Object;
@@ -27,48 +27,48 @@ impl Foo {
2727
}
2828
impl IFoo for Foo {}
2929
pub trait IFoo: Sized + std::ops::Deref {
30-
unsafe fn method(self)
30+
unsafe fn method(&self)
3131
where
3232
<Self as std::ops::Deref>::Target: objc::Message + Sized,
3333
{
34-
msg_send!(self, method)
34+
msg_send!(*self, method)
3535
}
36-
unsafe fn methodWithInt_(self, foo: ::std::os::raw::c_int)
36+
unsafe fn methodWithInt_(&self, foo: ::std::os::raw::c_int)
3737
where
3838
<Self as std::ops::Deref>::Target: objc::Message + Sized,
3939
{
40-
msg_send!(self, methodWithInt: foo)
40+
msg_send!(*self, methodWithInt: foo)
4141
}
42-
unsafe fn methodWithFoo_(self, foo: Foo)
42+
unsafe fn methodWithFoo_(&self, foo: Foo)
4343
where
4444
<Self as std::ops::Deref>::Target: objc::Message + Sized,
4545
{
46-
msg_send!(self, methodWithFoo: foo)
46+
msg_send!(*self, methodWithFoo: foo)
4747
}
48-
unsafe fn methodReturningInt(self) -> ::std::os::raw::c_int
48+
unsafe fn methodReturningInt(&self) -> ::std::os::raw::c_int
4949
where
5050
<Self as std::ops::Deref>::Target: objc::Message + Sized,
5151
{
52-
msg_send!(self, methodReturningInt)
52+
msg_send!(*self, methodReturningInt)
5353
}
54-
unsafe fn methodReturningFoo(self) -> Foo
54+
unsafe fn methodReturningFoo(&self) -> Foo
5555
where
5656
<Self as std::ops::Deref>::Target: objc::Message + Sized,
5757
{
58-
msg_send!(self, methodReturningFoo)
58+
msg_send!(*self, methodReturningFoo)
5959
}
6060
unsafe fn methodWithArg1_andArg2_andArg3_(
61-
self,
61+
&self,
6262
intvalue: ::std::os::raw::c_int,
6363
ptr: *mut ::std::os::raw::c_char,
6464
floatvalue: f32,
6565
) where
6666
<Self as std::ops::Deref>::Target: objc::Message + Sized,
6767
{
68-
msg_send ! ( self , methodWithArg1 : intvalue andArg2 : ptr andArg3 : floatvalue )
68+
msg_send ! ( * self , methodWithArg1 : intvalue andArg2 : ptr andArg3 : floatvalue )
6969
}
7070
unsafe fn methodWithAndWithoutKeywords_arg2Name__arg4Name_(
71-
self,
71+
&self,
7272
arg1: ::std::os::raw::c_int,
7373
arg2: f32,
7474
arg3: f32,
@@ -77,7 +77,7 @@ pub trait IFoo: Sized + std::ops::Deref {
7777
where
7878
<Self as std::ops::Deref>::Target: objc::Message + Sized,
7979
{
80-
msg_send ! ( self , methodWithAndWithoutKeywords : arg1 arg2Name : arg2 arg3 : arg3 arg4Name : arg4 )
80+
msg_send ! ( * self , methodWithAndWithoutKeywords : arg1 arg2Name : arg2 arg3 : arg3 arg4Name : arg4 )
8181
}
8282
}
8383
pub type instancetype = id;

tests/expectations/tests/objc_method_clash.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extern crate objc;
1111
#[allow(non_camel_case_types)]
1212
pub type id = *mut objc::runtime::Object;
1313
#[repr(transparent)]
14-
#[derive(Clone, Copy)]
14+
#[derive(Clone)]
1515
pub struct Foo(pub id);
1616
impl std::ops::Deref for Foo {
1717
type Target = objc::runtime::Object;
@@ -27,11 +27,11 @@ impl Foo {
2727
}
2828
impl IFoo for Foo {}
2929
pub trait IFoo: Sized + std::ops::Deref {
30-
unsafe fn foo(self)
30+
unsafe fn foo(&self)
3131
where
3232
<Self as std::ops::Deref>::Target: objc::Message + Sized,
3333
{
34-
msg_send!(self, foo)
34+
msg_send!(*self, foo)
3535
}
3636
unsafe fn class_foo()
3737
where

tests/expectations/tests/objc_pointer_return_types.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extern crate objc;
1111
#[allow(non_camel_case_types)]
1212
pub type id = *mut objc::runtime::Object;
1313
#[repr(transparent)]
14-
#[derive(Clone, Copy)]
14+
#[derive(Clone)]
1515
pub struct Bar(pub id);
1616
impl std::ops::Deref for Bar {
1717
type Target = objc::runtime::Object;
@@ -28,7 +28,7 @@ impl Bar {
2828
impl IBar for Bar {}
2929
pub trait IBar: Sized + std::ops::Deref {}
3030
#[repr(transparent)]
31-
#[derive(Clone, Copy)]
31+
#[derive(Clone)]
3232
pub struct Foo(pub id);
3333
impl std::ops::Deref for Foo {
3434
type Target = objc::runtime::Object;
@@ -44,11 +44,11 @@ impl Foo {
4444
}
4545
impl IFoo for Foo {}
4646
pub trait IFoo: Sized + std::ops::Deref {
47-
unsafe fn methodUsingBar_(self, my_bar: Bar)
47+
unsafe fn methodUsingBar_(&self, my_bar: Bar)
4848
where
4949
<Self as std::ops::Deref>::Target: objc::Message + Sized,
5050
{
51-
msg_send!(self, methodUsingBar: my_bar)
51+
msg_send!(*self, methodUsingBar: my_bar)
5252
}
5353
unsafe fn methodReturningBar() -> Bar
5454
where

tests/expectations/tests/objc_property_fnptr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extern crate objc;
1111
#[allow(non_camel_case_types)]
1212
pub type id = *mut objc::runtime::Object;
1313
#[repr(transparent)]
14-
#[derive(Clone, Copy)]
14+
#[derive(Clone)]
1515
pub struct Foo(pub id);
1616
impl std::ops::Deref for Foo {
1717
type Target = objc::runtime::Object;
@@ -28,7 +28,7 @@ impl Foo {
2828
impl IFoo for Foo {}
2929
pub trait IFoo: Sized + std::ops::Deref {
3030
unsafe fn func(
31-
self,
31+
&self,
3232
) -> ::std::option::Option<
3333
unsafe extern "C" fn(
3434
arg1: ::std::os::raw::c_char,
@@ -39,10 +39,10 @@ pub trait IFoo: Sized + std::ops::Deref {
3939
where
4040
<Self as std::ops::Deref>::Target: objc::Message + Sized,
4141
{
42-
msg_send!(self, func)
42+
msg_send!(*self, func)
4343
}
4444
unsafe fn setFunc_(
45-
self,
45+
&self,
4646
func: ::std::option::Option<
4747
unsafe extern "C" fn(
4848
arg1: ::std::os::raw::c_char,
@@ -53,6 +53,6 @@ pub trait IFoo: Sized + std::ops::Deref {
5353
) where
5454
<Self as std::ops::Deref>::Target: objc::Message + Sized,
5555
{
56-
msg_send!(self, setFunc: func)
56+
msg_send!(*self, setFunc: func)
5757
}
5858
}

0 commit comments

Comments
 (0)