0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-21 15:04:33 -05:00
This commit is contained in:
Bert Belder 2019-10-08 02:32:58 +02:00
parent 16e82f13a0
commit 1b61d2c4da
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461

View file

@ -46,16 +46,42 @@ mod channel {
}
}
pub trait ChannelOverrides {
pub trait ChannelOverrides: AsChannel {
fn extender(&self) -> &ChannelExtender;
fn extender_mut(&mut self) -> &mut ChannelExtender;
fn method1(&mut self) {
ChannelDefaults::method1(self.extender_mut())
ChannelDefaults::method1(self.extender_mut().as_mut())
}
fn method2(&self) -> i32;
}
pub trait AsChannel {
fn as_channel(&self) -> &Channel;
fn as_channel_mut(&mut self) -> &mut Channel;
}
impl AsChannel for ChannelExtender {
fn as_channel(&self) -> &Channel {
&self.cxx_channel
}
fn as_channel_mut(&mut self) -> &mut Channel {
&mut self.cxx_channel
}
}
impl<T> AsChannel for T
where
T: ChannelOverrides,
{
fn as_channel(&self) -> &Channel {
&self.extender().cxx_channel
}
fn as_channel_mut(&mut self) -> &mut Channel {
&mut self.extender_mut().cxx_channel
}
}
pub struct ChannelExtender {
cxx_channel: Channel,
extender_offset: usize,
@ -143,58 +169,19 @@ mod channel {
}
}
impl std::ops::Deref for ChannelExtender {
type Target = Channel;
fn deref(&self) -> &Channel {
impl std::convert::AsRef<Channel> for ChannelExtender {
fn as_ref(&self) -> &Channel {
&self.cxx_channel
}
}
impl std::ops::DerefMut for ChannelExtender {
fn deref_mut(&mut self) -> &mut Channel {
impl std::convert::AsMut<Channel> for ChannelExtender {
fn as_mut(&mut self) -> &mut Channel {
&mut self.cxx_channel
}
}
}
mod trying {
use super::channel::*;
#[allow(dead_code)]
pub struct Session {
a: i32,
channel_extender: ChannelExtender,
b: i32,
}
impl ChannelOverrides for Session {
fn extender(&self) -> &ChannelExtender {
&self.channel_extender
}
fn extender_mut(&mut self) -> &mut ChannelExtender {
&mut self.channel_extender
}
fn method1(&mut self) {
println!("overriden a() called");
self.a += self.b;
}
fn method2(&self) -> i32 {
println!("overriden b() called");
self.a * self.b
}
}
impl Session {
pub fn new() -> Self {
Self {
channel_extender: ChannelExtender::new::<Self>(),
a: 2,
b: 3,
}
}
}
}
mod util {
use std::marker::PhantomData;
use std::mem::size_of;
@ -249,6 +236,49 @@ mod util {
}
}
fn main() {
trying::Session::new();
mod example {
use super::channel::*;
#[allow(dead_code)]
pub struct Example {
a: i32,
channel_extender: ChannelExtender,
b: i32,
}
impl ChannelOverrides for Example {
fn extender(&self) -> &ChannelExtender {
&self.channel_extender
}
fn extender_mut(&mut self) -> &mut ChannelExtender {
&mut self.channel_extender
}
fn method1(&mut self) {
println!("overriden a() called");
self.a += self.b;
}
fn method2(&self) -> i32 {
println!("overriden b() called");
self.a * self.b
}
}
impl Example {
pub fn new() -> Self {
Self {
channel_extender: ChannelExtender::new::<Self>(),
a: 2,
b: 3,
}
}
}
}
fn main() {
use channel::*;
use example::*;
let mut ex = Example::new();
let chan = ex.as_channel_mut();
chan.method1();
println!("{}", chan.method2());
}