Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

macros

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?

like image 954
dragostis Avatar asked Feb 21 '16 12:02

dragostis


1 Answers

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.

like image 99
DK. Avatar answered Oct 31 '22 00:10

DK.