polish more

This commit is contained in:
Bert Belder 2019-10-08 02:13:18 +02:00
parent 48db2272ca
commit 16e82f13a0
No known key found for this signature in database
GPG Key ID: 7A77887B2E2ED461

View File

@ -4,12 +4,12 @@ mod channel {
extern "C" { extern "C" {
// Call a method/destructor; virtual methods use C++ dynamic dispatch. // Call a method/destructor; virtual methods use C++ dynamic dispatch.
fn Channel__DTOR(this: &mut Channel) -> (); fn Channel__DTOR(this: &mut Channel) -> ();
fn Channel__a(this: &mut Channel) -> (); fn Channel__method1(this: &mut Channel) -> ();
fn Channel__b(this: &Channel) -> i32; fn Channel__method2(this: &Channel) -> i32;
// Call a method of a specific class implementation, bypassing dynamic // Call a method of a specific class implementation, bypassing dynamic
// dispatch. C++ equivalent: `my_channel.Channel::a()`. // dispatch. C++ equivalent: `my_channel.Channel::a()`.
fn Channel__Channel__a(this: &mut Channel) -> (); fn Channel__Channel__method1(this: &mut Channel) -> ();
// Constructs a special class derived from Channel that forwards all // Constructs a special class derived from Channel that forwards all
// virtual method invocations to rust. It is assumed that this subclass // virtual method invocations to rust. It is assumed that this subclass
@ -25,11 +25,11 @@ mod channel {
#[allow(dead_code)] #[allow(dead_code)]
impl Channel { impl Channel {
pub fn a(&mut self) { pub fn method1(&mut self) {
unsafe { Channel__a(self) } unsafe { Channel__method1(self) }
} }
pub fn b(&self) -> i32 { pub fn method2(&self) -> i32 {
unsafe { Channel__b(self) } unsafe { Channel__method2(self) }
} }
} }
@ -41,8 +41,8 @@ mod channel {
pub struct ChannelDefaults; pub struct ChannelDefaults;
impl ChannelDefaults { impl ChannelDefaults {
pub fn a(this: &mut Channel) { pub fn method1(this: &mut Channel) {
unsafe { Channel__Channel__a(this) } unsafe { Channel__Channel__method1(this) }
} }
} }
@ -50,10 +50,10 @@ mod channel {
fn extender(&self) -> &ChannelExtender; fn extender(&self) -> &ChannelExtender;
fn extender_mut(&mut self) -> &mut ChannelExtender; fn extender_mut(&mut self) -> &mut ChannelExtender;
fn a(&mut self) { fn method1(&mut self) {
ChannelDefaults::a(self.extender_mut()) ChannelDefaults::method1(self.extender_mut())
} }
fn b(&self) -> i32; fn method2(&self) -> i32;
} }
pub struct ChannelExtender { pub struct ChannelExtender {
@ -63,12 +63,16 @@ mod channel {
} }
#[no_mangle] #[no_mangle]
unsafe extern "C" fn Channel__OVERRIDE__a__DISPATCH(this: &mut Channel) { unsafe extern "C" fn Channel__OVERRIDE__method1__DISPATCH(
ChannelExtender::dispatch_mut(this).a() this: &mut Channel,
) {
ChannelExtender::dispatch_mut(this).method1()
} }
#[no_mangle] #[no_mangle]
unsafe extern "C" fn Channel__OVERRIDE__b__DISPATCH(this: &Channel) -> i32 { unsafe extern "C" fn Channel__OVERRIDE__method2__DISPATCH(
ChannelExtender::dispatch(this).b() this: &Channel,
) -> i32 {
ChannelExtender::dispatch(this).method2()
} }
impl ChannelExtender { impl ChannelExtender {
@ -159,32 +163,33 @@ mod trying {
#[allow(dead_code)] #[allow(dead_code)]
pub struct Session { pub struct Session {
a: i32, a: i32,
b: String, channel_extender: ChannelExtender,
c: ChannelExtender, b: i32,
} }
impl ChannelOverrides for Session { impl ChannelOverrides for Session {
fn extender(&self) -> &ChannelExtender { fn extender(&self) -> &ChannelExtender {
&self.c &self.channel_extender
} }
fn extender_mut(&mut self) -> &mut ChannelExtender { fn extender_mut(&mut self) -> &mut ChannelExtender {
&mut self.c &mut self.channel_extender
} }
fn a(&mut self) { fn method1(&mut self) {
println!("ChannelExtender a!"); println!("overriden a() called");
self.a += self.b;
} }
fn b(&self) -> i32 { fn method2(&self) -> i32 {
println!("ChannelExtender b!"); println!("overriden b() called");
42 self.a * self.b
} }
} }
impl Session { impl Session {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
a: 1, channel_extender: ChannelExtender::new::<Self>(),
b: "abc".to_owned(), a: 2,
c: ChannelExtender::new::<Self>(), b: 3,
} }
} }
} }