use std::mem;
fn print_object(p:&dyn Bird){
let (data,vtable):(*const usize,*const usize)=unsafe{mem::transmute(p)};
println!("data :{:p} vtable:{:p}",data,vtable);
unsafe{
println!("data in vtable:{} {} {} {} {}",*vtable,*vtable.offset(1),
*vtable.offset(2),*vtable.offset(3),*vtable.offset(4));
println!("data in data:{:x} {} {} {} {} " ,*data,*data.offset(1),
*data.offset(2),*data.offset(3),*data.offset(4));
}
}
trait Bird{
fn fly(&self);
}
struct Duck{x:i64}
struct Swan{y:u32}
impl Bird for Duck{
fn fly(&self){
println!("{}",self.x);
}
}
impl Bird for Swan{
fn fly(&self){
println!("{}",self.y);
}
}
fn main(){
let duck=Duck{x:1};
let swan=Swan{y:2};
let p_duck=&duck;
let p_bird_swan=&swan as &dyn Bird;
let p_bird_duck=p_duck as &dyn Bird;
let Duck_fly:usize=Duck::fly as usize;
let Swan_fly:usize=Swan::fly as usize;
// let addr_duck=&duck as usize;
// let addr_swan=&swan as usize;
println!("data addr:{:p} {:p}",&duck,&swan);
println!("func addr:{} {}",Duck_fly,Swan_fly);
print_object(p_bird_duck);
print_object(p_bird_swan);
}