您的位置:首页 > 游戏 > 手游 > 克拉玛依商城网站建设平台_163免费企业邮箱_赣州seo_网站推广软文范例

克拉玛依商城网站建设平台_163免费企业邮箱_赣州seo_网站推广软文范例

2025/4/24 12:59:19 来源:https://blog.csdn.net/wowotuo/article/details/147281925  浏览:    关键词:克拉玛依商城网站建设平台_163免费企业邮箱_赣州seo_网站推广软文范例
克拉玛依商城网站建设平台_163免费企业邮箱_赣州seo_网站推广软文范例

*const () 可以替代泛型,更加灵活。下面举了两个完全不一样的数据结构Foo和Bar;以及不同的函数,来说明。

一、 代码


trait Work {fn process(&self);
}
struct Foo(String);impl Work for Foo {fn process(&self) {println!("process work  from Foo : {}", self.0);}
}
struct Bar{code:String,open:f64,high:f64,low:f64,close:f64,
}
impl Bar{fn new(code:String,open:f64,high:f64,low:f64,close:f64)->Self{Self{code,open,high,low,close,}}
}
impl Work for Bar {fn process(&self) {println!("process work  from Bar : {}", self.code);}
}
fn fn_foo(foo: &Foo){println!("call fn_foo, output ->{}", foo.0);
}
fn fn_bar(bar: &Bar){println!("call fn_bar, output ->code:{} open:{} high:{} close:{} low:{},", bar.code,bar.open,bar.high,bar.close,bar.low);
}
fn main() {let foo = Foo("foo".into());let bar = Bar::new("600036.SH".into(),36.9,37.8,36.7,36.4);let unsafe_fn_foo: unsafe fn(*const ()) = fn_const_ptr(fn_foo as fn(&Foo));process_method::<Foo>( unsafe_fn_foo,&foo);let unsafe_fn_bar: unsafe fn(*const ()) = fn_const_ptr(fn_bar as fn(&Bar));process_method::<Bar>(unsafe_fn_bar,&bar);let foo_ptr: *const () = data_const_ptr(&foo);let bar_ptr: *const () = data_const_ptr(&bar);process_data::<Foo>(foo_ptr);process_data::<Bar>(bar_ptr);
}fn process_data<T: Work>(data: *const ()) {let obj: *const T = data.cast::<T>();unsafe{(*obj).process()};
}
fn process_method<T>(f:  unsafe fn(*const ()),data:&T) {let ptr: *const () = data as *const T as _; // 这里"_"type eraseunsafe{f(ptr)};
}
fn data_const_ptr<T>(data: &T) -> *const () {data as *const T as _ // 这里"_"type erase
}
fn fn_const_ptr<T>(f: fn(&T)) -> unsafe fn(*const ()) {unsafe {std::mem::transmute(f)}
}

二、输出结果

   Compiling type_erase v0.1.0 (D:\my_program\type_erase)Finished `release` profile [optimized] target(s) in 2.08sRunning `target\release\type_erase.exe`
call fn_foo, output ->foo
call fn_bar, output ->code:600036.SH open:36.9 high:37.8 close:36.4 low:36.7,
process work  from Foo : foo
process work  from Bar : 600036.SH

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com