简体   繁体   English

如何重新导出具有具体类型的泛型函数?

[英]How do you re-export a generic function with concrete types?

I have a generic function foo<T>() .我有一个通用函数foo<T>() However, I want to specialize the function for the type usize .但是,我想专门化usize类型的usize I could declare another function and call the generic function.我可以声明另一个函数并调用通用函数。 However, I am wondering if there is any syntax sugar I could use, such as (pseudo code) pub use foo::<usize> as foo_usize .但是,我想知道是否有任何可以使用的语法糖,例如(伪代码) pub use foo::<usize> as foo_usize

use std::fmt::Debug;

fn foo<T: Debug>(a: &T) {
    println!("{:?}", a)
}

// I do not want generics, as I need to export this function as extern with #[no_mangle]
#[no_mangle]
pub extern "C" fn foo_usize(a: &usize) {
    foo::<usize>(a)
}

You will need to perform the monomorphization yourself by explicitly listing out the function and performing the call:您需要通过显式列出函数并执行调用来自己执行单态化:

use std::fmt::Debug;

fn foo<T: Debug>(a: &T) {
    println!("{:?}", a)
}

#[no_mangle]
pub extern "C" fn foo_usize(a: &usize) {
    foo::<usize>(a)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM