简体   繁体   中英

Is there any way to use private functions in public macros in Rust?

There is a variadic C function that I cannot call outside of a macro. This macro is public which it should be, but the C function with variadic arguments should not be visible.

Is there any way to only make it visible for inside of the macro? Or maybe a way to keep the function outside of the docs?

The only thing you can do is hide such "internal" symbols such that they do not appear in the documentation. For example:

#[macro_export]
macro_rules! custom_abort {
    ($($args:tt)*) => {
        match format!($($args)*) {
            msg => $crate::custom_abort__(&msg)
        }
    };
}

/// This is an implementation detail and *should not* be called directly!
#[doc(hidden)]
pub fn custom_abort__(msg: &str) -> ! {
    use std::io::Write;
    let _ = writeln!(std::io::stderr(), "{}", msg);
    std::process::exit(1);
}

As you might expect, this absolutely does not prevent someone from calling custom_abort__ directly. But really, if someone ignores warnings in the comments and does so anyway, feel free to laugh at them when their code breaks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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